diff options
| author | 2020-02-24 13:02:05 -0800 | |
|---|---|---|
| committer | 2020-03-22 15:54:34 -0700 | |
| commit | 8d968529cd27b61dbc12f41f96d850f0ddeab66b (patch) | |
| tree | cac9fa00f98d2166f089610cd71ca5ce74009422 | |
| parent | HelpChannels: implement create_name_queue (diff) | |
HelpChannels: retrieve category channels more efficiently
The channels property of categories sorts the channels before returning
them.
* Add a generator function to get category channels
| -rw-r--r-- | bot/cogs/help_channels.py | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/bot/cogs/help_channels.py b/bot/cogs/help_channels.py index edc15607a..1ba435308 100644 --- a/bot/cogs/help_channels.py +++ b/bot/cogs/help_channels.py @@ -1,5 +1,4 @@ import asyncio -import itertools import json import logging import typing as t @@ -85,16 +84,25 @@ class HelpChannels(Scheduler, commands.Cog): async def get_available_candidate(self) -> discord.TextChannel: """Return a dormant channel to turn into an available channel.""" + @staticmethod + def get_category_channels(category: discord.CategoryChannel) -> t.Iterable[discord.TextChannel]: + """Yield the channels of the `category` in an unsorted manner.""" + # This is faster than using category.channels because the latter sorts them. + for channel in category.guild.channels: + if channel.category_id == category.id: + yield channel + def get_used_names(self) -> t.Set[str]: """Return channels names which are already being used.""" start_index = len("help-") - channels = itertools.chain( - self.available_category.channels, - self.in_use_category.channels, - self.dormant_category.channels, - ) - return {c.name[start_index:] for c in channels} + names = set() + for cat in (self.available_category, self.in_use_category, self.dormant_category): + for channel in self.get_category_channels(cat): + name = channel.name[start_index:] + names.add(name) + + return names async def get_idle_time(self, channel: discord.TextChannel) -> int: """Return the time elapsed since the last message sent in the `channel`.""" |