Skip to content

Commit

Permalink
Adjust category and guild _channels attributes to work with NoneType …
Browse files Browse the repository at this point in the history
…positions (#1530)
  • Loading branch information
NeloBlivion authored Aug 5, 2022
1 parent 27c088b commit a56d801
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
10 changes: 5 additions & 5 deletions discord/channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -2094,7 +2094,7 @@ def channels(self) -> List[GuildChannelType]:
"""

def comparator(channel):
return not isinstance(channel, _TextChannel), channel.position
return not isinstance(channel, _TextChannel), (channel.position or -1)

ret = [c for c in self.guild.channels if c.category_id == self.id]
ret.sort(key=comparator)
Expand All @@ -2104,14 +2104,14 @@ def comparator(channel):
def text_channels(self) -> List[TextChannel]:
"""List[:class:`TextChannel`]: Returns the text channels that are under this category."""
ret = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, TextChannel)]
ret.sort(key=lambda c: (c.position, c.id))
ret.sort(key=lambda c: (c.position or -1, c.id))
return ret

@property
def voice_channels(self) -> List[VoiceChannel]:
"""List[:class:`VoiceChannel`]: Returns the voice channels that are under this category."""
ret = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, VoiceChannel)]
ret.sort(key=lambda c: (c.position, c.id))
ret.sort(key=lambda c: (c.position or -1, c.id))
return ret

@property
Expand All @@ -2121,7 +2121,7 @@ def stage_channels(self) -> List[StageChannel]:
.. versionadded:: 1.7
"""
ret = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, StageChannel)]
ret.sort(key=lambda c: (c.position, c.id))
ret.sort(key=lambda c: (c.position or -1, c.id))
return ret

@property
Expand All @@ -2131,7 +2131,7 @@ def forum_channels(self) -> List[ForumChannel]:
.. versionadded:: 2.0
"""
ret = [c for c in self.guild.channels if c.category_id == self.id and isinstance(c, ForumChannel)]
ret.sort(key=lambda c: (c.position, c.id))
ret.sort(key=lambda c: (c.position or -1, c.id))
return ret

async def create_text_channel(self, name: str, **options: Any) -> TextChannel:
Expand Down
14 changes: 7 additions & 7 deletions discord/guild.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ def voice_channels(self) -> List[VoiceChannel]:
This is sorted by the position and are in UI order from top to bottom.
"""
r = [ch for ch in self._channels.values() if isinstance(ch, VoiceChannel)]
r.sort(key=lambda c: (c.position, c.id))
r.sort(key=lambda c: (c.position or -1, c.id))
return r

@property
Expand All @@ -632,7 +632,7 @@ def stage_channels(self) -> List[StageChannel]:
This is sorted by the position and are in UI order from top to bottom.
"""
r = [ch for ch in self._channels.values() if isinstance(ch, StageChannel)]
r.sort(key=lambda c: (c.position, c.id))
r.sort(key=lambda c: (c.position or -1, c.id))
return r

@property
Expand All @@ -644,7 +644,7 @@ def forum_channels(self) -> List[ForumChannel]:
This is sorted by the position and are in UI order from top to bottom.
"""
r = [ch for ch in self._channels.values() if isinstance(ch, ForumChannel)]
r.sort(key=lambda c: (c.position, c.id))
r.sort(key=lambda c: (c.position or -1, c.id))
return r

@property
Expand All @@ -668,7 +668,7 @@ def text_channels(self) -> List[TextChannel]:
This is sorted by the position and are in UI order from top to bottom.
"""
r = [ch for ch in self._channels.values() if isinstance(ch, TextChannel)]
r.sort(key=lambda c: (c.position, c.id))
r.sort(key=lambda c: (c.position or -1, c.id))
return r

@property
Expand All @@ -678,7 +678,7 @@ def categories(self) -> List[CategoryChannel]:
This is sorted by the position and are in UI order from top to bottom.
"""
r = [ch for ch in self._channels.values() if isinstance(ch, CategoryChannel)]
r.sort(key=lambda c: (c.position, c.id))
r.sort(key=lambda c: (c.position or -1, c.id))
return r

def by_category(self) -> List[ByCategoryItem]:
Expand Down Expand Up @@ -707,13 +707,13 @@ def by_category(self) -> List[ByCategoryItem]:

def key(t: ByCategoryItem) -> Tuple[Tuple[int, int], List[GuildChannel]]:
k, v = t
return (k.position, k.id) if k else (-1, -1), v
return (k.position or -1, k.id) if k else (-1, -1), v

_get = self._channels.get
as_list: List[ByCategoryItem] = [(_get(k), v) for k, v in grouped.items()] # type: ignore
as_list.sort(key=key)
for _, channels in as_list:
channels.sort(key=lambda c: (c._sorting_bucket, c.position, c.id))
channels.sort(key=lambda c: (c._sorting_bucket, c.position or -1, c.id))
return as_list

def _resolve_channel(self, id: Optional[int], /) -> Optional[Union[GuildChannel, Thread]]:
Expand Down

0 comments on commit a56d801

Please sign in to comment.