Skip to content

Commit

Permalink
Add global check to bot to respond only to commands sent from the rig…
Browse files Browse the repository at this point in the history
…ht channel
  • Loading branch information
ctmbl committed Oct 15, 2023
1 parent 8151744 commit 5397df6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/bot/root_pythia_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions src/bot/root_pythia_cogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import discord
from discord.ext import commands, tasks
from discord.ext.commands.errors import CheckFailure

from pngmaker import NewValidatedChallenge

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 5397df6

Please sign in to comment.