From d1613c35bd9fea292afbe4357a3617afeb0ff9a9 Mon Sep 17 00:00:00 2001 From: ctmbl Date: Sun, 15 Oct 2023 20:57:37 +0200 Subject: [PATCH 1/3] Add global check to bot to respond only to commands sent from the right channel --- src/bot/root_pythia_bot.py | 5 ++++- src/bot/root_pythia_cogs.py | 11 +++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/bot/root_pythia_bot.py b/src/bot/root_pythia_bot.py index d53a7c8..84104e7 100644 --- a/src/bot/root_pythia_bot.py +++ b/src/bot/root_pythia_bot.py @@ -13,7 +13,7 @@ from bot.dummy_db_manager import DummyDBManager -CHANNEL_ID = getenv("CHANNEL_ID") +CHANNEL_ID = int(getenv("CHANNEL_ID")) def craft_intents(): @@ -50,6 +50,9 @@ def craft_intents(): ########### Setup bot events response ############### +@BOT.check +def is_my_channel(ctx): + return ctx.channel.id == CHANNEL_ID @BOT.event diff --git a/src/bot/root_pythia_cogs.py b/src/bot/root_pythia_cogs.py index 922c310..e0ab293 100644 --- a/src/bot/root_pythia_cogs.py +++ b/src/bot/root_pythia_cogs.py @@ -4,6 +4,7 @@ import discord from discord.ext import commands, tasks +from discord.ext.commands.errors import CheckFailure from pngmaker import NewValidatedChallenge @@ -146,6 +147,16 @@ async def verbose_if_idle(self, channel): @commands.Cog.listener() async def on_command_error(self, ctx, error): + if isinstance(error, CheckFailure): + self.logger.warning( + "discord.ext.commands.errors.CheckFailure (is_my_channel?)" + " was captured by on_command_error: some global check failed" + " for command: '%s' in channel ID '%s'", + ctx.command, + ctx.channel.id, + ) + return + await ctx.send("Command failed, please check logs for more details") await self.verbose_if_idle(ctx) From 924bcd6e4c96f9e9971a94a599f8adcb532fe3d2 Mon Sep 17 00:00:00 2001 From: ctmbl Date: Thu, 19 Oct 2023 00:50:21 +0200 Subject: [PATCH 2/3] Fix unit tests crashing because of unset but parsed env var --- src/bot/root_pythia_bot.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/bot/root_pythia_bot.py b/src/bot/root_pythia_bot.py index 84104e7..ffeaf99 100644 --- a/src/bot/root_pythia_bot.py +++ b/src/bot/root_pythia_bot.py @@ -13,7 +13,13 @@ from bot.dummy_db_manager import DummyDBManager -CHANNEL_ID = int(getenv("CHANNEL_ID")) +CHANNEL_ID = getenv("CHANNEL_ID") +if CHANNEL_ID.isnumeric(): + CHANNEL_ID = int(CHANNEL_ID) +else: + logging.warning( + "CHANNEL_ID environment variable is either not set or not an integer: %s", CHANNEL_ID + ) def craft_intents(): From 280976f1bbc5b65e54238d334537f135abdfdbfd Mon Sep 17 00:00:00 2001 From: ctmbl Date: Thu, 19 Oct 2023 23:56:32 +0200 Subject: [PATCH 3/3] Forgot the main issue, I'm silly --- src/bot/root_pythia_bot.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bot/root_pythia_bot.py b/src/bot/root_pythia_bot.py index ffeaf99..8822f25 100644 --- a/src/bot/root_pythia_bot.py +++ b/src/bot/root_pythia_bot.py @@ -14,7 +14,7 @@ CHANNEL_ID = getenv("CHANNEL_ID") -if CHANNEL_ID.isnumeric(): +if CHANNEL_ID is not None and CHANNEL_ID.isnumeric(): CHANNEL_ID = int(CHANNEL_ID) else: logging.warning(