Closed huenique closed 3 years ago
The proposed solution defeats the purpose of cogs. And you're actually not supposed to use the Cog class and its methods outside its subclass. As for the issue, discord.py already has a feature that prevents these types of problems. Instead of overriding events such as on_message
, on_member_join
, and on_command_error
, supply the name of the event as an argument to Cog.listener()
. The listener method has a name
parameter, the name of the event being listened to which defaults to function or method's name. Good issue, @huenique, but next time actually read the documentation.
Dayong's features are contained within cogs, otherwise known as extensions. As such, it makes sense to define an event listener in the cog for certain features. However, due to how discord.py implements event listeners for bots or cogs, overriding an event like
on_message
is a fatal mistake. Doing so will overwrite any other explicitly definedon_message
method or function found somewhere else in the codebase. It may be fine for a bot with a fixed number of features, but for a multipurpose bot, like Dayong, at scale, it's bound to cause a number of issues.Solution I thought about moving the event listeners in a separate directory with each event having its own module. We only have to overwrite an event listener once. I think this looks OK:
In the above code, I registered the
delete_vulgar_message()
feature from the cogmoderator.py
. The feature takes the Message instance as an argument and does its job on it.Additional Context External event listeners allow you to hear and handle multiple events from different servers, channels, users, etc. A single listener for a specific event is usually enough to handle different operations.