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

Discord Bot无法响应前缀命令:AttributeError报错求助

问题解决:AttributeError: 'Client' object has no attribute 'command'

错误根源很直接:discord.Client类本身不支持@client.command()装饰器,前缀命令系统是由discord.ext.commands.Bot类提供的——它是Client的扩展子类,专门用来处理命令相关逻辑。

修改步骤:

  • 把创建客户端的代码从discord.Client()替换为commands.Bot()
  • 保留原有的intents配置和命令逻辑即可

修改后的完整代码:

import discord
import random
from discord.ext import commands
from easy_pil import Editor, load_image_async, Font
from discord import File

# 移除无意义的imaplib.Commands导入
intent = discord.Intents.all()
# 用commands.Bot替代discord.Client,它才支持命令装饰器
client = commands.Bot(command_prefix=';', intents=intent)

@client.event
async def on_ready():
    print(f'We have logged in as {client.user}')

@client.command()
@commands.has_permissions(kick_members=True)
async def kick(ctx, member: discord.Member, *, reason=None):
    if reason is None:
        reason = 'no reason given'
    await ctx.guild.kick(member)
    await ctx.send(f'{member.mention} has been kicked for {reason}')

# 必须补充启动Bot的代码,替换成你的Bot令牌
# client.run('你的Bot令牌')

额外提示:

  1. 删掉了没用的from imaplib import Commands导入,避免命名混淆
  2. 将reason == None改成更规范的reason is None写法
  3. 原代码缺失启动Bot的client.run()语句,记得补上并替换成你的Bot令牌

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 23:45:42