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

Discord.py定义on_message后client.command指令不生效是什么原因?

问题原因

你重写了on_message事件处理函数后,没有手动调用命令处理逻辑,Discord.py默认的命令匹配规则会被覆盖,所以所有@client.command()注册的指令都会失效。

修复方案

在你自定义的on_message函数末尾,添加await client.process_commands(message)即可,修改后的代码如下:

import discord
from discord.ext import commands
import asyncio

client = commands.Bot(command_prefix="-")

@client.event
async def on_message(message):
    if message.content.startswith("uno regeln"):
        await message.channel.send('http://www.uno-kartenspiel.de/spielregeln/')
    # 新增这一行,执行默认的命令处理逻辑
    await client.process_commands(message)

@client.command()
async def test(ctx):
    await ctx.send("Ok")
     
client.run("TOKEN")
补充优化建议

可以在on_message开头先判断消息发送者是不是机器人本身,减少不必要的逻辑执行,避免循环触发事件:

@client.event
async def on_message(message):
    # 跳过机器人自己发送的消息
    if message.author.bot:
        return
    if message.content.startswith("uno regeln"):
        await message.channel.send('http://www.uno-kartenspiel.de/spielregeln/')
    await client.process_commands(message)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.04 06:15:03