aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Matteo Bertucci <[email protected]>2021-08-31 15:23:26 +0200
committerGravatar Chris Lovering <[email protected]>2021-08-31 17:21:24 +0100
commit3f90389d76b4666868cbb24e7958d5cf236207c5 (patch)
tree2f9359101b5be654225b34c6511ee6ff985d28e3
parentTrigger the bot on mentions (diff)
Make the bot automatically join threads
This is just a QoL of life thing, avoiding having us to check if the bot is here or not before issuing a command Co-authored-by: ChrisJL <[email protected]>
-rw-r--r--bot/bot.py20
1 files changed, 18 insertions, 2 deletions
diff --git a/bot/bot.py b/bot/bot.py
index b877233e..c7b87a65 100644
--- a/bot/bot.py
+++ b/bot/bot.py
@@ -1,14 +1,15 @@
import asyncio
import logging
import socket
+from contextlib import suppress
from typing import Optional
import discord
from aiohttp import AsyncResolver, ClientSession, TCPConnector
from async_rediscache import RedisSession
-from discord import DiscordException, Embed
+from discord import DiscordException, Embed, Forbidden, Thread
from discord.ext import commands
-from discord.ext.commands import when_mentioned_or
+from discord.ext.commands import Cog, when_mentioned_or
from bot import constants
@@ -46,6 +47,21 @@ class Bot(commands.Bot):
return None
return guild.me
+ @Cog.listener()
+ async def on_thread_join(self, thread: Thread) -> None:
+ """
+ Try to join newly created threads.
+
+ Despite the event name being misleading, this is dispatched when new threads are created.
+ We want our bots to automatically join threads in order to answer commands using their prefixes.
+ """
+ if thread.me:
+ # Already in this thread, return early
+ return
+
+ with suppress(Forbidden):
+ await thread.join()
+
async def close(self) -> None:
"""Close Redis session when bot is shutting down."""
await super().close()