【问题标题】:Discord.py bot crate function gives errorDiscord.py bot crate 函数给出错误
【发布时间】:2018-06-19 20:23:16
【问题描述】:

所以,我想要一个机器人让每 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'

【问题讨论】:

    标签: python discord.py


    【解决方案1】:

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

    您可能还应该将 await bot.process_commands(message) 添加到您的 on_message 协程的末尾。 Otherwise 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)
    

    【讨论】:

    • 我对如何将所有这些都放入我的代码感到困惑?
    猜你喜欢
    • 2018-12-22
    • 2021-03-27
    • 1970-01-01
    • 2021-04-04
    • 1970-01-01
    • 2021-11-11
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    相关资源
    最近更新 更多