From 3e8fa5a6617913e6ad6a1d9f75d5e2c7731f8804 Mon Sep 17 00:00:00 2001 From: Lilly Rose Berner Date: Fri, 22 Apr 2022 06:42:39 +0200 Subject: [PATCH] Add raw thread delete event --- discord/raw_models.py | 34 ++++++++++++++++++++++++++++++++++ discord/state.py | 6 ++++-- docs/api.rst | 26 +++++++++++++++++++++++++- 3 files changed, 63 insertions(+), 3 deletions(-) diff --git a/discord/raw_models.py b/discord/raw_models.py index a8cd8370f..a25a047ba 100644 --- a/discord/raw_models.py +++ b/discord/raw_models.py @@ -26,6 +26,8 @@ from __future__ import annotations from typing import TYPE_CHECKING, Optional, Set, List, Tuple, Union +from .enums import ChannelType, try_enum + if TYPE_CHECKING: from .types.gateway import ( MessageDeleteEvent, @@ -36,10 +38,12 @@ if TYPE_CHECKING: MessageReactionRemoveEmojiEvent as ReactionClearEmojiEvent, MessageUpdateEvent, IntegrationDeleteEvent, + ThreadDeleteEvent, ) from .message import Message from .partial_emoji import PartialEmoji from .member import Member + from .threads import Thread ReactionActionEvent = Union[MessageReactionAddEvent, MessageReactionRemoveEvent] @@ -52,6 +56,7 @@ __all__ = ( 'RawReactionClearEvent', 'RawReactionClearEmojiEvent', 'RawIntegrationDeleteEvent', + 'RawThreadDeleteEvent', ) @@ -280,3 +285,32 @@ class RawIntegrationDeleteEvent(_RawReprMixin): self.application_id: Optional[int] = int(data['application_id']) except KeyError: self.application_id: Optional[int] = None + + +class RawThreadDeleteEvent(_RawReprMixin): + """Represents the payload for a :func:`on_raw_thread_delete` event. + + .. versionadded:: 2.0 + + Attributes + ---------- + thread_id: :class:`int` + The ID of the thread that was deleted. + thread_type: :class:`discord.ChannelType` + The channel type of the deleted thread. + guild_id: :class:`int` + The ID of the guild the thread was deleted in. + parent_id: :class:`int` + The ID of the channel the thread belonged to. + thread: Optional[:class:`discord.Thread`] + The thread, if it could be found in the internal cache. + """ + + __slots__ = ('thread_id', 'thread_type', 'parent_id', 'guild_id', 'thread') + + def __init__(self, data: ThreadDeleteEvent) -> None: + self.thread_id: int = int(data['id']) + self.thread_type: ChannelType = try_enum(ChannelType, data['type']) + self.guild_id: int = int(data['guild_id']) + self.parent_id: int = int(data['parent_id']) + self.thread: Optional[Thread] = None diff --git a/discord/state.py b/discord/state.py index 3ec0d93f2..087c38fac 100644 --- a/discord/state.py +++ b/discord/state.py @@ -886,8 +886,10 @@ class ConnectionState: _log.debug('THREAD_DELETE referencing an unknown guild ID: %s. Discarding', guild_id) return - thread_id = int(data['id']) - thread = guild.get_thread(thread_id) + raw = RawThreadDeleteEvent(data) + raw.thread = thread = guild.get_thread(raw.thread_id) + self.dispatch('raw_thread_delete', raw) + if thread is not None: guild._remove_thread(thread) self.dispatch('thread_delete', thread) diff --git a/docs/api.rst b/docs/api.rst index 85c69f15e..71443c34c 100644 --- a/docs/api.rst +++ b/docs/api.rst @@ -1157,7 +1157,11 @@ Threads .. function:: on_thread_delete(thread) - Called whenever a thread is deleted. + Called whenever a thread is deleted. If the thread could + not be found in the internal cache this event will not be called. + Threads will not be in the cache if they are archived. + + If you need this information use :func:`on_raw_thread_delete` instead. Note that you can get the guild from :attr:`Thread.guild`. @@ -1168,6 +1172,18 @@ Threads :param thread: The thread that got deleted. :type thread: :class:`Thread` +.. function:: on_raw_thread_delete(payload) + + Called whenever a thread is deleted. Unlike :func:`on_thread_delete` this + is called regardless of the thread being in the internal thread cache or not. + + This requires :attr:`Intents.guilds` to be enabled. + + .. versionadded:: 2.0 + + :param payload: The raw event payload data. + :type payload: :class:`RawThreadDeleteEvent` + .. function:: on_thread_member_join(member) on_thread_member_remove(member) @@ -3991,6 +4007,14 @@ RawIntegrationDeleteEvent .. autoclass:: RawIntegrationDeleteEvent() :members: +RawThreadDeleteEvent +~~~~~~~~~~~~~~~~~~~~~~ + +.. attributetable:: RawThreadDeleteEvent + +.. autoclass:: RawThreadDeleteEvent() + :members: + PartialWebhookGuild ~~~~~~~~~~~~~~~~~~~~