diff --git a/bot/exts/help_channels/_channel.py b/bot/exts/help_channels/_channel.py index 1de6e5a83f..6a0c3264cf 100644 --- a/bot/exts/help_channels/_channel.py +++ b/bot/exts/help_channels/_channel.py @@ -41,7 +41,11 @@ def is_help_forum_post(channel: discord.abc.GuildChannel) -> bool: return getattr(channel, "parent_id", None) == constants.Channels.python_help -async def _close_help_post(closed_post: discord.Thread, closing_reason: _stats.ClosingReason) -> None: +async def _close_help_post( + closed_post: discord.Thread, + closing_reason: _stats.ClosingReason, + scheduler: scheduling.Scheduler, +) -> None: """Close the help post and record stats.""" # Get Thread with updated metadata (such as the title) closed_post = await get_or_fetch_channel(bot.instance, closed_post.id) @@ -82,6 +86,8 @@ async def _close_help_post(closed_post: discord.Thread, closing_reason: _stats.C locked=True, reason="Locked a closed help post", ) + if closed_post.id in scheduler: + scheduler.cancel(closed_post.id) _stats.report_post_count() await _stats.report_complete_session(closed_post, closing_reason) @@ -98,14 +104,18 @@ async def send_opened_post_message(post: discord.Thread) -> None: await post.send(embed=embed, content=post.owner.mention) -async def help_post_opened(opened_post: discord.Thread, *, reopen: bool = False) -> None: +async def help_post_opened( + opened_post: discord.Thread, + *, + scheduler: scheduling.Scheduler, +) -> None: """Apply new post logic to a new help forum post.""" _stats.report_post_count() bot.instance.stats.incr("help.claimed") if not isinstance(opened_post.owner, discord.Member): log.debug(f"{opened_post.owner_id} isn't a member. Closing post.") - await _close_help_post(opened_post, _stats.ClosingReason.CLEANUP) + await _close_help_post(opened_post, _stats.ClosingReason.CLEANUP, scheduler) return try: @@ -124,12 +134,12 @@ async def help_post_opened(opened_post: discord.Thread, *, reopen: bool = False) await send_opened_post_message(opened_post) -async def help_post_closed(closed_post: discord.Thread) -> None: +async def help_post_closed(closed_post: discord.Thread, scheduler: scheduling.Scheduler) -> None: """Apply archive logic to a manually closed help forum post.""" - await _close_help_post(closed_post, _stats.ClosingReason.COMMAND) + await _close_help_post(closed_post, _stats.ClosingReason.COMMAND, scheduler) -async def help_post_archived(archived_post: discord.Thread) -> None: +async def help_post_archived(archived_post: discord.Thread, scheduler: scheduling.Scheduler) -> None: """Apply archive logic to an archived help forum post.""" async for thread_update in archived_post.guild.audit_logs(limit=50, action=discord.AuditLogAction.thread_update): if thread_update.target.id != archived_post.id: @@ -140,7 +150,7 @@ async def help_post_archived(archived_post: discord.Thread) -> None: if thread_update.user.id == bot.instance.user.id: return - await _close_help_post(archived_post, _stats.ClosingReason.NATIVE) + await _close_help_post(archived_post, _stats.ClosingReason.NATIVE, scheduler) async def help_post_deleted(deleted_post_event: discord.RawThreadDeleteEvent) -> None: @@ -204,7 +214,7 @@ async def maybe_archive_idle_post(post: discord.Thread, scheduler: scheduling.Sc log.info( f"#{post} ({post.id}) is idle past {closing_time} and will be archived. Reason: {closing_reason.value}" ) - await _close_help_post(post, closing_reason) + await _close_help_post(post, closing_reason, scheduler) return if post.id in scheduler: diff --git a/bot/exts/help_channels/_cog.py b/bot/exts/help_channels/_cog.py index 0e02742556..1f00f374bc 100644 --- a/bot/exts/help_channels/_cog.py +++ b/bot/exts/help_channels/_cog.py @@ -81,9 +81,7 @@ async def close_command(self, ctx: commands.Context) -> None: # Don't use a discord.py check because the check needs to fail silently. if await self.close_check(ctx): log.info(f"Close command invoked by {ctx.author} in #{ctx.channel}.") - await _channel.help_post_closed(ctx.channel) - if ctx.channel.id in self.scheduler: - self.scheduler.cancel(ctx.channel.id) + await _channel.help_post_closed(ctx.channel, self.scheduler) @help_forum_group.command(name="title", root_aliases=("title",)) async def rename_help_post(self, ctx: commands.Context, *, title: str) -> None: @@ -112,7 +110,7 @@ async def new_post_listener(self, message: discord.Message) -> None: if thread.parent_id != self.help_forum_channel.id: return - await _channel.help_post_opened(thread) + await _channel.help_post_opened(thread, scheduler=self.scheduler) delay = min(constants.HelpChannels.deleted_idle_minutes, constants.HelpChannels.idle_minutes) * 60 self.scheduler.schedule_later( @@ -128,6 +126,8 @@ async def on_thread_update(self, before: discord.Thread, after: discord.Thread) return if not before.archived and after.archived: await _channel.help_post_archived(after) + if after.id in self.scheduler: + self.scheduler.cancel(after.id) @commands.Cog.listener() async def on_raw_thread_delete(self, deleted_thread_event: discord.RawThreadDeleteEvent) -> None: