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

discord机器人运行报错AttributeError: 'Client' object has no attribute 'command'如何解决

错误原因
  • 你对bot变量做了重复赋值:首次赋值的commands.Bot实例支持command()装饰器注册指令,后续你将bot重新覆盖为discord.Client实例,而Client类本身没有command属性,调用时就会触发属性错误。
  • 代码末尾存在变量名错误:你没有定义过client变量,直接调用client.run()会触发额外报错。
修复方法

commands.Bot本身是discord.Client的子类,你不需要额外创建Client实例,直接在初始化Bot时传入活动状态参数即可,修正后的完整代码如下:

import discord
from discord.ext import commands
from config import settings

prefix = settings['PREFIX']
# 初始化时直接传入activity参数设置机器人活动状态
bot = commands.Bot(
    command_prefix = settings['PREFIX'],
    intents=discord.Intents.all(),
    activity=discord.Game(name='!help')
)
bot.remove_command('help')

@bot.event
async def on_ready():
    print('机器人成功启动!')

@bot.command()
async def ping(ctx):
    await ctx.send("Pong")

bot.run(settings['TOKEN'])

内容的提问来源于stack exchange,提问作者Станислав Орлов

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.29 20:06:06