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

Discord.js消息收集器获取内容为空字符串问题求助

解决Discord.js v14中消息收集器获取空内容的问题

核心原因:缺失Message Content Intent权限

Discord.js v14开始,获取消息内容必须明确申请MessageContent权限,否则message.content会返回空字符串。

步骤1:在Discord开发者平台开启权限

进入你的机器人应用页面,找到Privileged Gateway Intents,开启Message Content Intent选项。

步骤2:在代码中声明该Intent

初始化Client时必须包含MessageContent Intent:

const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
  intents: [
    GatewayIntentBits.Guilds,
    GatewayIntentBits.GuildMessages,
    GatewayIntentBits.MessageContent, // 关键:添加这个Intent
    GatewayIntentBits.DirectMessages // 如果需要处理DM消息则添加
  ]
});

优化收集器过滤逻辑

为了避免收集到无关消息(比如机器人自己的回复、其他用户的消息),建议给收集器添加过滤条件,只收集命令发起者的消息:

const collector = interaction.channel.createMessageCollector({
  filter: m => m.author.id === interaction.user.id, // 仅收集当前命令用户的消息
  time: 15000
});

额外检查项

  • 确认你的Discord.js版本为v14.x,低版本Intent机制不同
  • 确保机器人在目标频道拥有读取消息历史和发送消息的权限

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 08:54:25