You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

discord.py机器人前缀命令失效问题排查求助

Discord.py 前缀命令无响应问题解决

你的问题根源在于自定义了on_message事件,但没有触发discord.py的命令处理逻辑。当重写on_message时,默认的命令解析流程会被覆盖,必须手动调用await bot.process_commands(message)才能让前缀命令生效。

修改后的代码如下:

import discord
from discord.ext import commands

intents = discord.Intents(messages=True, guilds=True)
intents.message_content = True
intents.messages = True
intents.guild_messages = True

bot = commands.Bot(command_prefix="$", intents=intents)

@bot.event
async def on_ready():
    print(f'Logged on as {bot.user}!')

@bot.event
async def on_message(message):
    print(f'Message from {message.author}: {message.content}')
    
    # 过滤机器人自身消息,避免循环
    if message.author == bot.user :   
        return
    
    # 手动触发命令处理逻辑
    await bot.process_commands(message)
        
@bot.command()
async def ping(ctx):
    print("test")
    await ctx.channel.send("pong")


bot.run('MY_BOT_TOKEN')

另外建议取消注释过滤机器人自身消息的代码,防止机器人响应自己发送的消息引发不必要的循环。

内容的提问来源于stack exchange,提问作者GreGre

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 23:55:18