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

Discord.js 14.6.0:接收其他机器人Slash Command时内容与嵌入为空

问题解决:Discord.js 14.6.0 无法获取Slash命令触发的机器人消息内容/嵌入

问题原因

当用户通过Slash命令调用其他机器人时,对方机器人的响应消息通常是通过Webhook发送的。Discord网关在推送这类Webhook消息时,不会默认携带完整的content和embeds字段,导致你在messageCreate事件中拿到的消息对象这两个字段为空。你开启的MESSAGE CONTENT INTENT仅作用于普通用户消息和非Webhook发送的机器人消息,对Webhook消息无效。

解决方案

通过message.webhookId判断消息是否来自Webhook,若是则调用message.fetch()方法获取完整的消息数据,即可拿到正确的content和embeds。同时确保你的机器人拥有ReadMessageHistory权限,否则无法成功fetch消息。

修改后的代码示例

const { Client, GatewayIntentBits , Partials , EmbedBuilder } = require('discord.js');
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildMembers,
    ],
    partials: [Partials.Channel],
});

client.login(TOKEN);

client.on("ready", () => {
    console.log("Server is on!")
});

client.on("messageCreate", async (message) => {
    // 判断是否为Webhook消息(Slash命令的机器人响应通常属于此类)
    if (message.webhookId) {
        try {
            // 获取完整消息对象
            const fullMessage = await message.fetch();
            console.log('消息内容:', fullMessage.content);
            console.log('消息嵌入:', fullMessage.embeds);
        } catch (err) {
            console.error('获取完整消息失败:', err);
        }
    } else {
        // 普通消息处理逻辑
        console.log('消息内容:', message.content);
        console.log('消息嵌入:', message.embeds);
    }
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 19:10:36