fix: use bot.process_commands() for prefix command dispatch

- Removed manual prefix stripping and bot._commands lookup
- Simply call bot.process_commands(message) in on_message
- This is the standard discord.py way to handle prefix commands
- bot.process_commands() internally: strips prefix → finds command → invokes it
- This separates prefix commands from slash commands completely
This commit is contained in:
Benjamyn 2026-08-27 16:53:56 +10:00
parent 6f8ed88f7a
commit 59bde86d20

35
main.py
View File

@ -142,7 +142,6 @@ async def hello_command(ctx: commands.Context, name: str = "World") -> None:
@bot.command(name="list-commands", description="List all available commands.") @bot.command(name="list-commands", description="List all available commands.")
async def list_commands(ctx: commands.Context) -> None: async def list_commands(ctx: commands.Context) -> None:
"""List all available commands.""" """List all available commands."""
# Get all registered commands (both prefix and slash)
cmds = list(bot.commands.values()) cmds = list(bot.commands.values())
cmd_list = [] cmd_list = []
for cmd in cmds: for cmd in cmds:
@ -209,36 +208,10 @@ async def on_message(message: discord.Message) -> None:
if message.guild is None: if message.guild is None:
return return
# Check if the message starts with the command prefix (exactly one !) # Call bot.process_commands() — this handles PREFIX COMMANDS
if message.content.startswith("!."): # It strips the prefix, finds the command, and invokes it.
# Strip ONLY the first '!' character # This is the CORRECT way to handle prefix commands in discord.py.
content = message.content[1:] await bot.process_commands(message)
# Find the matching command by name from the bot's internal command registry
command = bot._commands.get(content)
if command is not None:
logger.debug(f"Found command: {command.name}")
# Create a minimal context for the command
ctx = commands.Context(bot=bot, channel=message.channel, message=message, guild=message.guild)
# Call the command directly (bypassing prefix parsing)
await command.invoke(ctx)
else:
logger.debug(f"No command found for: {content}")
await message.channel.send("Command not found. Type `!list-commands` for a list of commands.")
else:
# Message doesn't start with '!', check for normal prefix commands
if message.content.startswith("!"):
content = message.content[1:]
command = bot._commands.get(content)
if command is not None:
logger.debug(f"Found command: {command.name}")
ctx = commands.Context(bot=bot, channel=message.channel, message=message, guild=message.guild)
await command.invoke(ctx)
else:
logger.debug(f"No command found for: {content}")
await message.channel.send("Command not found. Type `!list-commands` for a list of commands.")
@bot.event @bot.event