diff options
| author | 2021-09-06 15:51:35 +0200 | |
|---|---|---|
| committer | 2021-09-06 15:51:35 +0200 | |
| commit | ab9132a555b761226c948ff816d2710ce40147e9 (patch) | |
| tree | 1b52cacfbc3f814083ca21b32b39a246426c9716 | |
| parent | Migrate to Discord.py 2.0a0 (diff) | |
Make the bot auto-join threads
| -rw-r--r-- | bot/exts/utils/bot.py | 18 |
1 files changed, 17 insertions, 1 deletions
diff --git a/bot/exts/utils/bot.py b/bot/exts/utils/bot.py index d84709616..f3a7206fc 100644 --- a/bot/exts/utils/bot.py +++ b/bot/exts/utils/bot.py @@ -1,7 +1,8 @@ import logging +from contextlib import suppress from typing import Optional -from discord import Embed, TextChannel +from discord import Embed, Forbidden, TextChannel, Thread from discord.ext.commands import Cog, Context, command, group, has_any_role from bot.bot import Bot @@ -16,6 +17,21 @@ class BotCog(Cog, name="Bot"): def __init__(self, bot: Bot): self.bot = bot + @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() + @group(invoke_without_command=True, name="bot", hidden=True) async def botinfo_group(self, ctx: Context) -> None: """Bot informational commands.""" |