Skip to content

Commit fe31e46

Browse files
Merge pull request #123 from nerdguyahmad/raw-thread-delete
Add Raw thread delete event
2 parents 049945c + f8d3153 commit fe31e46

File tree

4 files changed

+71
-4
lines changed

4 files changed

+71
-4
lines changed

discord/raw_models.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
from typing import TYPE_CHECKING, Optional, Set, List
2828

29+
from .enums import ChannelType, try_enum
30+
2931
if TYPE_CHECKING:
3032
from .types.raw_models import (
3133
MessageDeleteEvent,
@@ -34,11 +36,14 @@
3436
MessageUpdateEvent,
3537
ReactionClearEvent,
3638
ReactionClearEmojiEvent,
37-
IntegrationDeleteEvent
39+
IntegrationDeleteEvent,
40+
ThreadDeleteEvent,
3841
)
3942
from .message import Message
4043
from .partial_emoji import PartialEmoji
4144
from .member import Member
45+
from .threads import Thread
46+
4247

4348

4449
__all__ = (
@@ -49,6 +54,7 @@
4954
'RawReactionClearEvent',
5055
'RawReactionClearEmojiEvent',
5156
'RawIntegrationDeleteEvent',
57+
'RawThreadDeleteEvent',
5258
)
5359

5460

@@ -276,3 +282,33 @@ def __init__(self, data: IntegrationDeleteEvent) -> None:
276282
self.application_id: Optional[int] = int(data['application_id'])
277283
except KeyError:
278284
self.application_id: Optional[int] = None
285+
286+
class RawThreadDeleteEvent(_RawReprMixin):
287+
"""Represents the payload for :func:`on_raw_thread_delete` event.
288+
289+
.. versionadded:: 2.0
290+
291+
Attributes
292+
----------
293+
294+
thread_id: :class:`int`
295+
The ID of the thread that was deleted.
296+
thread_type: :class:`discord.ChannelType`
297+
The channel type of the deleted thread.
298+
guild_id: :class:`int`
299+
The ID of the guild the deleted thread belonged to.
300+
parent_id: :class:`int`
301+
The ID of the channel the thread belonged to.
302+
thread: Optional[:class:`discord.Thread`]
303+
The thread that was deleted. This may be ``None`` if deleted thread is not found in internal cache.
304+
"""
305+
__slots__ = ('thread_id', 'thread_type', 'guild_id', 'parent_id', 'thread')
306+
307+
def __init__(self, data: ThreadDeleteEvent) -> None:
308+
self.thread_id: int = int(data['id'])
309+
self.thread_type: ChannelType = try_enum(ChannelType, int(data['type']))
310+
self.guild_id: int = int(data['guild_id'])
311+
self.parent_id: int = int(data['parent_id'])
312+
self.thread: Optional[Thread] = None
313+
314+

discord/state.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,12 +850,18 @@ def parse_thread_update(self, data) -> None:
850850
def parse_thread_delete(self, data) -> None:
851851
guild_id = int(data['guild_id'])
852852
guild = self._get_guild(guild_id)
853+
853854
if guild is None:
854855
_log.debug('THREAD_DELETE referencing an unknown guild ID: %s. Discarding', guild_id)
855856
return
856857

857-
thread_id = int(data['id'])
858-
thread = guild.get_thread(thread_id)
858+
raw = RawThreadDeleteEvent(data)
859+
thread = guild.get_thread(raw.thread_id)
860+
raw.thread = thread
861+
862+
self.dispatch('raw_thread_delete', raw)
863+
864+
859865
if thread is not None:
860866
guild._remove_thread(thread) # type: ignore
861867
self.dispatch('thread_delete', thread)

discord/types/raw_models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,10 @@ class _IntegrationDeleteEventOptional(TypedDict, total=False):
8585
class IntegrationDeleteEvent(_IntegrationDeleteEventOptional):
8686
id: Snowflake
8787
guild_id: Snowflake
88+
89+
class ThreadDeleteEvent(TypedDict, total=False):
90+
thread_id: Snowflake
91+
thread_type: int
92+
guild_id: Snowflake
93+
parent_id: Snowflake
94+

docs/api.rst

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -759,7 +759,9 @@ to handle it, which defaults to print a traceback and ignoring the exception.
759759

760760
.. function:: on_thread_delete(thread)
761761

762-
Called whenever a thread is deleted.
762+
Called whenever a thread is deleted. If the deleted thread isn't found in internal cache
763+
then this will not be called. Archived threads are not in the cache. Consider using :func:`on_raw_thread_delete`
764+
763765

764766
Note that you can get the guild from :attr:`Thread.guild`.
765767

@@ -770,6 +772,14 @@ to handle it, which defaults to print a traceback and ignoring the exception.
770772
:param thread: The thread that got deleted.
771773
:type thread: :class:`Thread`
772774

775+
.. function:: on_raw_thread_delete(payload)
776+
777+
Called whenever a thread is deleted. Unlike :func:`on_thread_delete` this is called
778+
regardless of the state of the internal cache.
779+
780+
:param payload: The raw event payload data.
781+
:type payload: :class:`RawThreadDeleteEvent`
782+
773783
.. function:: on_thread_member_join(member)
774784
on_thread_member_remove(member)
775785

@@ -3943,6 +3953,14 @@ RawIntegrationDeleteEvent
39433953
.. autoclass:: RawIntegrationDeleteEvent()
39443954
:members:
39453955

3956+
RawThreadDeleteEvent
3957+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3958+
3959+
.. attributetable:: RawThreadDeleteEvent
3960+
3961+
.. autoclass:: RawThreadDeleteEvent()
3962+
:members:
3963+
39463964
PartialWebhookGuild
39473965
~~~~~~~~~~~~~~~~~~~~
39483966

0 commit comments

Comments
 (0)