From 1aaa32d4bced65d346f0634984c95a707bf0c00b Mon Sep 17 00:00:00 2001 From: z03h <7235242+z03h@users.noreply.github.com> Date: Sun, 10 Jul 2022 01:00:26 -0700 Subject: [PATCH] Add on_raw_app_command_permissions_update event --- discord/raw_models.py | 35 +++++++++++++++++++++++++++++++++++ discord/state.py | 5 +++++ docs/api.rst | 22 +++++++++++++++++++++- 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/discord/raw_models.py b/discord/raw_models.py index bca0721aa..4c17a8df3 100644 --- a/discord/raw_models.py +++ b/discord/raw_models.py @@ -29,6 +29,7 @@ from typing import TYPE_CHECKING, Optional, Set, List, Tuple, Union from .enums import ChannelType, try_enum from .utils import _get_as_snowflake +from .app_commands import AppCommandPermissions if TYPE_CHECKING: from .types.gateway import ( @@ -46,11 +47,14 @@ if TYPE_CHECKING: TypingStartEvent, GuildMemberRemoveEvent, ) + from .types.command import GuildApplicationCommandPermissions from .message import Message from .partial_emoji import PartialEmoji from .member import Member from .threads import Thread from .user import User + from .state import ConnectionState + from .guild import Guild ReactionActionEvent = Union[MessageReactionAddEvent, MessageReactionRemoveEvent] @@ -68,6 +72,7 @@ __all__ = ( 'RawThreadMembersUpdate', 'RawTypingEvent', 'RawMemberRemoveEvent', + 'RawAppCommandPermissionsUpdateEvent', ) @@ -432,3 +437,33 @@ class RawMemberRemoveEvent(_RawReprMixin): def __init__(self, data: GuildMemberRemoveEvent, user: User, /) -> None: self.user: Union[User, Member] = user self.guild_id: int = int(data['guild_id']) + + +class RawAppCommandPermissionsUpdateEvent(_RawReprMixin): + """Represents the payload for a :func:`on_raw_app_command_permissions_update` event. + + .. versionadded:: 2.0 + + Attributes + ---------- + target_id: :class:`int` + The ID of the command or application whos permissions were updated. + When this is the application ID instead of a command ID, the permissions + apply to all commands that do not contain explicit overwrites. + application_id: :class:`int` + The ID of the application that the command belongs to. + guild: :class:`~discord.Guild` + The guild where the permissions were updated. + permissions: List[:class:`~discord.app_commands.AppCommandPermissions`] + List of new permissions for the app command. + """ + + __slots__ = ('target_id', 'application_id', 'guild', 'permissions') + + def __init__(self, *, data: GuildApplicationCommandPermissions, state: ConnectionState): + self.target_id: int = int(data['id']) + self.application_id: int = int(data['application_id']) + self.guild: Guild = state._get_or_create_unavailable_guild(int(data['guild_id'])) + self.permissions: List[AppCommandPermissions] = [ + AppCommandPermissions(data=perm, guild=self.guild, state=state) for perm in data['permissions'] + ] diff --git a/discord/state.py b/discord/state.py index e371d2a00..21e93439d 100644 --- a/discord/state.py +++ b/discord/state.py @@ -95,6 +95,7 @@ if TYPE_CHECKING: from .types.guild import Guild as GuildPayload from .types.message import Message as MessagePayload, PartialMessage as PartialMessagePayload from .types import gateway as gw + from .types.command import GuildApplicationCommandPermissions as GuildApplicationCommandPermissionsPayload T = TypeVar('T') Channel = Union[GuildChannel, VocalGuildChannel, PrivateChannel, PartialMessageable] @@ -1475,6 +1476,10 @@ class ConnectionState: else: _log.debug('SCHEDULED_EVENT_USER_REMOVE referencing unknown guild ID: %s. Discarding.', data['guild_id']) + def parse_application_command_permissions_update(self, data: GuildApplicationCommandPermissionsPayload): + raw = RawAppCommandPermissionsUpdateEvent(data=data, state=self) + self.dispatch('raw_app_command_permissions_update', raw) + def parse_voice_state_update(self, data: gw.VoiceStateUpdateEvent) -> None: guild = self._get_guild(utils._get_as_snowflake(data, 'guild_id')) channel_id = utils._get_as_snowflake(data, 'channel_id') diff --git a/docs/api.rst b/docs/api.rst index e08d12bac..e4f7c2fde 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -203,8 +203,20 @@ to handle it, which defaults to logging the traceback and ignoring the exception errors. In order to turn a function into a coroutine they must be ``async def`` functions. +App Commands +~~~~~~~~~~~~~ + +.. function:: on_raw_app_command_permissions_update(payload) + + Called when application command permissions are updated. + + .. versionadded:: 2.0 + + :param payload: The raw event payload data. + :type payload: :class:`RawAppCommandPermissionsUpdateEvent` + AutoMod -~~~~~~~ +~~~~~~~~ .. function:: on_automod_rule_create(rule) @@ -4417,6 +4429,14 @@ RawMemberRemoveEvent .. autoclass:: RawMemberRemoveEvent() :members: +RawAppCommandPermissionsUpdateEvent +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +.. attributetable:: RawAppCommandPermissionsUpdateEvent + +.. autoclass:: RawAppCommandPermissionsUpdateEvent() + :members: + PartialWebhookGuild ~~~~~~~~~~~~~~~~~~~~