diff --git a/discord/channel.py b/discord/channel.py index dc4f88e45d..b1fac739cb 100644 --- a/discord/channel.py +++ b/discord/channel.py @@ -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) @@ -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 @@ -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 @@ -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: diff --git a/discord/guild.py b/discord/guild.py index 9177553174..301bb1db5f 100644 --- a/discord/guild.py +++ b/discord/guild.py @@ -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 @@ -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 @@ -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 @@ -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 @@ -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]: @@ -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]]: