Discord.py命令运行两次

2024-10-03 13:28:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我最近在使用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)

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


Tags: in命令selfmessageifisawaitcommands
1条回答
网友
1楼 · 发布于 2024-10-03 13:28:08

您正在为同一条消息呼叫process_commands两次。这是因为默认的on_message侦听器已经调用了process_commands。所以cog中的on_message侦听器再次调用它。您应该从cogon_message中删除process_commands调用

相关问题 更多 >