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令牌')
额外提示:
- 删掉了没用的
from imaplib import Commands导入,避免命名混淆 - 将
reason == None改成更规范的reason is None写法 - 原代码缺失启动Bot的
client.run()语句,记得补上并替换成你的Bot令牌
内容的提问来源于stack exchange,提问作者Bladeorade
相关产品推荐
相关产品推荐

