#๐Ÿ”’ Modification of a listener

4 messages ยท Page 1 of 1 (latest)

hollow cave
#

Hello, i tried to edit the listener foncton to handle it for it can handle in case of an error, it found the attr but it doesnt edit the thing


    @commands.Cog.listener()
    async def on_cog_load(self):
        for cog in self.bot.cogs.values():
            for attr_name in dir(cog):
                attr = getattr(cog, attr_name)
                if callable(attr) and hasattr(attr, "__cog_listener__"):
                    wrapped_listener = self.listener_error_handle(attr)
                    setattr(cog, attr_name, wrapped_listener)

    @staticmethod
    def listener_error_handle(func):
        @wraps(func)
        async def wrapper(*args, **kwargs):
            try:
                return await func(*args, **kwargs)
            except Exception as e:
                logger.error(f"Error in {func.__name__}: {e}")

        return wrapper

    @commands.Cog.listener()
    async def on_message(self, message):
        if message.content == "error":
            raise ValueError("This is a test error.")
vernal cloakBOT
#

@hollow cave

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

vernal cloakBOT
#

@hollow cave

Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.