diff options
| author | 2021-04-11 11:29:34 -0700 | |
|---|---|---|
| committer | 2021-04-11 11:29:34 -0700 | |
| commit | f9eac386d2e5c5b5e6bd05c7591707e9ec0fa661 (patch) | |
| tree | 0d775714aec72e0c3dae82ffdca77af8ec211fc0 | |
| parent | Merge pull request #1503 from python-discord/tp-get_review-command (diff) | |
| parent | Merge branch 'main' into dynamic-available-message-patch (diff) | |
Merge pull request #1414 from python-discord/dynamic-available-message-patch
Reworked dynamic available help message logic.
| -rw-r--r-- | bot/constants.py | 1 | ||||
| -rw-r--r-- | bot/exts/help_channels/_caches.py | 4 | ||||
| -rw-r--r-- | bot/exts/help_channels/_cog.py | 48 | ||||
| -rw-r--r-- | config-default.yml | 1 | 
4 files changed, 54 insertions, 0 deletions
| diff --git a/bot/constants.py b/bot/constants.py index 547a94a0b..6d14bbb3a 100644 --- a/bot/constants.py +++ b/bot/constants.py @@ -412,6 +412,7 @@ class Channels(metaclass=YAMLGetter):      python_general: int      cooldown: int +    how_to_get_help: int      attachment_log: int      message_log: int diff --git a/bot/exts/help_channels/_caches.py b/bot/exts/help_channels/_caches.py index e741fd20f..c5e4ee917 100644 --- a/bot/exts/help_channels/_caches.py +++ b/bot/exts/help_channels/_caches.py @@ -20,3 +20,7 @@ non_claimant_last_message_times = RedisCache(namespace="HelpChannels.non_claiman  # This cache maps a help channel to original question message in same channel.  # RedisCache[discord.TextChannel.id, discord.Message.id]  question_messages = RedisCache(namespace="HelpChannels.question_messages") + +# This cache keeps track of the dynamic message ID for +# the continuously updated message in the #How-to-get-help channel. +dynamic_message = RedisCache(namespace="HelpChannels.dynamic_message") diff --git a/bot/exts/help_channels/_cog.py b/bot/exts/help_channels/_cog.py index 18457f6a5..c18fcf875 100644 --- a/bot/exts/help_channels/_cog.py +++ b/bot/exts/help_channels/_cog.py @@ -21,6 +21,7 @@ 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): @@ -75,6 +76,9 @@ class HelpChannels(commands.Cog):          self.last_notification: t.Optional[arrow.Arrow] = None +        self.dynamic_message: t.Optional[int] = None +        self.available_help_channels: t.Set[discord.TextChannel] = set() +          # Asyncio stuff          self.queue_tasks: t.List[asyncio.Task] = []          self.init_task = self.bot.loop.create_task(self.init_cog()) @@ -123,6 +127,9 @@ class HelpChannels(commands.Cog):          # Delete to indicate that the help session has yet to receive an answer.          await _caches.non_claimant_last_message_times.delete(message.channel.id) +        # Removing the help channel from the dynamic message, and editing/sending that message. +        self.available_help_channels.remove(message.channel) +          # 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}") @@ -240,6 +247,10 @@ class HelpChannels(commands.Cog):              for channel in channels[:abs(missing)]:                  await self.unclaim_channel(channel, closed_on=_channel.ClosingReason.CLEANUP) +        # Getting channels that need to be included in the dynamic message. +        await self.update_available_help_channels() +        log.trace("Dynamic available help message updated.") +      async def init_categories(self) -> None:          """Get the help category objects. Remove the cog if retrieval fails."""          log.trace("Getting the CategoryChannel objects for the help categories.") @@ -284,6 +295,10 @@ class HelpChannels(commands.Cog):          # This may confuse users. So would potentially long delays for the cog to become ready.          self.close_command.enabled = True +        # Acquiring the dynamic message ID, if it exists within the cache. +        log.trace("Attempting to fetch How-to-get-help dynamic message ID.") +        self.dynamic_message = await _caches.dynamic_message.get("message_id") +          await self.init_available()          _stats.report_counts() @@ -338,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) +        await self.update_available_help_channels() +          _stats.report_counts()      async def move_to_dormant(self, channel: discord.TextChannel) -> None: @@ -472,3 +491,32 @@ 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: +            self.available_help_channels = set( +                c for c in self.available_category.channels if not _channel.is_excluded_channel(c) +            ) + +        available_channels = AVAILABLE_HELP_CHANNELS.format( +            available=', '.join(c.mention for c in self.available_help_channels) or None +        ) + +        if self.dynamic_message is not None: +            try: +                log.trace("Help channels have changed, dynamic message has been edited.") +                await self.bot.http.edit_message( +                    constants.Channels.how_to_get_help, self.dynamic_message, content=available_channels +                ) +            except discord.NotFound: +                pass +            else: +                return + +        log.trace("Dynamic message could not be edited or found. Creating a new one.") +        new_dynamic_message = await self.bot.http.send_message( +            constants.Channels.how_to_get_help, available_channels +        ) +        self.dynamic_message = new_dynamic_message["id"] +        await _caches.dynamic_message.set("message_id", self.dynamic_message) diff --git a/config-default.yml b/config-default.yml index 9b07d026d..8c6e18470 100644 --- a/config-default.yml +++ b/config-default.yml @@ -163,6 +163,7 @@ guild:          # Python Help: Available          cooldown:           720603994149486673 +        how_to_get_help:    704250143020417084          # Topical          discord_py:         343944376055103488 | 
