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)
This commit is contained in:
parent
e458214467
commit
331e0e10a7
34
main.py
34
main.py
@ -139,6 +139,20 @@ async def hello_command(ctx: commands.Context, name: str = "World") -> None:
|
|||||||
"""Say hello to someone."""
|
"""Say hello to someone."""
|
||||||
await ctx.send(f"Hello, {name}!")
|
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 ──────────────────────────────────────────────────────────
|
# ─── Event Handlers ──────────────────────────────────────────────────────────
|
||||||
|
|
||||||
@bot.event
|
@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}")
|
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
|
@bot.event
|
||||||
async def on_voice_state_update(
|
async def on_voice_state_update(
|
||||||
member: discord.Member,
|
member: discord.Member,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user