Skip to content

Commit

Permalink
fix: use built-in check for whisper
Browse files Browse the repository at this point in the history
handle CheckFailure in on_command_error
lint & typos
remove redundant log
  • Loading branch information
Zalk0 committed Nov 23, 2023
1 parent c23a55a commit 4d7a0db
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
17 changes: 12 additions & 5 deletions commands/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
26 changes: 20 additions & 6 deletions commands_list.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,43 @@
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
tree.add_command(Skyblock(), guild=hypixel_guild)

# 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} "
Expand All @@ -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)
1 change: 0 additions & 1 deletion responses.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 4d7a0db

Please sign in to comment.