Skip to content

Commit b96f65d

Browse files
committed
Close Support: add a task to close out old threads periodically
1 parent 98c3043 commit b96f65d

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

cogs/close_support.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
"""Discord Cog to close support threads."""
22

33
import asyncio
4+
import datetime
45
import logging
56

67
import discord
78
import prometheus_client # type: ignore
89
from discord.ext import commands
10+
from discord.ext import tasks
911

1012
from cogs import base_cog
1113

@@ -28,6 +30,8 @@ def __init__(
2830
self.resolved_reaction = resolved_reaction
2931
self.support_channel = support_channel
3032

33+
self.task_close_old_support.start() # pylint: disable=E1101
34+
3135
@commands.Cog.listener()
3236
async def on_thread_update(self, before, after) -> None:
3337
"""On thread archive, lock a thread."""
@@ -72,3 +76,40 @@ async def on_raw_reaction_add(self, payload) -> None:
7276
logger.debug("Locking thread %d due to owner resolving it.", payload.channel_id)
7377
await thread.edit(locked=True, archived=True)
7478
PROM_CLOSED.inc()
79+
80+
@tasks.loop(minutes=60 * 11)
81+
async def task_close_old_support(self) -> None:
82+
"""Close old support threads."""
83+
guild = self.bot.get_guild(self.exercism_guild_id)
84+
if not guild:
85+
logger.error("Failed to find the guild.")
86+
return
87+
channel = guild.get_channel(self.support_channel)
88+
if not channel:
89+
logger.error("Failed to find the guild.")
90+
return
91+
count = 0
92+
cutoff = datetime.datetime.now(datetime.timezone.utc) - datetime.timedelta(days=21)
93+
oldest = None
94+
for thread in channel.threads:
95+
if thread.archived or thread.locked:
96+
continue
97+
try:
98+
last = await thread.fetch_message(thread.last_message_id)
99+
except discord.errors.NotFound:
100+
continue
101+
if not last:
102+
continue
103+
oldest = min(last.created_at, oldest) if oldest else last.created_at # type: ignore
104+
if last.created_at > cutoff:
105+
continue
106+
count += 1
107+
await thread.edit(locked=True, archived=True)
108+
PROM_CLOSED.inc()
109+
logger.warning("Locking thread: %s", last.content)
110+
await asyncio.sleep(1)
111+
112+
@task_close_old_support.before_loop
113+
async def before_close_old_support(self):
114+
"""Before starting the task, wait for bot ready."""
115+
await self.bot.wait_until_ready()

0 commit comments

Comments
 (0)