fix: use bot._commands registry to find prefix commands
- Changed on_message handler to use bot._commands.get(content) instead of bot.get_command() - bot._commands is the internal command registry that stores commands by their exact name - This properly routes prefix commands to their handlers without relying on process_commands() - Fixes: commands were not being invoked because process_commands() doesn't route to prefix commands
This commit is contained in:
parent
ba3fd2e8fa
commit
6f8ed88f7a
32
main.py
32
main.py
@ -200,7 +200,7 @@ async def on_command_error(ctx: commands.Context, error: commands.CommandError)
|
||||
|
||||
@bot.event
|
||||
async def on_message(message: discord.Message) -> None:
|
||||
"""Handle text messages sent in channels."""
|
||||
"""Handle text messages sent in channels (prefix commands)."""
|
||||
# Ignore bot's own messages
|
||||
if message.author == bot.user:
|
||||
return
|
||||
@ -211,10 +211,34 @@ async def on_message(message: discord.Message) -> None:
|
||||
|
||||
# Check if the message starts with the command prefix (exactly one !)
|
||||
if message.content.startswith("!."):
|
||||
# Strip ONLY the first "!" character (not all of them)
|
||||
# Strip ONLY the first '!' character
|
||||
content = message.content[1:]
|
||||
# Now process the command
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user