pycord icon indicating copy to clipboard operation
pycord copied to clipboard

More convenient and classic way to dispatch events

Open Dest0re opened this issue 2 years ago • 2 comments

Summary

A way to enumerate all event types and, as follows, maybe event dispatching system rework

What is the feature request for?

The core library

The Problem

Pycord doesn't have any ways to enumerate all of the event types via code, and event dispatching system looks like this:

self.dispatch('guild_available', guild)

As we can see, 'guild_available' (that is event type) is hardcoded, that is not a very flexible way, as for me. Also, to be honest, I'm not a great specialists in design, but current event storing system (as methods of a Client class) looks very strange and inconvenient for me, and the current client side is at odds with a view of an usual dispatching system, which looks about like this:

@client.add_listener(discord.EventType.guild_available)
async def _on_guild_available(guild):
    pass

and gives us a more flexible way to manipulate our event listeners.

The Ideal Solution

I think, it would be great to see client code as I wrote above. Also, I think that it can be convenient to have a some kind of dict to store async functions instead of storing them as methods with names like on_event_type.

The Current Solution

When we write

@client.event
async def on_event_name():
    pass

that's literally what happen: client.on_event_name = on_event_name

Then, when the event received, the dispatcher do, roughly say, next things:

  1. It says to a client, that we have event with name 'event_name' to dispatch
  2. Client looks at the name and does coro = getattr(self, 'on_' + 'event_name')
  3. Client runs coro with loop.create_task()

It's also noteworthy, that dispatcher calls now looks literally like I described above (self.dispatch('guild_available', guild)), so the 'guild_available' string is hardcoded, and there's no way to easily enumerate all of event types.

Additional Context

I have already written this Enum for myself, and tried to bring it into line with the events that come from Discord. I will be glad if it turns to be useful:

class EventType(Enum):
    # Guild:
    guild_update = 1
    guild_join = 2
    guild_available = 3
    guild_unavailable = 4
    guild_remove = 5
    guild_role_create = 6
    guild_role_update = 7
    guild_role_delete = 8
    guild_channel_create = 9
    guild_channel_update = 10
    guild_channel_delete = 11
    guild_channel_pins_update = 12
    thread_create = 13
    thread_update = 14
    thread_delete = 15
    raw_thread_delete = 16
    thread_remove = 17
    thread_member_join = 18
    thread_join = 19
    thread_member_remove = 20
    stage_instance_create = 21
    stage_instance_update = 22
    stage_instance_delete = 23

    # Guild Members:
    member_join = 24
    member_update = 25
    member_remove = 26

    # Guild member bans:
    member_ban = 27
    member_unban = 28

    # Guild emojis
    guild_emojis_update = 29
    guild_stickers_update = 30

    # Guild integrations:
    guild_integrations_update = 31
    integration_create = 32
    integration_update = 33
    raw_integration_delete = 34

    # Guild webhooks:
    webhooks_update = 35

    # Guild invites:
    invite_create = 36
    invite_delete = 37

    # Guild voice state:
    voice_state_update = 38

    # Guild presence:
    presence_update = 39

    # Guild and Direct messages:
    message = 40
    raw_message_delete = 41
    message_delete = 42
    raw_bulk_message_delete = 43
    bulk_message_delete = 44
    raw_message_edit = 45
    message_edit = 46

    # Private Channels:
    private_channel_update = 47
    private_channel_pins_update = 48

    # Guild and Direct message typing
    typing = 49

    # Guild and Direct message reactions:
    raw_reaction_add = 50
    reaction_add = 51
    raw_reaction_clear = 52
    reaction_clear = 53
    raw_reaction_remove = 54
    reaction_remove = 55
    raw_reaction_clear_emoji = 56
    reaction_clear_emoji = 57

    # Utility events:
    disconnect = 58
    ready = 59
    connect = 60
    resumed = 61
    shard_ready = 62

    # Interactions:
    interaction = 63
    command = 64
    command_completion = 65
    command_error = 66

    def __str__(self):
        return self.name

Dest0re avatar Sep 13 '21 04:09 Dest0re

Wait... why was this closed? The PR that's linked to this issue hasn't been merged yet

EmmmaTech avatar Jul 10 '22 04:07 EmmmaTech

Sawwy

Lulalaby avatar Jul 10 '22 04:07 Lulalaby

Not planned for 2.x.

Dorukyum avatar Dec 01 '23 09:12 Dorukyum