Discord.py 命令 运行 两次

Discord.py commands run twice

我最近才在使用 discord.py 和 discord.ext 命令扩展制作机器人时遇到问题

当我调用一个命令时,它会运行两次,导致许多错误和错误。添加此命令后才开始发生

@commands.command()
    async def afk(self, ctx, userMessage):

        #if user is already afk, remove them from the afk dict, if not add them to it
        if ctx.message.author in self.afkUsers:
            self.afkUsers.pop(ctx.message.author)
        else:
            self.afkUsers[ctx.message.author] = userMessage

但是删除此命令并不能解决问题。我在 heroku 上托管但停止了它并 运行 它在我自己的电脑上进行测试但问题仍然存在。我在命令中使用打印函数来测试它们是否 运行 两次并且其中的字符串输出了两次

我也有一个 on_message 事件

@commands.Cog.listener()
    async def on_message(self, message):
        
        #if a member is mentioned but the member is afk, a message is sent
        textChannel = message.channel
        afkChannel = self.client.get_channel(690550327975346176)
        
        for member in message.mentions:
            if member in self.afkUsers:
                await textChannel.send(f"user is afk- {self.afkUsers[member]}")
            elif member in afkChannel.members:
                await textChannel.send("user is afk")
            

        #allows commands to work with on_message event
        await self.client.process_commands(message)

编辑:这也发生在我的主文件中的一些命令上,但是 st运行ge 的事情只是其中一些受到影响

您正在为同一条消息呼叫 process_commands 两次。这是因为默认的 on_message 侦听器已经调用了 process_commands。所以来自你的 cog 的 on_message 监听器第二次调用它。您应该从 cog on_message.

中删除 process_commands 调用