aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Chris Lovering <[email protected]>2022-11-26 13:37:32 +0000
committerGravatar Chris Lovering <[email protected]>2022-11-26 17:38:58 +0000
commit8a9a2793e19d13351459b3dcd5b55c0e56e6748c (patch)
treeaebe2c8e78bfe45a85835ca4187034023e8a2fee
parentAuto archive help forum posts after inactivity (diff)
Listen for thread starter on_message rather than thread_create
thread_create events are triggered before the thread's starter message is available, so listening for starter messages with on_message instead ensures we have a message object to use.
-rw-r--r--bot/exts/help_channels/_channel.py6
-rw-r--r--bot/exts/help_channels/_cog.py23
2 files changed, 19 insertions, 10 deletions
diff --git a/bot/exts/help_channels/_channel.py b/bot/exts/help_channels/_channel.py
index 48dcc13c6..ffdd1d2bf 100644
--- a/bot/exts/help_channels/_channel.py
+++ b/bot/exts/help_channels/_channel.py
@@ -1,5 +1,4 @@
"""Contains all logic to handle changes to posts in the help forum."""
-import asyncio
import textwrap
from datetime import timedelta
@@ -124,11 +123,6 @@ async def help_post_opened(opened_post: discord.Thread, *, reopen: bool = False)
await _close_help_post(opened_post, _stats.ClosingReason.CLEANUP)
return
- # Discord sends the open event long before the thread is ready for actions in the API.
- # This causes actions such as fetching the message, pinning message, etc to fail.
- # We sleep here to try and delay our code enough so the thread is ready in the API.
- await asyncio.sleep(2)
-
await send_opened_post_dm(opened_post)
try:
diff --git a/bot/exts/help_channels/_cog.py b/bot/exts/help_channels/_cog.py
index 0c3510c29..5da0cd52c 100644
--- a/bot/exts/help_channels/_cog.py
+++ b/bot/exts/help_channels/_cog.py
@@ -127,15 +127,30 @@ class HelpForum(commands.Cog):
await ctx.channel.edit(name=title)
- @commands.Cog.listener()
- async def on_thread_create(self, thread: discord.Thread) -> None:
+ @commands.Cog.listener("on_message")
+ async def new_post_listener(self, message: discord.Message) -> None:
"""Defer application of new post logic for posts the help forum to the _channel helper."""
+ if not isinstance(message.channel, discord.Thread):
+ return
+ thread = message.channel
+
+ if not message.id == thread.id:
+ # Opener messages have the same ID as the thread
+ return
+
if thread.parent_id != self.help_forum_channel.id:
return
await self.post_with_disallowed_title_check(thread)
await _channel.help_post_opened(thread)
+ delay = min(constants.HelpChannels.deleted_idle_minutes, constants.HelpChannels.idle_minutes) * 60
+ self.scheduler.schedule_later(
+ delay,
+ thread.id,
+ _channel.maybe_archive_idle_post(thread, self.scheduler)
+ )
+
@commands.Cog.listener()
async def on_thread_update(self, before: discord.Thread, after: discord.Thread) -> None:
"""Defer application archive logic for posts in the help forum to the _channel helper."""
@@ -152,8 +167,8 @@ class HelpForum(commands.Cog):
if deleted_thread_event.parent_id == self.help_forum_channel.id:
await _channel.help_post_deleted(deleted_thread_event)
- @commands.Cog.listener()
- async def on_message(self, message: discord.Message) -> None:
+ @commands.Cog.listener("on_message")
+ async def new_post_message_listener(self, message: discord.Message) -> None:
"""Defer application of new message logic for messages in the help forum to the _message helper."""
if not _channel.is_help_forum_post(message.channel):
return None