
Discord 机器人无法响应 !test 等文本命令,通常是因为未启用 message_content 意图(intent)——它既是代码中必须显式开启的权限,也需在 Discord 开发者门户后台手动启用。
discord 机器人无法响应 `!test` 等文本命令,通常是因为未启用 `message_content` 意图(intent)——它既是代码中必须显式开启的权限,也需在 discord 开发者门户后台手动启用。
从 Discord.py v2.0 起,出于隐私与安全策略,读取消息内容(即识别用户发送的命令文本)不再默认开启。这意味着即使你的代码语法完全正确,只要 message_content intent 缺失,@bot.command() 就无法接收到任何消息,自然不会触发响应。
✅ 正确配置分为两步,缺一不可:
1. 代码中启用 intent
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.message_content = True # ? 关键:必须显式设为 True
bot = commands.Bot(
command_prefix='!',
intents=intents,
# 建议添加 help_command=None 避免默认帮助命令冲突(可选)
)
@bot.event
async def on_ready():
print(f'The bot is ready and logged in as {bot.user}')
@bot.command()
async def test(ctx):
await ctx.send('Test')
bot.run('YOUR_BOT_TOKEN_HERE')⚠️ 注意:
立即学习“Python免费学习笔记(深入)”;
- 不要写
discord.Intents.all()(除非你明确需要所有 intent 且已授权),它在未配置开发者门户时会直接报错; -
intents.message_content = True必须在discord.Intents.default()之后、传入Bot()之前设置; - 若使用
commands.Bot(...),无需再手动调用bot.add_cog()或注册事件——装饰器@bot.command()会自动注册。
2. 在 Discord 开发者门户启用对应权限
前往 Discord Developer Portal → 选择你的应用 → 左侧菜单进入 "Bot" → 向下滚动至 "Privileged Gateway Intents" 区域 → 开启 "Message Content Intent" 开关 ✅
? 此操作需你拥有服务器管理员权限,且该 Bot 必须以包含
message_content权限的方式重新邀请(推荐使用 OAuth2 URL Generator 生成带scope=bot&permissions=...的链接,确保勾选Send Messages和Read Message History等基础权限)。
? 验证是否生效:
- 启动机器人后,在 Discord 中发送
!test; - 若仍无响应,请检查终端是否有
Ignoring exception in command test类错误(说明命令已识别但执行出错); - 若完全静默(无日志、无报错、无回复),99% 是
message_contentintent 未启用(代码或门户任一环节遗漏)。
? 总结:message_content 是命令驱动型机器人的“听觉器官”——没有它,Bot 就像关闭了耳朵,再精准的 ! 前缀也无法被听见。务必同步完成代码赋值 + 门户开关 + 重新邀请三步,方可让命令真正“活起来”。


















