From 331e0e10a7d5b7305cfe6831e1d0e1d6a9f8e69d Mon Sep 17 00:00:00 2001 From: Benjamyn Date: Thu, 27 Aug 2026 16:39:20 +1000 Subject: [PATCH] fix: add on_message handler to support !help and other text commands - Added on_message event that strips leading '!' and passes to process_commands() - Added !help command that lists all registered commands - Fixes: 'Command not found' error when typing !help or any ! command - Now supports both slash commands (/ping) and prefix commands (!ping) --- main.py | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/main.py b/main.py index 74d33c9..639f096 100644 --- a/main.py +++ b/main.py @@ -139,6 +139,20 @@ async def hello_command(ctx: commands.Context, name: str = "World") -> None: """Say hello to someone.""" await ctx.send(f"Hello, {name}!") +@bot.command(name="help", description="List all available commands.") +async def help_command(ctx: commands.Context) -> None: + """List all available commands.""" + # Get all registered commands (both prefix and slash) + cmds = list(bot.commands.values()) + cmd_list = [] + for cmd in cmds: + cmd_list.append(f" `{cmd.name}` — {cmd.description}") + + await ctx.send( + f"**{bot.user.name} — Command List**\n\n" + + "\n".join(cmd_list) + ) + # ─── Event Handlers ────────────────────────────────────────────────────────── @bot.event @@ -184,6 +198,26 @@ async def on_command_error(ctx: commands.Context, error: commands.CommandError) await ctx.send(f"An error occurred: {error}") +@bot.event +async def on_message(message: discord.Message) -> None: + """Handle text messages sent in channels.""" + # Ignore bot's own messages + if message.author == bot.user: + return + + # Check if the message is in a guild (server) + if message.guild is None: + return + + # Check if the message is a command + if message.content.startswith("!."): + # Strip the leading "!" and process + content = message.content[1:] + # Remove the leading "!" again since we stripped one + content = content.lstrip("!") + await bot.process_commands(message) + + @bot.event async def on_voice_state_update( member: discord.Member,