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

Discord Python Bot命令无响应问题及client与Bot区别咨询

问题1:事件与命令冲突的解决

当你自定义on_message事件处理函数时,会覆盖commands.Bot默认的消息处理流程,导致命令解析逻辑被跳过,所以命令无法响应。

解决方法很直接:在on_message函数末尾添加await client.process_commands(message),让Bot处理完你的自定义逻辑后,继续解析命令。

修改后的代码如下:

import discord
from discord.ext import commands
from datetime import datetime  # 你的原代码漏了这个导入,补上才能正常使用datetime

client = commands.Bot(command_prefix='!',help_command=None,intents = discord.Intents.all())

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

@client.event
async def on_message(message):
    if message.author == client.user:
        return
    if message.content.startswith('!ligar'):
        now = datetime.now()
        current_time = now.strftime("%H:%M:%S")
        await message.channel.send(f'Caldeira ligada, as {current_time}')
    
    # 必须加这一行,让Bot处理命令
    await client.process_commands(message)

@client.command()
async def hello(ctx):
    await ctx.channel.send('funciona fdp')

client.run('token')
问题2:discord.Client与commands.Bot的区别
  • discord.Client:是Discord.py提供的基础客户端类,仅封装Discord API的底层交互,能处理原生Discord事件(比如on_message、on_member_join),但没有内置命令系统。如果你的Bot只需要监听事件、做简单消息响应,用它足够,代码更轻量化。
  • commands.Bot:是discord.Client的子类,在基础功能之上额外提供完整命令框架:
    • 自动解析前缀式命令(比如!hello)
    • 自带可自定义的帮助命令
    • 内置命令错误处理机制
    • 支持命令分组、权限控制等进阶功能
      当你需要开发带复杂命令体系的Bot时,用它能省大量自己写命令解析逻辑的时间。

简单来说,选哪个看Bot需求:纯事件监听用Client,需要命令系统用Bot。

内容的提问来源于stack exchange,提问作者Mário João

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 08:05:21