简体   繁体   中英

TypeError: 'dict' object is not callable python3

I'm just making discord bot with python3

    if message.content.startswith('!meal'):
        with open('menu.json', encoding='utf-8') as json_file:
            data = json.load(json_file)
        channel = message.channel
        await channel.send('this is meal info!')
        await channel.send(data())

but I got errors when I used this command

~/pythonruby/discord bot/main.py", line 30, in on_message
    await channel.send(data())
TypeError: 'dict' object is not callable

this errors.. I tried make data to dict..

The problem is that you are treating as a callable the returned value of json.loads . Check this table to see what are the possible values you could get from.

I just reviewed the discord documentation , and you are supposed pass a string as the first parameter, so you do not need to treat data as a callable since after running json.loads you will probably have a string.

The following line will probably solve your problem:

await channel.send(data)

However, in any case data is something different than a string, you will need to cast it:

await channel.send(str(data)) # or f"{data}"

You can always send the dict as a string like this

await channel.send(str(data))

But... that may not be the friendliest representation of the data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM