Skip to content

Commit 160830e

Browse files
committed
feat: more thread events
1 parent 79883a0 commit 160830e

File tree

2 files changed

+61
-52
lines changed

2 files changed

+61
-52
lines changed

discord/app/state.py

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -519,57 +519,6 @@ async def query_members(
519519
)
520520
raise
521521

522-
523-
def parse_thread_update(self, data) -> None:
524-
guild_id = int(data["guild_id"])
525-
guild = self._get_guild(guild_id)
526-
raw = RawThreadUpdateEvent(data)
527-
if guild is None:
528-
_log.debug(
529-
"THREAD_UPDATE referencing an unknown guild ID: %s. Discarding",
530-
guild_id,
531-
)
532-
return
533-
else:
534-
thread = guild.get_thread(raw.thread_id)
535-
if thread is not None:
536-
old = copy.copy(thread)
537-
thread._update(data)
538-
if thread.archived:
539-
guild._remove_thread(thread)
540-
self.dispatch("thread_update", old, thread)
541-
else:
542-
thread = Thread(guild=guild, state=guild._state, data=data)
543-
if not thread.archived:
544-
guild._add_thread(thread)
545-
self.dispatch("thread_join", thread)
546-
raw.thread = thread
547-
self.dispatch("raw_thread_update", raw)
548-
549-
def parse_thread_delete(self, data) -> None:
550-
guild_id = int(data["guild_id"])
551-
guild = self._get_guild(guild_id)
552-
553-
if guild is None:
554-
_log.debug(
555-
"THREAD_DELETE referencing an unknown guild ID: %s. Discarding",
556-
guild_id,
557-
)
558-
return
559-
560-
raw = RawThreadDeleteEvent(data)
561-
thread = guild.get_thread(raw.thread_id)
562-
raw.thread = thread
563-
564-
self.dispatch("raw_thread_delete", raw)
565-
566-
if thread is not None:
567-
guild._remove_thread(thread) # type: ignore
568-
self.dispatch("thread_delete", thread)
569-
570-
if (msg := thread.starting_message) is not None:
571-
msg.thread = None
572-
573522
def parse_thread_list_sync(self, data) -> None:
574523
guild_id = int(data["guild_id"])
575524
guild: Guild | None = self._get_guild(guild_id)

discord/events/thread.py

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
from typing import Any, Self
1+
from typing import Any, Self, cast
22
from discord import utils
3+
from discord.abc import Snowflake
34
from discord.app.event_emitter import Event
45
from discord.app.state import ConnectionState
6+
from discord.raw_models import RawThreadDeleteEvent, RawThreadUpdateEvent
57
from discord.threads import Thread, ThreadMember
8+
from discord.types.raw_models import ThreadDeleteEvent, ThreadUpdateEvent
69

710

811
class ThreadCreate(Event, Thread):
@@ -40,7 +43,64 @@ async def __load__(cls, data: dict[str, Any], state: ConnectionState) -> Self |
4043
)
4144
)
4245
self.just_joined = False
46+
self.__dict__.update(thread.__dict__)
4347
else:
48+
self.__dict__.update(cached_thread.__dict__)
4449
self.just_joined = True
4550

4651
return self
52+
53+
class ThreadUpdate(Event, Thread):
54+
__event_name__ = "THREAD_UPDATE"
55+
56+
def __init__(self) -> None:
57+
...
58+
59+
old: Thread
60+
61+
@classmethod
62+
async def __load__(cls, data: ThreadUpdateEvent, state: ConnectionState) -> Self | None:
63+
guild_id = int(data["guild_id"])
64+
guild = await state._get_guild(guild_id)
65+
raw = RawThreadUpdateEvent(data)
66+
if guild is None:
67+
return
68+
69+
self = cls()
70+
71+
thread = guild.get_thread(raw.thread_id)
72+
if thread:
73+
self.old = thread
74+
await thread._update(thread)
75+
if thread.archived:
76+
guild._remove_thread(cast(Snowflake, raw.thread_id))
77+
else:
78+
thread = Thread(guild=guild, state=guild._state, data=data) # type: ignore
79+
if not thread.archived:
80+
guild._add_thread(thread)
81+
82+
self.__dict__.update(thread.__dict__)
83+
return self
84+
85+
class ThreadDelete(Event, Thread):
86+
__event_name__ = "THREAD_DELETE"
87+
88+
def __init__(self) -> None:
89+
...
90+
91+
@classmethod
92+
async def __load__(cls, data: ThreadDeleteEvent, state: ConnectionState) -> Self | None:
93+
raw = RawThreadDeleteEvent(data)
94+
guild = await state._get_guild(raw.guild_id)
95+
if guild is None:
96+
return
97+
98+
self = cls()
99+
100+
thread = guild.get_thread(raw.thread_id)
101+
if thread:
102+
guild._remove_thread(cast(Snowflake, thread.id))
103+
if (msg := await thread.get_starting_message()) is not None:
104+
msg.thread = None # type: ignore
105+
106+
return cast(Self, thread)

0 commit comments

Comments
 (0)