【问题标题】:discord.py on_message is breaking botdiscord.py on_message 正在破坏机器人
【发布时间】:2021-06-01 13:52:58
【问题描述】:

如果我把它放在 main.py 中,机器人就会坏掉,没有命令可以工作,但我没有收到错误

@client.event
async def on_message(message):
    if client.user.mentioned_in(message):
        embed=discord.Embed(description='My prefix here is ?. You can see available commands by typing `?help`', color=0x850000)
        await message.channel.send(embed=embed) 



如果把它放在 cogs/commands.py 中,on_message 仍然不起作用,但其他命令起作用。但是每次我在不和谐中输入一些东西时都会出现这个错误,即使没有前缀

文件“C:\Users\BUGA\Documents\VSCODE\Python Projects\activity role\cogs\commands.py",第 17 行,在 on_message 中 if client.user.提到_in(message): AttributeError: 'NoneType' object has no attribute '提到_in'

@commands.Cog.listener()
async def on_message(self, message):
    if client.user.mentioned_in(message):
        embed=discord.Embed(description='My prefix here is ?. You can see available commands by typing ?help', color=0x850000)
    await message.channel.send(embed=embed)

【问题讨论】:

    标签: python python-3.x discord discord.py


    【解决方案1】:

    对于您的第一段代码,您可以使用await client.process_commands(message)。您可以查看the docs' Frequently Asked Questions 了解更多信息,但要简单:

    @client.event
    async def on_message(message):
       # do whatever you needed to do in your on_message
       if client.user.mentioned_in(message):
           await message.channel.send("My prefix for this server is ?")
    
       await client.process_commands(message)
    

    参考:

    【讨论】:

    • 这工作谢谢。当我将所有代码都放在一个文件中时 main.py on_message 在没有 await client.process_commands(message) 的情况下可以工作,为什么会这样?
    • 也许你没有使用命令扩展?
    【解决方案2】:

    根据您的错误,client.user 的评估结果为 None。从文档中,如果用户未登录,client.user 将返回 None。所以检查 client.user 返回的内容,然后查看登录用户是否可以获得不同的结果

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-02-17
      • 2021-06-15
      • 1970-01-01
      • 2016-12-07
      • 2020-09-16
      • 2021-05-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多