Python Discord.py命令运行两次

Python Discord.py命令运行两次,python,python-3.x,discord.py,discord.py-rewrite,Python,Python 3.x,Discord.py,Discord.py Rewrite,我最近在使用discord.py和discord.ext命令扩展来创建机器人时遇到了一个问题 当我调用一个命令时,它会运行两次,导致许多bug和错误。它仅在添加此命令后才开始发生 @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

我最近在使用discord.py和discord.ext命令扩展来创建机器人时遇到了一个问题

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

@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上托管,但停止了,并在我自己的pc上运行它进行测试,但问题仍然存在。我在命令中使用print函数来测试它们是否运行了两次,其中的字符串是否输出了两次

我也有一个关于你的消息事件

@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)

编辑:在我的主文件中的某些命令上也会发生这种情况,但奇怪的是,只有其中一些命令会受到影响

您为同一消息调用了两次
process\u命令
。这是因为消息侦听器上的默认
已经调用了
process\u命令
。因此来自cog的
on_消息
侦听器再次调用它。您应该从cog
on\u消息中删除
process\u命令
调用

您有任何
on\u消息
事件吗?如果是这样,你能提供它们吗?是的,当然,我编辑了这篇文章以包含itI我用我的机器人尝试了你的代码,我没有遇到任何问题,所以你的问题可能来自其他地方。你确定你阻止了你的heroku工人吗?是的,如果我没有阻止你,你能告诉我具体的方法吗?这不应该是因为我在终端上添加了两次打印的打印语句,如果它在heroku上,它会被发送到heroku日志,这与您的问题无关,但如果您希望您的用户能够在之后写一句话!afk,您应该将
async def afk(self,ctx,userMessage):
替换为
async def afk(self,ctx,*,userMessage):
My bot依赖on_消息侦听器格式化消息内容,并在处理之前检查发送消息的人。是否有办法阻止discord.py在默认情况下处理命令?是的,如果您在bot本身而不是cog中的_message
侦听器上注册自己的
,则它将替换默认的。你可以让它通过而不做任何事情