Adjust category and guild _channels attributes to work with NoneType positions #1530
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
Adapts guild/category _channels attributes (text_channels, voice_channels etc.) to work correctly with Optional positions.
Currently when grabbing a list of a guild's or a category's channels, the library attempts to sort them by position to output them in their UI order:
This worked fine when we just had TextChannel and VoiceChannel because
position
underabc.GuildChannel
was required, but since 2.0 with new channel types and interaction payloadsposition
has been made optional for all channel types; sinceposition
can now be None, this sort breaks withTypeError: '<' not supported between instances of 'NoneType' and 'int'
.The solution i've applied is to assume the channel's position is -1 if
position = None
, so it will order as much as possible; other solutions could be to try-except, completely give up on sorting, or to make -1 the default instead of None (achieving the same result, but perhaps with a knock-on effect elsewhere in the library).I went with -1 since this was actually already present in
Guild.by_category()
, where it's assumed ID and position are -1 if it runs into a NoneType category. Alternatively, it could be a large number to assume the position is last instead of first.Information
Checklist
type: ignore
comments were used, a comment is also left explaining why.