Search code examples
pythondiscorddiscord.py

Can multiple listeners be used in a cog?


Problem

I wanted to use multiple listener in a cog file in discord.py but it wont work. Only the last listener in the code gets called. How to solve it?

Code

class test(commands.Cog):
    @commands.Cog.listener()
    async def on_message(self, message):
        # code of this function

    @commands.Cog.listener()
    async def on_message(self, message):
        # code of this function

def setup(bot):
    bot.add_cog(test(bot))

Solution

  • https://discordpy.readthedocs.io/en/stable/ext/commands/api.html?highlight=commands%20cog%20listener#discord.ext.commands.Cog.listener

    class test(commands.Cog):
        @commands.Cog.listener("on_message")
        async def listener_one(self, message):
            # code of this function
    
        @commands.Cog.listener("on_message")
        async def listener_two(self, message):
            # code of this function
    
    def setup(bot):
        bot.add_cog(test(bot))