Discord.py报错AttributeError: 'Client'对象无'command'属性求解决
解决Discord机器人AttributeError: 'Client' object has no attribute 'command'错误
错误原因很明确:你用discord.Client()创建机器人实例,但@bot.command()是discord.ext.commands.Bot类独有的装饰器,Client类不支持命令装饰器,所以触发该报错。
只需把创建实例的代码替换为commands.Bot即可,它是Client的子类,继承了所有基础功能,同时支持命令系统。修改后的完整代码如下:
#Importing stuff import discord import os import random from discord.ext import commands #Variables command_prefix = '!' intents = discord.Intents.all() token = 'MTAzOTA3ODMyODcxMzM1NTMwNA.GMKlpL.gLzQ9H6UCaAE7PNeb0Nv-I2cb-e8p-NgZZJydY' #Client Setup - 替换为commands.Bot bot = commands.Bot(command_prefix=command_prefix, intents=intents) #On ready function (startup) @bot.event async def on_ready(): print('Online') #Status Setup activity = discord.Game(name="Put Status Here", type=3) await bot.change_presence(activity=activity) @bot.command() async def help(ctx): await ctx.send('baller') #Run bot.run(token)
替换后,原有的@bot.event事件监听逻辑不受影响,同时@bot.command()装饰器可以正常工作。
内容的提问来源于stack exchange,提问作者Panda Monium
相关产品推荐
相关产品推荐

