aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar MarkKoz <[email protected]>2020-03-29 17:00:45 -0700
committerGravatar MarkKoz <[email protected]>2020-03-29 17:00:45 -0700
commit28e9c74a57dbfac8049c90e7d128a041cbadedde (patch)
treed5cc31f816a34d18cd7efbee11037737504f3ca5
parentConstants: add a config value to toggle help channels extension (diff)
HelpChannels: fix alphabetical sorting of dormant channels
When a channel is moved, all channels below have their positions incremented by 1. This threw off the previous implementation which relied on position numbers being static. Co-authored-by: Sebastiaan Zeeff <[email protected]>
Diffstat (limited to '')
-rw-r--r--bot/cogs/help_channels.py35
1 files changed, 22 insertions, 13 deletions
diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py
index 69af085ee..10b17cdb8 100644
--- a/bot/cogs/help_channels.py
+++ b/bot/cogs/help_channels.py
@@ -1,4 +1,5 @@
import asyncio
+import bisect
import inspect
import itertools
import json
@@ -218,22 +219,30 @@ class HelpChannels(Scheduler, commands.Cog):
return channel
- def get_alphabetical_position(self, channel: discord.TextChannel) -> t.Optional[int]:
- """
- Return the position to move `channel` to so alphabetic order is maintained.
-
- If the channel does not have a valid name with a chemical element, return None.
- """
+ @staticmethod
+ def get_position(channel: discord.TextChannel, destination: discord.CategoryChannel) -> int:
+ """Return alphabetical position for `channel` if moved to `destination`."""
log.trace(f"Getting alphabetical position for #{channel} ({channel.id}).")
- try:
- position = self.name_positions[channel.name]
- except KeyError:
- log.warning(f"Channel #{channel} ({channel.id}) doesn't have a valid name.")
- position = None
+ # If the destination category is empty, use the first position
+ if not destination.channels:
+ position = 1
+ else:
+ # Make a sorted list of channel names for bisect.
+ channel_names = [c.name for c in destination.channels]
+
+ # Get location which would maintain sorted order if channel was inserted into the list.
+ rank = bisect.bisect(channel_names, channel.name)
+
+ if rank == len(destination.channels):
+ # Channel should be moved to the end of the category.
+ position = destination.channels[-1].position + 1
+ else:
+ # Channel should be moved to the position of its alphabetical successor.
+ position = destination.channels[rank].position
log.trace(
- f"Position of #{channel} ({channel.id}) in Dormant will be {position} "
+ f"Position of #{channel} ({channel.id}) in {destination.name} will be {position} "
f"(was {channel.position})."
)
@@ -438,7 +447,7 @@ class HelpChannels(Scheduler, commands.Cog):
category=self.dormant_category,
sync_permissions=True,
topic=DORMANT_TOPIC,
- position=self.get_alphabetical_position(channel),
+ position=self.get_position(channel, self.dormant_category),
)
log.trace(f"Position of #{channel} ({channel.id}) is actually {channel.position}.")