diff --git a/commands/admin.py b/commands/admin.py index 77e7d74..2b634a9 100644 --- a/commands/admin.py +++ b/commands/admin.py @@ -8,9 +8,16 @@ if TYPE_CHECKING: from bot import ChouetteBot + +# Check if user is an admin of the bot +# Maybe add server admins later +async def is_admin(interaction: discord.Interaction[ChouetteBot]): + return await interaction.client.is_team_member_or_owner(interaction.user) + + # Command to publish a message from admins by the bot -@app_commands.command(name="wisper", description="Wisper an admin message") -async def wisper(interaction: discord.Interaction[ChouetteBot], message: str): - if await interaction.client.is_team_member_or_owner(interaction.user): - await interaction.channel.send(f"{interaction.client.user.name} wants so say this message: {message}") - await interaction.response.send_message("Commande réussie", ephemeral=True) +@app_commands.check(is_admin) +@app_commands.command(name="whisper", description="Wisper an admin message") +async def whisper(interaction: discord.Interaction[ChouetteBot], message: str): + await interaction.channel.send(f"{interaction.client.user.name} wants so say this message: {message}") + await interaction.response.send_message("Commande réussie", ephemeral=True, delete_after=2) diff --git a/commands_list.py b/commands_list.py index cb772fc..35e69f8 100644 --- a/commands_list.py +++ b/commands_list.py @@ -1,27 +1,34 @@ -from typing import Tuple +from __future__ import annotations + +from typing import Tuple, TYPE_CHECKING import discord +from commands.admin import whisper from commands.skyblock import Skyblock from commands.utils import latex, die_roll, ping, cheh, pin, delete -from commands.admin import wisper + +if TYPE_CHECKING: + from bot import ChouetteBot # List the commands -commands_list: Tuple = ( +COMMANDS_LIST: Tuple = ( latex, die_roll, ping, cheh, pin, delete, - wisper + whisper ) +SPACES = " " * 38 + # List of commands to add to the command tree async def commands(tree: discord.app_commands.CommandTree, hypixel_guild: discord.Guild): # Add the commands to the Tree - for command in commands_list: + for command in COMMANDS_LIST: tree.add_command(command) # Add the Skyblock command group to my Hypixel guild @@ -29,7 +36,8 @@ async def commands(tree: discord.app_commands.CommandTree, hypixel_guild: discor # Create a global commands error handler @tree.error - async def on_command_error(interaction: discord.Interaction, error: discord.app_commands.AppCommandError): + async def on_command_error(interaction: discord.Interaction[ChouetteBot], + error: discord.app_commands.AppCommandError): if isinstance(error, discord.app_commands.BotMissingPermissions): bot_perms = ", ".join(error.missing_permissions) interaction.client.bot_logger.error(f"{interaction.client.user} is missing {bot_perms} " @@ -52,6 +60,12 @@ async def on_command_error(interaction: discord.Interaction, error: discord.app_ await interaction.response.send_message(f"You are missing these permissions: {user_perms}", ephemeral=True) return + if isinstance(error, discord.app_commands.CheckFailure): + interaction.client.bot_logger.error(f"{interaction.user} tried to do {interaction.command.name} " + f"in #{interaction.channel}\n{SPACES}{error}") + await interaction.response.send_message(f"You're not allowed to use this command!", + ephemeral=True) + return await interaction.response.send_message(f"{error}\nThis error is not caught, please signal it!", ephemeral=True) interaction.client.bot_logger.error(error) diff --git a/responses.py b/responses.py index 66cdc66..4ab21dd 100644 --- a/responses.py +++ b/responses.py @@ -32,7 +32,6 @@ async def responses(client: ChouetteBot, channel: Messageable, message: str, aut await client.tree.sync() for guild in client.guilds: await client.tree.sync(guild=guild) - client.bot_logger.info(f"{author} synced the slash commands") return "Successfully synced the slash commands!", True except discord.app_commands.CommandSyncFailure as e: client.bot_logger.error(e)