PythonistaGuild / TwitchIO

An Async Bot/API wrapper for Twitch made in Python.
https://twitchio.dev
MIT License
798 stars 162 forks source link

Strange behavior on event handling in cogs #175

Closed rn4n closed 3 years ago

rn4n commented 3 years ago

Describe the bug I'm having behavior that I believe to be incorrect on creating of cogs. When I create more than one class inheriting from Cog, even though I don't instantiate it, it's being executed by the bot. And when I use the class, it seems to me that the bot is handling the same event more than once. Perhaps with the code below it will be easier to understand.

Are you using TwitchIO within a Discord Bot? No

What commit of TwitchIO are you using?

Name: twitchio
Version: 2.0.0b7
Summary: A Python IRC and API wrapper for Twitch.
Home-page: https://github.com/TwitchIO/TwitchIO
Author: TwitchIO
Author-email: UNKNOWN
License: MIT
Location: c:\users\renan\desktop\twitch-bot-template\.venv\lib\site-packages
Requires: aiohttp
Required-by:

To Reproduce Steps to reproduce the behaviour:

from twitchio.ext import commands

class FirstCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.event()
    async def event_message(self, message):
        if message.echo:
            return
        print("Hello from first cog")

class SecondCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.event()
    async def event_message(self, message):
        if message.echo:
            return
        print("Hello from second cog")

bot = commands.Bot(
    token="...",
    prefix="...",
    initial_channels=["..."],
)

bot.add_cog(FirstCog(bot))
# bot.add_cog(SecondCog(bot))  # do not add the second cog :D

bot.run()

When I type something on Twitch's chat, the console shows me:

Hello from first cog
Hello from second cog

If you uncomment the bot.add_cog(SecondCog(bot)) part and execute:

Hello from first cog
Hello from second cog
Hello from first cog 
Hello from second cog

Expected behaviour Only the events of the added cog should work. Do not call twice the same event in different cogs

(please complete the following information):

rn4n commented 3 years ago

Split the code in separated cogs also duplicate the event handling:

#bot/first_cog.py

from twitchio.ext import commands

class FirstCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.event()
    async def event_message(self, message):
        if message.echo:
            return
        print("Hello from first cog")

def prepare(bot):
    bot.add_cog(FirstCog(bot))
#bot/second_cog.py

from twitchio.ext import commands

class SecondCog(commands.Cog):
    def __init__(self, bot):
        self.bot = bot

    @commands.Cog.event()
    async def event_message(self, message):
        if message.echo:
            return
        print("Hello from second cog")

def prepare(bot):
    bot.add_cog(SecondCog(bot))
# __main__.py

from twitchio.ext import commands

bot = commands.Bot(
    token="...",
    prefix="...",
    initial_channels=["..."],
)

bot.load_module("bot.first_cog")
bot.load_module("bot.second_cog")

bot.run()

After any message on twitch chat:

Hello from first cog
Hello from first cog 
Hello from second cog
EvieePy commented 3 years ago

Fixed as of version 2.0.4. Thanks for the issue 👍 If you find any bugs in version 2.0.4 please let me know