Discord.py bot装箱工具功能出现错误

时间:2018-06-19 20:23:17

标签: python discord.py

因此,我希望机器人在25个注册的用户中给1个,他们说要获得一个具有5个硬币的板条箱。

代码:

@bot.event
async def on_message(ctx):
    primary_id = ctx.message.author.id
    if primary_id not in amounts:
        print("")
    else:
        bob = random.randint(1,25)
        if bob == 1:
             await bot.say("You got a crate! It contained 5 coins!")
             amounts[primary_id] += 5
             with open('amounts.json', 'w+') as f:
                json.dump(amounts, f)
        else:
           print("")

每当我输入内容时出错:

Ignoring exception in on_message
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/discord/client.py", line 307, in _run_event
    yield from getattr(self, event)(*args, **kwargs)
  File "/home/zorin/Desktop/den.py", line 68, in on_message
    id = ctx.message.author.id
AttributeError: 'Message' object has no attribute 'message'

1 个答案:

答案 0 :(得分:2)

on_message使用Message对象,而不是Context对象。

您可能还应该在await bot.process_commands(message)协程的末尾添加on_messageOtherwise none of your commands will be called

bot.say will not work outside a command。请改用bot.send_message

@bot.event
async def on_message(message):
    primary_id = message.author.id
    if primary_id not in amounts:
        print("")
    else:
        bob = random.randint(1,25)
        if bob == 1:
             await bot.send_message(message.channel, "You got a crate! It contained 5 coins!")
             amounts[primary_id] += 5
             with open('amounts.json', 'w+') as f:
                json.dump(amounts, f)
        else:
           print("")
    await bot.process_commands(message)