Skip to content

Commit

Permalink
refactor: move event data to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
shiftinv committed Apr 30, 2023
1 parent ee43582 commit 52d7b95
Show file tree
Hide file tree
Showing 2 changed files with 158 additions and 149 deletions.
154 changes: 154 additions & 0 deletions disnake/_event_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# SPDX-License-Identifier: MIT

from __future__ import annotations

from typing import Dict, List, Tuple

from .enums import Event


class EventData:
type_args: Tuple[str, ...]
"""Type names of event arguments, e.g. `("Guild", "User")`"""

bot: bool
"""Whether the event is specific to ext.commands"""

event_only: bool
"""Whether the event can only be used through `@event` and not other listeners"""

def __init__(self, type_args: List[str], bot: bool = False, event_only: bool = False) -> None:
self.type_args = tuple(type_args)
self.bot = bot
self.event_only = event_only


EVENT_DATA: Dict[Event, EventData] = {
Event.connect: EventData([]),
Event.disconnect: EventData([]),
# FIXME: figure out how to specify varargs for these two if we ever add overloads for @event
Event.error: EventData([], event_only=True),
Event.gateway_error: EventData([], event_only=True),
Event.ready: EventData([]),
Event.resumed: EventData([]),
Event.shard_connect: EventData(["int"]),
Event.shard_disconnect: EventData(["int"]),
Event.shard_ready: EventData(["int"]),
Event.shard_resumed: EventData(["int"]),
Event.socket_event_type: EventData(["str"]),
Event.socket_raw_receive: EventData(["str"]),
Event.socket_raw_send: EventData(["Union[str, bytes]"]),
Event.guild_channel_create: EventData(["GuildChannel"]),
Event.guild_channel_update: EventData(["GuildChannel", "GuildChannel"]),
Event.guild_channel_delete: EventData(["GuildChannel"]),
Event.guild_channel_pins_update: EventData(
["Union[GuildChannel, Thread]", "Optional[datetime]"]
),
Event.invite_create: EventData(["Invite"]),
Event.invite_delete: EventData(["Invite"]),
Event.private_channel_update: EventData(["GroupChannel", "GroupChannel"]),
Event.private_channel_pins_update: EventData(["PrivateChannel", "Optional[datetime]"]),
Event.webhooks_update: EventData(["GuildChannel"]),
Event.thread_create: EventData(["Thread"]),
Event.thread_update: EventData(["Thread", "Thread"]),
Event.thread_delete: EventData(["Thread"]),
Event.thread_join: EventData(["Thread"]),
Event.thread_remove: EventData(["Thread"]),
Event.thread_member_join: EventData(["ThreadMember"]),
Event.thread_member_remove: EventData(["ThreadMember"]),
Event.raw_thread_member_remove: EventData(["RawThreadMemberRemoveEvent"]),
Event.raw_thread_update: EventData(["Thread"]),
Event.raw_thread_delete: EventData(["RawThreadDeleteEvent"]),
Event.guild_join: EventData(["Guild"]),
Event.guild_remove: EventData(["Guild"]),
Event.guild_update: EventData(["Guild", "Guild"]),
Event.guild_available: EventData(["Guild"]),
Event.guild_unavailable: EventData(["Guild"]),
Event.guild_role_create: EventData(["Role"]),
Event.guild_role_delete: EventData(["Role"]),
Event.guild_role_update: EventData(["Role", "Role"]),
Event.guild_emojis_update: EventData(["Guild", "Sequence[Emoji]", "Sequence[Emoji]"]),
Event.guild_stickers_update: EventData(
["Guild", "Sequence[GuildSticker]", "Sequence[GuildSticker]"]
),
Event.guild_integrations_update: EventData(["Guild"]),
Event.guild_scheduled_event_create: EventData(["GuildScheduledEvent"]),
Event.guild_scheduled_event_update: EventData(["GuildScheduledEvent", "GuildScheduledEvent"]),
Event.guild_scheduled_event_delete: EventData(["GuildScheduledEvent"]),
Event.guild_scheduled_event_subscribe: EventData(
["GuildScheduledEvent", "Union[Member, User]"]
),
Event.guild_scheduled_event_unsubscribe: EventData(
["GuildScheduledEvent", "Union[Member, User]"]
),
Event.raw_guild_scheduled_event_subscribe: EventData(["RawGuildScheduledEventUserActionEvent"]),
Event.raw_guild_scheduled_event_unsubscribe: EventData(
["RawGuildScheduledEventUserActionEvent"]
),
Event.application_command_permissions_update: EventData(["GuildApplicationCommandPermissions"]),
Event.automod_action_execution: EventData(["AutoModActionExecution"]),
Event.automod_rule_create: EventData(["AutoModRule"]),
Event.automod_rule_update: EventData(["AutoModRule"]),
Event.automod_rule_delete: EventData(["AutoModRule"]),
Event.audit_log_entry_create: EventData(["AuditLogEntry"]),
Event.integration_create: EventData(["Integration"]),
Event.integration_update: EventData(["Integration"]),
Event.raw_integration_delete: EventData(["RawIntegrationDeleteEvent"]),
Event.member_join: EventData(["Member"]),
Event.member_remove: EventData(["Member"]),
Event.member_update: EventData(["Member", "Member"]),
Event.raw_member_remove: EventData(["RawGuildMemberRemoveEvent"]),
Event.raw_member_update: EventData(["Member"]),
Event.member_ban: EventData(["Guild", "Union[User, Member]"]),
Event.member_unban: EventData(["Guild", "User"]),
Event.presence_update: EventData(["Member", "Member"]),
Event.user_update: EventData(["User", "User"]),
Event.voice_state_update: EventData(["Member", "VoiceState", "VoiceState"]),
Event.stage_instance_create: EventData(["StageInstance"]),
Event.stage_instance_delete: EventData(["StageInstance", "StageInstance"]),
Event.stage_instance_update: EventData(["StageInstance"]),
Event.application_command: EventData(["ApplicationCommandInteraction"]),
Event.application_command_autocomplete: EventData(["ApplicationCommandInteraction"]),
Event.button_click: EventData(["MessageInteraction"]),
Event.dropdown: EventData(["MessageInteraction"]),
Event.interaction: EventData(["Interaction"]),
Event.message_interaction: EventData(["MessageInteraction"]),
Event.modal_submit: EventData(["ModalInteraction"]),
Event.message: EventData(["Message"]),
Event.message_edit: EventData(["Message", "Message"]),
Event.message_delete: EventData(["Message"]),
Event.bulk_message_delete: EventData(["List[Message]"]),
Event.raw_message_edit: EventData(["RawMessageUpdateEvent"]),
Event.raw_message_delete: EventData(["RawMessageDeleteEvent"]),
Event.raw_bulk_message_delete: EventData(["RawBulkMessageDeleteEvent"]),
Event.reaction_add: EventData(["Reaction", "Union[Member, User]"]),
Event.reaction_remove: EventData(["Reaction", "Union[Member, User]"]),
Event.reaction_clear: EventData(["Message", "List[Reaction]"]),
Event.reaction_clear_emoji: EventData(["Reaction"]),
Event.raw_reaction_add: EventData(["RawReactionActionEvent"]),
Event.raw_reaction_remove: EventData(["RawReactionActionEvent"]),
Event.raw_reaction_clear: EventData(["RawReactionClearEvent"]),
Event.raw_reaction_clear_emoji: EventData(["RawReactionClearEmojiEvent"]),
Event.typing: EventData(
["Union[Messageable, ForumChannel]", "Union[User, Member]", "datetime"]
),
Event.raw_typing: EventData(["RawTypingEvent"]),
Event.command: EventData(["commands.Context"], bot=True),
Event.command_completion: EventData(["commands.Context"], bot=True),
Event.command_error: EventData(["commands.Context", "commands.CommandError"], bot=True),
Event.slash_command: EventData(["ApplicationCommandInteraction"], bot=True),
Event.slash_command_completion: EventData(["ApplicationCommandInteraction"], bot=True),
Event.slash_command_error: EventData(
["ApplicationCommandInteraction", "commands.CommandError"], bot=True
),
Event.user_command: EventData(["ApplicationCommandInteraction"], bot=True),
Event.user_command_completion: EventData(["ApplicationCommandInteraction"], bot=True),
Event.user_command_error: EventData(
["ApplicationCommandInteraction", "commands.CommandError"], bot=True
),
Event.message_command: EventData(["ApplicationCommandInteraction"], bot=True),
Event.message_command_completion: EventData(["ApplicationCommandInteraction"], bot=True),
Event.message_command_error: EventData(
["ApplicationCommandInteraction", "commands.CommandError"], bot=True
),
}
153 changes: 4 additions & 149 deletions scripts/codemods/typed_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
from __future__ import annotations

import types
from dataclasses import dataclass
from typing import Dict, List, Optional, Tuple, cast
from typing import List, Optional, cast

import libcst as cst
import libcst.matchers as m
from libcst import codemod

from disnake import Event
from disnake._event_data import EVENT_DATA, EventData


def get_param(func: cst.FunctionDef, name: str) -> cst.Param:
Expand Down Expand Up @@ -85,14 +85,14 @@ def create_literal(self, event: Event) -> cst.BaseExpression:

def create_args_list(self, event_data: EventData) -> cst.BaseExpression:
return cst.parse_expression(
f'[{",".join(event_data.args)}]',
f'[{",".join(event_data.type_args)}]',
config=self.module.config_for_parsing,
)

def generate_wait_for_overload(
self, func: cst.FunctionDef, event: Event, event_data: EventData
) -> cst.FunctionDef:
args = event_data.args
args = event_data.type_args

new_overload = self.create_empty_overload(func)

Expand Down Expand Up @@ -133,148 +133,3 @@ def generate_wait_for_overload(
)

return new_overload


@dataclass
class EventData:
# type names of event arguments, e.g. `("Guild", "User")`
args: Tuple[str, ...]
# whether the event is specific to ext.commands
bot: bool = False
# whether the event can only be used through `@event` and not other listeners
event_only: bool = False


EVENT_DATA: Dict[Event, EventData] = {
Event.connect: EventData(()),
Event.disconnect: EventData(()),
# FIXME: figure out how to specify varargs for these two if we ever add overloads for @event
Event.error: EventData((), event_only=True),
Event.gateway_error: EventData((), event_only=True),
Event.ready: EventData(()),
Event.resumed: EventData(()),
Event.shard_connect: EventData(("int",)),
Event.shard_disconnect: EventData(("int",)),
Event.shard_ready: EventData(("int",)),
Event.shard_resumed: EventData(("int",)),
Event.socket_event_type: EventData(("str",)),
Event.socket_raw_receive: EventData(("str",)),
Event.socket_raw_send: EventData(("Union[str, bytes]",)),
Event.guild_channel_create: EventData(("GuildChannel",)),
Event.guild_channel_update: EventData(("GuildChannel", "GuildChannel")),
Event.guild_channel_delete: EventData(("GuildChannel",)),
Event.guild_channel_pins_update: EventData(
("Union[GuildChannel, Thread]", "Optional[datetime]")
),
Event.invite_create: EventData(("Invite",)),
Event.invite_delete: EventData(("Invite",)),
Event.private_channel_update: EventData(("GroupChannel", "GroupChannel")),
Event.private_channel_pins_update: EventData(("PrivateChannel", "Optional[datetime]")),
Event.webhooks_update: EventData(("GuildChannel",)),
Event.thread_create: EventData(("Thread",)),
Event.thread_update: EventData(("Thread", "Thread")),
Event.thread_delete: EventData(("Thread",)),
Event.thread_join: EventData(("Thread",)),
Event.thread_remove: EventData(("Thread",)),
Event.thread_member_join: EventData(("ThreadMember",)),
Event.thread_member_remove: EventData(("ThreadMember",)),
Event.raw_thread_member_remove: EventData(("RawThreadMemberRemoveEvent",)),
Event.raw_thread_update: EventData(("Thread",)),
Event.raw_thread_delete: EventData(("RawThreadDeleteEvent",)),
Event.guild_join: EventData(("Guild",)),
Event.guild_remove: EventData(("Guild",)),
Event.guild_update: EventData(("Guild", "Guild")),
Event.guild_available: EventData(("Guild",)),
Event.guild_unavailable: EventData(("Guild",)),
Event.guild_role_create: EventData(("Role",)),
Event.guild_role_delete: EventData(("Role",)),
Event.guild_role_update: EventData(("Role", "Role")),
Event.guild_emojis_update: EventData(("Guild", "Sequence[Emoji]", "Sequence[Emoji]")),
Event.guild_stickers_update: EventData(
("Guild", "Sequence[GuildSticker]", "Sequence[GuildSticker]")
),
Event.guild_integrations_update: EventData(("Guild",)),
Event.guild_scheduled_event_create: EventData(("GuildScheduledEvent",)),
Event.guild_scheduled_event_update: EventData(("GuildScheduledEvent", "GuildScheduledEvent")),
Event.guild_scheduled_event_delete: EventData(("GuildScheduledEvent",)),
Event.guild_scheduled_event_subscribe: EventData(
("GuildScheduledEvent", "Union[Member, User]")
),
Event.guild_scheduled_event_unsubscribe: EventData(
("GuildScheduledEvent", "Union[Member, User]")
),
Event.raw_guild_scheduled_event_subscribe: EventData(
("RawGuildScheduledEventUserActionEvent",)
),
Event.raw_guild_scheduled_event_unsubscribe: EventData(
("RawGuildScheduledEventUserActionEvent",)
),
Event.application_command_permissions_update: EventData(
("GuildApplicationCommandPermissions",)
),
Event.automod_action_execution: EventData(("AutoModActionExecution",)),
Event.automod_rule_create: EventData(("AutoModRule",)),
Event.automod_rule_update: EventData(("AutoModRule",)),
Event.automod_rule_delete: EventData(("AutoModRule",)),
Event.audit_log_entry_create: EventData(("AuditLogEntry",)),
Event.integration_create: EventData(("Integration",)),
Event.integration_update: EventData(("Integration",)),
Event.raw_integration_delete: EventData(("RawIntegrationDeleteEvent",)),
Event.member_join: EventData(("Member",)),
Event.member_remove: EventData(("Member",)),
Event.member_update: EventData(("Member", "Member")),
Event.raw_member_remove: EventData(("RawGuildMemberRemoveEvent",)),
Event.raw_member_update: EventData(("Member",)),
Event.member_ban: EventData(("Guild", "Union[User, Member]")),
Event.member_unban: EventData(("Guild", "User")),
Event.presence_update: EventData(("Member", "Member")),
Event.user_update: EventData(("User", "User")),
Event.voice_state_update: EventData(("Member", "VoiceState", "VoiceState")),
Event.stage_instance_create: EventData(("StageInstance",)),
Event.stage_instance_delete: EventData(("StageInstance", "StageInstance")),
Event.stage_instance_update: EventData(("StageInstance",)),
Event.application_command: EventData(("ApplicationCommandInteraction",)),
Event.application_command_autocomplete: EventData(("ApplicationCommandInteraction",)),
Event.button_click: EventData(("MessageInteraction",)),
Event.dropdown: EventData(("MessageInteraction",)),
Event.interaction: EventData(("Interaction",)),
Event.message_interaction: EventData(("MessageInteraction",)),
Event.modal_submit: EventData(("ModalInteraction",)),
Event.message: EventData(("Message",)),
Event.message_edit: EventData(("Message", "Message")),
Event.message_delete: EventData(("Message",)),
Event.bulk_message_delete: EventData(("List[Message]",)),
Event.raw_message_edit: EventData(("RawMessageUpdateEvent",)),
Event.raw_message_delete: EventData(("RawMessageDeleteEvent",)),
Event.raw_bulk_message_delete: EventData(("RawBulkMessageDeleteEvent",)),
Event.reaction_add: EventData(("Reaction", "Union[Member, User]")),
Event.reaction_remove: EventData(("Reaction", "Union[Member, User]")),
Event.reaction_clear: EventData(("Message", "List[Reaction]")),
Event.reaction_clear_emoji: EventData(("Reaction",)),
Event.raw_reaction_add: EventData(("RawReactionActionEvent",)),
Event.raw_reaction_remove: EventData(("RawReactionActionEvent",)),
Event.raw_reaction_clear: EventData(("RawReactionClearEvent",)),
Event.raw_reaction_clear_emoji: EventData(("RawReactionClearEmojiEvent",)),
Event.typing: EventData(
("Union[Messageable, ForumChannel]", "Union[User, Member]", "datetime")
),
Event.raw_typing: EventData(("RawTypingEvent",)),
Event.command: EventData(("commands.Context",), bot=True),
Event.command_completion: EventData(("commands.Context",), bot=True),
Event.command_error: EventData(("commands.Context", "commands.CommandError"), bot=True),
Event.slash_command: EventData(("ApplicationCommandInteraction",), bot=True),
Event.slash_command_completion: EventData(("ApplicationCommandInteraction",), bot=True),
Event.slash_command_error: EventData(
("ApplicationCommandInteraction", "commands.CommandError"), bot=True
),
Event.user_command: EventData(("ApplicationCommandInteraction",), bot=True),
Event.user_command_completion: EventData(("ApplicationCommandInteraction",), bot=True),
Event.user_command_error: EventData(
("ApplicationCommandInteraction", "commands.CommandError"), bot=True
),
Event.message_command: EventData(("ApplicationCommandInteraction",), bot=True),
Event.message_command_completion: EventData(("ApplicationCommandInteraction",), bot=True),
Event.message_command_error: EventData(
("ApplicationCommandInteraction", "commands.CommandError"), bot=True
),
}

0 comments on commit 52d7b95

Please sign in to comment.