diff options
-rw-r--r-- | bot/exts/help_channels/_caches.py | 4 | ||||
-rw-r--r-- | bot/exts/help_channels/_cog.py | 14 | ||||
-rw-r--r-- | bot/exts/help_channels/_message.py | 42 |
3 files changed, 26 insertions, 34 deletions
diff --git a/bot/exts/help_channels/_caches.py b/bot/exts/help_channels/_caches.py index 8d45c2466..937c4ab57 100644 --- a/bot/exts/help_channels/_caches.py +++ b/bot/exts/help_channels/_caches.py @@ -17,10 +17,6 @@ claimant_last_message_times = RedisCache(namespace="HelpChannels.claimant_last_m # RedisCache[discord.TextChannel.id, UtcPosixTimestamp] non_claimant_last_message_times = RedisCache(namespace="HelpChannels.non_claimant_last_message_times") -# 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 bef6f3709..f1351013a 100644 --- a/bot/exts/help_channels/_cog.py +++ b/bot/exts/help_channels/_cog.py @@ -394,17 +394,17 @@ class HelpChannels(commands.Cog): log.trace("Making a channel available.") channel = await self.get_available_candidate() - log.info(f"Making #{channel} ({channel.id}) available.") + channel_str = f"#{channel} ({channel.id})" + log.info(f"Making {channel_str} available.") await _message.send_available_message(channel) - log.trace(f"Moving #{channel} ({channel.id}) to the Available category.") + log.trace(f"Moving {channel_str} to the Available category.") # Unpin any previously stuck pins - log.trace(f"Looking for pins stuck in #{channel} ({channel.id}).") - for message in await channel.pins(): - await _message.pin_wrapper(message.id, channel, pin=False) - log.debug(f"Removed a stuck pin from #{channel} ({channel.id}). ID: {message.id}") + log.trace(f"Looking for pins stuck in {channel_str}.") + if stuck_pins := await _message.unpin_all(channel): + log.debug(f"Removed {stuck_pins} stuck pins from {channel_str}.") await _channel.move_to_bottom( channel=channel, @@ -480,7 +480,7 @@ class HelpChannels(commands.Cog): else: await members.handle_role_change(claimant, claimant.remove_roles, self.cooldown_role) - await _message.unpin(channel) + await _message.unpin_all(channel) await _stats.report_complete_session(channel.id, closed_on) await self.move_to_dormant(channel) diff --git a/bot/exts/help_channels/_message.py b/bot/exts/help_channels/_message.py index 128fa847b..00d57ea40 100644 --- a/bot/exts/help_channels/_message.py +++ b/bot/exts/help_channels/_message.py @@ -214,9 +214,8 @@ async def notify_running_low(number_of_channels_left: int, last_notification: Ar async def pin(message: discord.Message) -> None: - """Pin an initial question `message` and store it in a cache.""" - if await pin_wrapper(message.id, message.channel, pin=True): - await _caches.question_messages.set(message.channel.id, message.id) + """Pin an initial question `message`.""" + await _pin_wrapper(message, pin=True) async def send_available_message(channel: discord.TextChannel) -> None: @@ -240,13 +239,14 @@ async def send_available_message(channel: discord.TextChannel) -> None: await channel.send(embed=embed) -async def unpin(channel: discord.TextChannel) -> None: - """Unpin the initial question message sent in `channel`.""" - msg_id = await _caches.question_messages.pop(channel.id) - if msg_id is None: - log.debug(f"#{channel} ({channel.id}) doesn't have a message pinned.") - else: - await pin_wrapper(msg_id, channel, pin=False) +async def unpin_all(channel: discord.TextChannel) -> int: + """Unpin all pinned messages in `channel` and return the amount of unpinned messages.""" + count = 0 + for message in await channel.pins(): + if await _pin_wrapper(message, pin=False): + count += 1 + + return count def _match_bot_embed(message: t.Optional[discord.Message], description: str) -> bool: @@ -261,30 +261,26 @@ def _match_bot_embed(message: t.Optional[discord.Message], description: str) -> return message.author == bot.instance.user and bot_msg_desc.strip() == description.strip() -async def pin_wrapper(msg_id: int, channel: discord.TextChannel, *, pin: bool) -> bool: +async def _pin_wrapper(message: discord.Message, *, pin: bool) -> bool: """ - Pin message `msg_id` in `channel` if `pin` is True or unpin if it's False. + Pin `message` if `pin` is True or unpin if it's False. Return True if successful and False otherwise. """ - channel_str = f"#{channel} ({channel.id})" - if pin: - func = bot.instance.http.pin_message - verb = "pin" - else: - func = bot.instance.http.unpin_message - verb = "unpin" + channel_str = f"#{message.channel} ({message.channel.id})" + func = message.pin if pin else message.unpin try: - await func(channel.id, msg_id) + await func() except discord.HTTPException as e: if e.code == 10008: - log.debug(f"Message {msg_id} in {channel_str} doesn't exist; can't {verb}.") + log.debug(f"Message {message.id} in {channel_str} doesn't exist; can't {func.__name__}.") else: log.exception( - f"Error {verb}ning message {msg_id} in {channel_str}: {e.status} ({e.code})" + f"Error {func.__name__}ning message {message.id} in {channel_str}: " + f"{e.status} ({e.code})" ) return False else: - log.trace(f"{verb.capitalize()}ned message {msg_id} in {channel_str}.") + log.trace(f"{func.__name__.capitalize()}ned message {message.id} in {channel_str}.") return True |