discord.py on_message 正在破坏机器人

时间:2021-03-03 00:56:58

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

如果我把它放在 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 中 如果 client.user.提到_in(message): AttributeError: 'NoneType' 对象没有属性 '提到_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)

2 个答案:

答案 0 :(得分: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)

参考:

答案 1 :(得分:0)

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

相关问题