Skip to content

feat: Event manager - #12

Open
joren-dev wants to merge 7 commits into
devfrom
event_manager
Open

feat: Event manager#12
joren-dev wants to merge 7 commits into
devfrom
event_manager

Conversation

@joren-dev

@joren-dev joren-dev commented Feb 6, 2023

Copy link
Copy Markdown
Owner

Implements a basic way to add events, I am thinking of implementing this concept.

When someone wants to add a feature, based on an event they have to do the following.

  1. Add a class with the feature name, create a class that derives from the abstract class named after the event they'd like to utilise. In this case this feature makes use of the on_message (OnMessageEvent) event.
    /events/print_message.py
from src.events.detail.event_base import OnMessageEvent


class PrintMessage(OnMessageEvent):
    # Overrided from abstract base class
    async def on_message(self, message) -> None:
        print(f"Message {message.author}: {message.content}")

within the on_message, one can do call and do anything they wish. The params will match the original event, to ensure you have the same functionality.

Then in EventManager one has to register their feature:

    def register_events(self):
        # Please include the py module that contains your feature and create an instance
        # fmt: off

        from src.events.print_message import PrintMessage
        self.on_message_events.append(PrintMessage())
        
        # fmt: on

And thats it, whatever they have written inside their on_message method will be ran the moment the event is called.

This is how it works behind the scenes:

@_discord_client.event
async def on_message(message):
    # Ensures commands work properly, wont process any commands without it.
    await _discord_client.process_commands(message)

    from src.managers.event_manager import EventManager

    for each in EventManager.on_message_events:
        await each.on_message(message)

Essentially just iterates over all the objects and calls the event method that we defined ourselves.

@joren-dev joren-dev added the work in progress Still in the making label Feb 6, 2023
@joren-dev joren-dev self-assigned this Feb 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

work in progress Still in the making

Projects

Development

Successfully merging this pull request may close these issues.

1 participant