How to not process commands in discord.py if the channel if private?

Viewed 83

My code :

    @commands.Cog.listener()
    async def on_message(self, message):
        if message.author.id == self.bot.user.id:
            return
        if message.channel.type == discord.ChannelType.private:
            await message.channel.send("Vous devez être dans un serveur pour realiser cette commande !")
            return

but it doesn't work, it's recognise that is was a private channel and it send the right message but it also process the command and send the answer ...

1 Answers

Don't include bot.process_commands(message) in the if code block which runs if the channel is private. Try this:

@commands.Cog.listener()
    async def on_message(self, message):
        if message.author.id == self.bot.user.id:
            bot.process_commands(message)
            return
        if message.channel.type == discord.ChannelType.private:
            await message.channel.send("Vous devez être dans un serveur pour realiser cette commande !")
            return
        #Don't add bot.process_commands(message) here.

Don't add bot.process_commands(message) outside the if block because if you do then it will run even if it is private.

Related