Discord机器人bot.commands命令失效问题求助
解决Discord机器人命令失效问题
你的代码核心问题是同时创建了两个Discord客户端实例,却启动了错误的那个,导致注册在bot上的命令根本没被加载运行。
问题分析
- 你初始化了
client = discord.Client(intents=intents)和bot = commands.Bot(command_prefix='!',intents=intents)两个对象,但最后调用的是client.run("TOKEN")启动客户端。 commands.Bot是discord.Client的子类,自带命令框架支持,完全不需要单独创建client实例。
修正后的完整代码
#Imports om de code werkend te maken. import discord from discord.ext import commands #Stukje om de bot aan te zetten en aan de eisen te voldoen van Discord. intents = discord.Intents.default() intents.message_content = True # 只保留commands.Bot实例,删除多余的discord.Client bot = commands.Bot(command_prefix='!',intents=intents) @bot.command() async def test(ctx, arg): await ctx.send(arg) print("Test") # 启动bot实例而不是client bot.run("TOKEN")
关键修正点
- 删除无用的
client = discord.Client(intents=intents)语句 - 将启动语句从
client.run("TOKEN")改为bot.run("TOKEN")
这样修改后,你的!test命令就能正常响应了。
内容的提问来源于stack exchange,提问作者Marijn Klasens
相关产品推荐
相关产品推荐

