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:
parent
6f8ed88f7a
commit
59bde86d20
35
main.py
35
main.py
@ -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.")
|
||||
async def list_commands(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:
|
||||
@ -209,36 +208,10 @@ async def on_message(message: discord.Message) -> None:
|
||||
if message.guild is None:
|
||||
return
|
||||
|
||||
# Check if the message starts with the command prefix (exactly one !)
|
||||
if message.content.startswith("!."):
|
||||
# Strip ONLY the first '!' character
|
||||
content = message.content[1:]
|
||||
|
||||
# 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.")
|
||||
# Call bot.process_commands() — this handles PREFIX COMMANDS
|
||||
# It strips the prefix, finds the command, and invokes it.
|
||||
# This is the CORRECT way to handle prefix commands in discord.py.
|
||||
await bot.process_commands(message)
|
||||
|
||||
|
||||
@bot.event
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user