From 5da7bdfeba938a91f4476057219aea473db44bcc Mon Sep 17 00:00:00 2001 From: dementati Date: Fri, 5 Nov 2021 09:03:07 +0100 Subject: Unpin all messages when help channel goes dormant --- bot/exts/help_channels/_caches.py | 4 ---- bot/exts/help_channels/_message.py | 10 +++------- 2 files changed, 3 insertions(+), 11 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/_message.py b/bot/exts/help_channels/_message.py index 241dd606c..cb8aa2d1a 100644 --- a/bot/exts/help_channels/_message.py +++ b/bot/exts/help_channels/_message.py @@ -174,8 +174,7 @@ async def notify(channel: discord.TextChannel, last_notification: t.Optional[Arr 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) + await pin_wrapper(message.id, message.channel, pin=True) async def send_available_message(channel: discord.TextChannel) -> None: @@ -201,11 +200,8 @@ async def send_available_message(channel: discord.TextChannel) -> None: 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) + for message in await channel.pins(): + await pin_wrapper(message.id, channel, pin=False) def _match_bot_embed(message: t.Optional[discord.Message], description: str) -> bool: -- cgit v1.2.3 From 19e7bc398c8aedf1be321d88babd134def3d12fb Mon Sep 17 00:00:00 2001 From: Mark <1515135+MarkKoz@users.noreply.github.com> Date: Tue, 7 Jun 2022 12:37:34 -0700 Subject: HelpChannels: pass message directly to pin_wrapper --- bot/exts/help_channels/_cog.py | 2 +- bot/exts/help_channels/_message.py | 26 +++++++++++--------------- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/bot/exts/help_channels/_cog.py b/bot/exts/help_channels/_cog.py index 60209ba6e..b34431af0 100644 --- a/bot/exts/help_channels/_cog.py +++ b/bot/exts/help_channels/_cog.py @@ -365,7 +365,7 @@ class HelpChannels(commands.Cog): # 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) + await _message.pin_wrapper(message, pin=False) log.debug(f"Removed a stuck pin from #{channel} ({channel.id}). ID: {message.id}") await _channel.move_to_bottom( diff --git a/bot/exts/help_channels/_message.py b/bot/exts/help_channels/_message.py index cb8aa2d1a..ef2366f23 100644 --- a/bot/exts/help_channels/_message.py +++ b/bot/exts/help_channels/_message.py @@ -174,7 +174,7 @@ async def notify(channel: discord.TextChannel, last_notification: t.Optional[Arr async def pin(message: discord.Message) -> None: """Pin an initial question `message` and store it in a cache.""" - await pin_wrapper(message.id, message.channel, pin=True) + await pin_wrapper(message, pin=True) async def send_available_message(channel: discord.TextChannel) -> None: @@ -201,7 +201,7 @@ async def send_available_message(channel: discord.TextChannel) -> None: async def unpin(channel: discord.TextChannel) -> None: """Unpin the initial question message sent in `channel`.""" for message in await channel.pins(): - await pin_wrapper(message.id, channel, pin=False) + await pin_wrapper(message, pin=False) def _match_bot_embed(message: t.Optional[discord.Message], description: str) -> bool: @@ -216,30 +216,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 -- cgit v1.2.3 From 8896b2e2e2e569df2962c87429834804472b01c3 Mon Sep 17 00:00:00 2001 From: Mark <1515135+MarkKoz@users.noreply.github.com> Date: Tue, 7 Jun 2022 12:48:41 -0700 Subject: HelpChannels: refactor pin/unpin functions --- bot/exts/help_channels/_cog.py | 14 +++++++------- bot/exts/help_channels/_message.py | 16 ++++++++++------ 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/bot/exts/help_channels/_cog.py b/bot/exts/help_channels/_cog.py index b34431af0..6a6ada3ca 100644 --- a/bot/exts/help_channels/_cog.py +++ b/bot/exts/help_channels/_cog.py @@ -356,17 +356,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, 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, @@ -439,7 +439,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 ef2366f23..720ad0774 100644 --- a/bot/exts/help_channels/_message.py +++ b/bot/exts/help_channels/_message.py @@ -173,8 +173,8 @@ async def notify(channel: discord.TextChannel, last_notification: t.Optional[Arr async def pin(message: discord.Message) -> None: - """Pin an initial question `message` and store it in a cache.""" - await pin_wrapper(message, pin=True) + """Pin an initial question `message`.""" + await _pin_wrapper(message, pin=True) async def send_available_message(channel: discord.TextChannel) -> None: @@ -198,10 +198,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`.""" +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(): - await pin_wrapper(message, pin=False) + if await _pin_wrapper(message, pin=False): + count += 1 + + return count def _match_bot_embed(message: t.Optional[discord.Message], description: str) -> bool: @@ -216,7 +220,7 @@ 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(message: discord.Message, *, pin: bool) -> bool: +async def _pin_wrapper(message: discord.Message, *, pin: bool) -> bool: """ Pin `message` if `pin` is True or unpin if it's False. -- cgit v1.2.3