簡體   English   中英

類型錯誤:'dict' object 不可調用 python3

[英]TypeError: 'dict' object is not callable python3

我只是用 python3 制作 discord 機器人

    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())

但是當我使用這個命令時出現錯誤

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

這個錯誤..我試着讓數據來聽寫..

問題是您將json.loads的返回值視為可調用的。 檢查此表以查看您可以從中獲得哪些可能的值。

我剛剛查看了discord 文檔,並且您應該傳遞一個字符串作為第一個參數,因此您不需要將data視為可調用的,因為在運行json.loads之后您可能會有一個字符串。

以下行可能會解決您的問題:

await channel.send(data)

但是,在任何情況下, data都不同於字符串,您需要對其進行轉換:

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

您始終可以像這樣將字典作為字符串發送

await channel.send(str(data))

但是......這可能不是數據的最友好表示。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM