Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust category and guild _channels attributes to work with NoneType positions #1530

Merged
merged 3 commits into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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