Skip to content

Commit dac6257

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

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

Diff for: cogs/close_support.py

+44
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,43 @@ 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 or not isinstance(channel, discord.ForumChannel):
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+
message_id = thread.last_message_id
98+
if not isinstance(message_id, int):
99+
continue
100+
try:
101+
last = await thread.fetch_message(message_id)
102+
except discord.errors.NotFound:
103+
continue
104+
if not last:
105+
continue
106+
oldest = min(last.created_at, oldest) if oldest else last.created_at # type: ignore
107+
if last.created_at > cutoff:
108+
continue
109+
count += 1
110+
await thread.edit(locked=True, archived=True)
111+
PROM_CLOSED.inc()
112+
logger.warning("Locking thread: %s", last.content)
113+
await asyncio.sleep(1)
114+
115+
@task_close_old_support.before_loop
116+
async def before_close_old_support(self):
117+
"""Before starting the task, wait for bot ready."""
118+
await self.bot.wait_until_ready()

0 commit comments

Comments
 (0)