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

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")

关键修正点

  1. 删除无用的client = discord.Client(intents=intents)语句
  2. 将启动语句从client.run("TOKEN")改为bot.run("TOKEN")

这样修改后,你的!test命令就能正常响应了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.31 18:01:18