【问题标题】:Error in the on_message function of a discord.py botdiscord.py 机器人的 on_message 函数出错
【发布时间】:2020-04-03 21:09:16
【问题描述】:

除了我创建的 send_message 函数外,整个机器人都可以工作。

@client.event
async def send_winners():
    img = Image.open("placement_temp.jpg")

    send_winners = json.loads(open('send_message.json').read())

    if send_winners == True:
        channel = 686026687145705505 #648365930639785985   


        await message.channel.send(channel, "<@&684816171316412458> And the Winners of this week are:", img)  # Error happening here
        send_winners = no
        with open("winner_send.json", "w") as fp:
            json.dump(send_winners, fp)
    await asyncio.sleep(10)

【问题讨论】:

  • 你能附上完整的错误信息吗?
  • NameError: name 'message' is not defined
  • 您能否包含错误中包含的完整堆栈跟踪?
  • 您也可以尝试将频道放在引号中:channel = "686026687145705505"
  • 文件“C:\Users\agweb\Documents\GitHub\TrophyBot\TrophyBot.py”,第 223 行,在 client.loop.create_task(send_winners(message)) NameError: name '消息'未定义

标签: python discord.py


【解决方案1】:

试试这个:

@client.event
async def send_winners(message):

    send_winners = json.loads(open('send_message.json').read())

    if send_winners == True:
        channel = 686026687145705505 #648365930639785985   


        await message.channel.send(channel, "<@&684816171316412458> And the Winners of this week are:", Image.open("placement_temp.jpg"))  # Error happening here
        send_winners = no
        with open("winner_send.json", "w") as fp:
            json.dump(send_winners, fp)
    await asyncio.sleep(10)

【讨论】:

  • 但消息仍未定义
  • 试试这个然后@client.event async def send_winners(message): send_winners = json.loads(open('send_message.json').read()) if send_winners == True: channel = 686026687145705505 #648365930639785985 await message.channel.send(channel, "&lt;@&amp;684816171316412458&gt; And the Winners of this week are:", Image.open("placement_temp.jpg")) # Error happening here send_winners = no with open("winner_send.json", "w") as fp: json.dump(send_winners, fp) await asyncio.sleep(10)
猜你喜欢
  • 2021-06-01
  • 2021-06-15
  • 2019-08-23
  • 1970-01-01
  • 2020-09-16
  • 1970-01-01
  • 2021-05-03
  • 2021-01-30
  • 2020-08-31
相关资源
最近更新 更多