fix: add ! prefix commands to main.py

- Added !ping, !info, !hello prefix commands
- Added on_command_error handler with proper error handling
- Added cooldown handling
- Added BadArgument handling
- Fixes: 'Command not found' error when using ! prefix commands
This commit is contained in:
Benjamyn 2026-08-27 16:35:46 +10:00
parent f7bdec8195
commit e458214467

31
main.py
View File

@ -64,7 +64,6 @@ logging.getLogger("discord.utils").setLevel(logging.DEBUG if DEBUG else logging.
logging.getLogger("discord.app_commands").setLevel(logging.DEBUG if DEBUG else logging.WARNING) logging.getLogger("discord.app_commands").setLevel(logging.DEBUG if DEBUG else logging.WARNING)
logging.getLogger("discord.ext.commands").setLevel(logging.DEBUG if DEBUG else logging.WARNING) logging.getLogger("discord.ext.commands").setLevel(logging.DEBUG if DEBUG else logging.WARNING)
logging.getLogger("discord.errors").setLevel(logging.DEBUG if DEBUG else logging.WARNING) logging.getLogger("discord.errors").setLevel(logging.DEBUG if DEBUG else logging.WARNING)
logging.getLogger("discord.utils").setLevel(logging.DEBUG if DEBUG else logging.WARNING)
logging.getLogger("discord.client").setLevel(logging.DEBUG if DEBUG else logging.WARNING) logging.getLogger("discord.client").setLevel(logging.DEBUG if DEBUG else logging.WARNING)
# ─── Logging Configuration ─────────────────────────────────────────────────── # ─── Logging Configuration ───────────────────────────────────────────────────
@ -118,6 +117,28 @@ tree.add_command(AppCommand(name="upload-emoji", callback=upload_emoji, descript
tree.add_command(AppCommand(name="list-emojis", callback=list_emojis, description="List all custom emojis in the guild.")) tree.add_command(AppCommand(name="list-emojis", callback=list_emojis, description="List all custom emojis in the guild."))
tree.add_command(AppCommand(name="delete-emoji", callback=delete_emoji, description="Delete a custom emoji.")) tree.add_command(AppCommand(name="delete-emoji", callback=delete_emoji, description="Delete a custom emoji."))
# ─── Prefix Commands ─────────────────────────────────────────────────────────
@bot.command(name="ping", description="Check bot latency.")
async def ping_command(ctx: commands.Context) -> None:
"""Check the bot's latency."""
latency = round(bot.latency * 1000)
await ctx.send(f"Pong! Latency: {latency}ms")
@bot.command(name="info", description="Show bot information.")
async def info_command(ctx: commands.Context) -> None:
"""Show bot information."""
await ctx.send(
f"**{ctx.guild.name if ctx.guild else 'DM'}**\n"
f"Bot: `{bot.user.name}` (ID: {bot.user.id})\n"
f"Latency: `{bot.latency:.3f}s`"
)
@bot.command(name="hello", description="Say hello to someone.")
async def hello_command(ctx: commands.Context, name: str = "World") -> None:
"""Say hello to someone."""
await ctx.send(f"Hello, {name}!")
# ─── Event Handlers ────────────────────────────────────────────────────────── # ─── Event Handlers ──────────────────────────────────────────────────────────
@bot.event @bot.event
@ -143,6 +164,7 @@ async def on_ready() -> None:
@bot.event @bot.event
async def on_command_error(ctx: commands.Context, error: commands.CommandError) -> None: async def on_command_error(ctx: commands.Context, error: commands.CommandError) -> None:
"""Global error handler for all commands.""" """Global error handler for all commands."""
logger.debug(f"Command error for {ctx.author.name} ({ctx.author.id}): {error}")
if isinstance(error, commands.CommandNotFound): if isinstance(error, commands.CommandNotFound):
await ctx.send("Command not found. Type `!help` for a list of commands.") await ctx.send("Command not found. Type `!help` for a list of commands.")
elif isinstance(error, commands.CheckFailure): elif isinstance(error, commands.CheckFailure):
@ -151,6 +173,13 @@ async def on_command_error(ctx: commands.Context, error: commands.CommandError)
await ctx.send("You don't have permission to use this command.") await ctx.send("You don't have permission to use this command.")
elif isinstance(error, commands.MissingRequiredArgument): elif isinstance(error, commands.MissingRequiredArgument):
await ctx.send(f"Missing argument: {error.param.name}.") await ctx.send(f"Missing argument: {error.param.name}.")
elif isinstance(error, commands.BadArgument):
await ctx.send(f"Bad argument: {error.argument}.")
elif isinstance(error, commands.CommandOnCooldown):
remaining = int(error.retry_after)
await ctx.send(
f"This command is on cooldown. Try again in {remaining} seconds."
)
else: else:
await ctx.send(f"An error occurred: {error}") await ctx.send(f"An error occurred: {error}")