Silas Hayes-Williams
Silas Hayes-Williams

Reputation: 112

Discord.py commands run twice

I only recently encountered an issue when using discord.py and the discord.ext commands extension to make a bot

When I call a command it runs twice, causing many bugs and errors. It only started happening after adding this command

@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

however removing this command doesn't solve the issue. I was hosting on heroku but stopped that and ran it on my own pc to test but the problem still persisted . I used print functions in the commands to test if they were running twice and the strings inside them got outputted twice

I also have an on_message event

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

Edit: this also happens on some commands in my main file, but the strange thing is only some of them are affected

Upvotes: 1

Views: 3089

Answers (1)

Patrick Haugh
Patrick Haugh

Reputation: 60964

You're calling process_commands twice for the same message. This is because the default on_message listener already calls process_commands. So the on_message listener from your cog calls it a second time. You should remove the process_commands call from your cog on_message.

Upvotes: 5

Related Questions