Skip to content

Commit

Permalink
Fix channel deletion not removing associated threads
Browse files Browse the repository at this point in the history
  • Loading branch information
LostLuma committed May 5, 2024
1 parent 0b4263e commit 0ee1794
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
9 changes: 5 additions & 4 deletions discord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,10 +378,11 @@ def _remove_thread(self, thread: Snowflake, /) -> None:
def _clear_threads(self) -> None:
self._threads.clear()

def _remove_threads_by_channel(self, channel_id: int) -> None:
to_remove = [k for k, t in self._threads.items() if t.parent_id == channel_id]
for k in to_remove:
del self._threads[k]
def _remove_threads_by_channel(self, channel_id: int) -> List[Thread]:
to_remove = [t for t in self._threads.values() if t.parent_id == channel_id]
for thread in to_remove:
del self._threads[thread.id]
return to_remove

def _filter_threads(self, channel_ids: Set[int]) -> Dict[int, Thread]:
to_remove: Dict[int, Thread] = {k: t for k, t in self._threads.items() if t.parent_id in channel_ids}
Expand Down
16 changes: 16 additions & 0 deletions discord/raw_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
from .colour import Colour

if TYPE_CHECKING:
from typing_extensions import Self

from .types.gateway import (
MessageDeleteEvent,
MessageDeleteBulkEvent as BulkMessageDeleteEvent,
Expand Down Expand Up @@ -399,6 +401,20 @@ def __init__(self, data: ThreadDeleteEvent) -> None:
self.parent_id: int = int(data['parent_id'])
self.thread: Optional[Thread] = None

@classmethod
def _from_thread(cls, thread: Thread) -> Self:
data: ThreadDeleteEvent = {
'id': thread.id,
'type': thread.type.value,
'guild_id': thread.guild.id,
'parent_id': thread.parent_id,
}

instance = cls(data)
instance.thread = thread

return instance


class RawThreadMembersUpdate(_RawReprMixin):
"""Represents the payload for a :func:`on_raw_thread_member_remove` event.
Expand Down
6 changes: 6 additions & 0 deletions discord/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,6 +831,12 @@ def parse_channel_delete(self, data: gw.ChannelDeleteEvent) -> None:
guild._scheduled_events.pop(s.id)
self.dispatch('scheduled_event_delete', s)

threads = guild._remove_threads_by_channel(channel_id)

for thread in threads:
self.dispatch('thread_delete', thread)
self.dispatch('raw_thread_delete', RawThreadDeleteEvent._from_thread(thread))

def parse_channel_update(self, data: gw.ChannelUpdateEvent) -> None:
channel_type = try_enum(ChannelType, data.get('type'))
channel_id = int(data['id'])
Expand Down

0 comments on commit 0ee1794

Please sign in to comment.