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

discord.py命令无法识别:.ping指令无响应问题求助

解决你的discord.py .ping命令无响应问题

嘿,我一眼就发现问题啦——你自定义了on_message事件,但没给机器人留机会处理命令!

在discord.py里,如果你重写了on_message函数,默认会阻止命令系统自动触发。因为消息事件会先被你的on_message捕获,而没有继续传递给命令处理器。要解决这个,只需要在你的on_message函数末尾加上一行代码,让机器人去处理消息里的命令。

修正后的代码

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix = ".")

@bot.event
async def on_ready():
    print("Everything's all ready to go~")

@bot.event
async def on_message(message):
    author = message.author
    content = message.content
    print(content)
    # 关键:添加这行让机器人处理命令
    await bot.process_commands(message)

@bot.event
async def on_message_delete(message):
    author = message.author
    channel = message.channel
    # 注意:旧版discord.py的send_message写法,如果你用的是v1.x+版本,应该改成await channel.send(message.content)
    await bot.send_message(channel, message.content)

@bot.command()
async def ping():
    await bot.say('Pong!')

bot.run('Token')

额外提示

  • 如果你后续升级到discord.py v1.x及以上版本,bot.say()已经被移除了,需要改成await ctx.send('Pong!'),同时命令函数要接收ctx参数:
    @bot.command()
    async def ping(ctx):
        await ctx.send('Pong!')
    
  • 确保你的机器人token正确,并且机器人已经被邀请到服务器,拥有发送消息的权限哦。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.12 04:14:33