fix: replace decorator misuse with MaintenanceAwareTree subclass (#82)
All checks were successful
Build Docker Image / build (pull_request) Successful in 1m4s
All checks were successful
Build Docker Image / build (pull_request) Successful in 1m4s
Closes #82 `@self.tree.interaction_check` in `setup_hook` called the coroutine as a decorator factory instead of overriding it — producing an unawaited coroutine warning and leaving maintenance mode as a no-op. Replace with `MaintenanceAwareTree(discord.app_commands.CommandTree)` that overrides `interaction_check`, and pass `tree_cls=MaintenanceAwareTree` to `super().__init__`. Remove the now-obsolete decorator block from `setup_hook`. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
0a2df0f36e
commit
f7b2e452e0
36
bot.py
36
bot.py
@ -64,6 +64,25 @@ def setup_logging():
|
||||
return logger
|
||||
|
||||
|
||||
class MaintenanceAwareTree(discord.app_commands.CommandTree):
|
||||
"""CommandTree subclass that blocks non-admin users when maintenance mode is active."""
|
||||
|
||||
async def interaction_check(self, interaction: discord.Interaction) -> bool:
|
||||
bot = interaction.client
|
||||
if not getattr(bot, "maintenance_mode", False):
|
||||
return True
|
||||
if (
|
||||
isinstance(interaction.user, discord.Member)
|
||||
and interaction.user.guild_permissions.administrator
|
||||
):
|
||||
return True
|
||||
await interaction.response.send_message(
|
||||
"🔧 The bot is currently in maintenance mode. Please try again later.",
|
||||
ephemeral=True,
|
||||
)
|
||||
return False
|
||||
|
||||
|
||||
class SBABot(commands.Bot):
|
||||
"""Custom bot class for SBA league management."""
|
||||
|
||||
@ -77,6 +96,7 @@ class SBABot(commands.Bot):
|
||||
command_prefix="!", # Legacy prefix, primarily using slash commands
|
||||
intents=intents,
|
||||
description="Major Domo v2.0",
|
||||
tree_cls=MaintenanceAwareTree,
|
||||
)
|
||||
|
||||
self.logger = logging.getLogger("discord_bot_v2")
|
||||
@ -86,22 +106,6 @@ class SBABot(commands.Bot):
|
||||
"""Called when the bot is starting up."""
|
||||
self.logger.info("Setting up bot...")
|
||||
|
||||
@self.tree.interaction_check
|
||||
async def maintenance_check(interaction: discord.Interaction) -> bool:
|
||||
"""Block non-admin users when maintenance mode is enabled."""
|
||||
if not self.maintenance_mode:
|
||||
return True
|
||||
if (
|
||||
isinstance(interaction.user, discord.Member)
|
||||
and interaction.user.guild_permissions.administrator
|
||||
):
|
||||
return True
|
||||
await interaction.response.send_message(
|
||||
"🔧 The bot is currently in maintenance mode. Please try again later.",
|
||||
ephemeral=True,
|
||||
)
|
||||
return False
|
||||
|
||||
# Load command packages
|
||||
await self._load_command_packages()
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user