You need to enable JavaScript to run this app. Discord.py使用@bot.command编写的指令无任何响应问题咨询
优惠活动
大模型
产品
解决方案
定价
更多

Discord.py使用@bot.command编写的指令无任何响应问题咨询

问题根因
  • 重复创建两个独立机器人实例:commands.Bot 本身继承自 discord.Client 全部能力,无需额外实例化 discord.Client,两个独立实例使用同一token登录会直接造成运行冲突。
  • 重复发起网关连接:同时调用 client.run()、bot.run() 会让两个实例抢占Discord连接资源,逻辑完全混乱。
  • 冗余命令注册:@bot.command() 装饰器已自动将函数绑定为bot的可执行指令,手动调用 bot.add_command(yes) 属于重复操作,无实际作用。
  • 缺少必要权限配置:Discord.py 2.0+ 强制要求,必须在代码中显式开启 message_content 意图,且同步在Discord开发者后台开启对应的特权开关,否则bot无法读取消息文本,自然无法识别前缀指令。
修正后可正常运行的代码
from discord.ext import commands
import discord
from config import *

# 配置连接所需的权限意图
intents = discord.Intents.default()
intents.message_content = True  # 消息内容读取权限为必需配置

# 仅初始化一个bot实例
bot = commands.Bot(command_prefix='+', intents=intents)

@bot.command()
async def yes(ctx):
    await ctx.send('yes')
    print("worked")

# 移除冗余的client实例、重复add_command、多份run调用
bot.run("token")
排查提示

如果修正代码后仍无响应,请到Discord开发者后台对应机器人的Bot设置页,确认「MESSAGE CONTENT INTENT」特权开关为开启状态,否则代码层面的权限配置无法生效。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.11.02 15:54:02