Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Close Support: add a task to close out old threads periodically #111

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions cogs/close_support.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Discord Cog to close support threads."""

import asyncio
import datetime
import logging

import discord
import prometheus_client # type: ignore
from discord.ext import commands
from discord.ext import tasks

from cogs import base_cog

Expand All @@ -28,6 +30,8 @@ def __init__(
self.resolved_reaction = resolved_reaction
self.support_channel = support_channel

self.task_close_old_support.start() # pylint: disable=E1101

@commands.Cog.listener()
async def on_thread_update(self, before, after) -> None:
"""On thread archive, lock a thread."""
Expand Down Expand Up @@ -72,3 +76,43 @@ async def on_raw_reaction_add(self, payload) -> None:
logger.debug("Locking thread %d due to owner resolving it.", payload.channel_id)
await thread.edit(locked=True, archived=True)
PROM_CLOSED.inc()

@tasks.loop(minutes=60 * 11)
async def task_close_old_support(self) -> None:
"""Close old support threads."""
guild = self.bot.get_guild(self.exercism_guild_id)
if not guild:
logger.error("Failed to find the guild.")
return
channel = guild.get_channel(self.support_channel)
if not channel or not isinstance(channel, discord.ForumChannel):
logger.error("Failed to find the guild.")
return
count = 0
cutoff = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=21)
oldest = None
for thread in channel.threads:
if thread.archived or thread.locked:
continue
message_id = thread.last_message_id
if not isinstance(message_id, int):
continue
try:
last = await thread.fetch_message(message_id)
except discord.errors.NotFound:
continue
if not last:
continue
oldest = min(last.created_at, oldest) if oldest else last.created_at # type: ignore
if last.created_at > cutoff:
continue
count += 1
await thread.edit(locked=True, archived=True)
PROM_CLOSED.inc()
logger.warning("Locking thread: %s", last.content)
await asyncio.sleep(1)

@task_close_old_support.before_loop
async def before_close_old_support(self):
"""Before starting the task, wait for bot ready."""
await self.bot.wait_until_ready()
Loading