diff options
| author | 2021-02-04 16:07:37 -0800 | |
|---|---|---|
| committer | 2021-02-04 16:07:37 -0800 | |
| commit | 0f46bf58bea83da9434b53ddfda3ce8331829588 (patch) | |
| tree | 184c95e6aad8ce7cc76dae8d2b01b9a2fa24f0f8 | |
| parent | Added how_to_get_help constant. (diff) | |
Added dynamic available help channels message
| -rw-r--r-- | bot/exts/help_channels/_cog.py | 48 | 
1 files changed, 48 insertions, 0 deletions
diff --git a/bot/exts/help_channels/_cog.py b/bot/exts/help_channels/_cog.py index 0995c8a79..c4ec820b5 100644 --- a/bot/exts/help_channels/_cog.py +++ b/bot/exts/help_channels/_cog.py @@ -11,6 +11,7 @@ from discord.ext import commands  from bot import constants  from bot.bot import Bot +from bot.constants import Channels, Categories  from bot.exts.help_channels import _caches, _channel, _cooldown, _message, _name, _stats  from bot.utils import channel as channel_utils, lock, scheduling @@ -20,6 +21,9 @@ NAMESPACE = "help"  HELP_CHANNEL_TOPIC = """  This is a Python help channel. You can claim your own help channel in the Python Help: Available category.  """ +AVAILABLE_HELP_CHANNELS = """ +Currently available help channel(s): {available} +"""  class HelpChannels(commands.Cog): @@ -72,6 +76,11 @@ class HelpChannels(commands.Cog):          self.last_notification: t.Optional[datetime] = None +        # Acquiring and modifying the channel to dynamically update the available help channels message. +        self.how_to_get_help: discord.TextChannel = None +        self.available_help_channels: t.Set[int] = set() +        self.dynamic_message: discord.Message = None +          # Asyncio stuff          self.queue_tasks: t.List[asyncio.Task] = []          self.init_task = self.bot.loop.create_task(self.init_cog()) @@ -114,6 +123,9 @@ class HelpChannels(commands.Cog):          await _caches.unanswered.set(message.channel.id, True) +        self.available_help_channels.remove(message.channel.id) +        await self.update_available_help_channels() +          # Not awaited because it may indefinitely hold the lock while waiting for a channel.          scheduling.create_task(self.move_to_available(), name=f"help_claim_{message.id}") @@ -275,6 +287,15 @@ class HelpChannels(commands.Cog):          # This may confuse users. So would potentially long delays for the cog to become ready.          self.close_command.enabled = True +        # Getting channels that need to be included in the dynamic message. +        task = asyncio.create_task(self.update_available_help_channels()) +        self.queue_tasks.append(task) + +        await task + +        log.trace(f"Dynamic available help message updated.") +        self.queue_tasks.remove(task) +          await self.init_available()          _stats.report_counts() @@ -332,6 +353,10 @@ class HelpChannels(commands.Cog):              category_id=constants.Categories.help_available,          ) +        # Adding the help channel to the dynamic message, and editing/sending that message. +        self.available_help_channels.add(channel.id) +        await self.update_available_help_channels() +          _stats.report_counts()      async def move_to_dormant(self, channel: discord.TextChannel) -> None: @@ -461,3 +486,26 @@ class HelpChannels(commands.Cog):          self.queue_tasks.remove(task)          return channel + +    async def update_available_help_channels(self) -> None: +        """Updates the dynamic message within #how-to-get-help for available help channels.""" +        if not self.available_help_channels: +            available_channels_category = await channel_utils.try_get_channel(Categories.help_available) +            self.available_help_channels = set( +                c.id for c in available_channels_category.channels if 'help-' in c.name +            ) + +        if self.how_to_get_help is None: +            self.how_to_get_help = await channel_utils.try_get_channel(Channels.how_to_get_help) + +        if self.dynamic_message is None: +            self.dynamic_message = await self.how_to_get_help.history(limit=1).next() + +        available_channels = AVAILABLE_HELP_CHANNELS.format( +            available=', '.join(f"<#{c}>" for c in self.available_help_channels) +        ) + +        try: +            await self.dynamic_message.edit(content=available_channels) +        except discord.Forbidden: +            self.dynamic_message = await self.how_to_get_help.send(available_channels)  |