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

discord.js开发机器人发送Embed后报错,程序无响应求助

Discord.js发送Embed后报错且程序无响应的问题解决

我想要编写一段代码,实现发送包含所有命令的Embed消息。代码可以运行,但发送Embed后,消息上方会显示报错,且程序出现无响应的情况。

以下是我的代码:

const { Client, GatewayIntentBits, CommandInteraction, EmbedBuilder } = require('discord.js')
const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        // ...
    ]
    
});

client.on('ready', async () => {
    console.log(`Logged in as ${client.user.tag}!`);

    const commands = [
        {
            name: 'pomoc',
            description: 'Wyświetla wszystkie komendy'
        }
    ];

    const commandData = commands.map(command => ({
        name: command.name,
        description: command.description
    }));

    const commandManager = await client.application?.commands.set(commandData);
    console.log(`Registered ${commandManager.size} slash commands`);
});

client.on('interactionCreate', interaction => {
    if (!interaction.isCommand()) return;

    if (interaction.commandName === 'pomoc') {
const embed = new EmbedBuilder()
  .setTitle('some title')
  .setDescription('some description')


interaction.channel.send({embeds: [embed]})


    }
});

client.login(process.env.CLIENT_TOKEN);

我还尝试了以下写法:

interaction.channel.send({
  embeds: [{
    title: 'some title',
    description: 'some description',
    image: {url: 'image url'}
  }]
})

问题原因

核心问题是未按Discord要求对Slash命令交互进行响应。Discord规定Slash命令必须在3秒内通过interaction.reply()或interaction.deferReply()完成响应,否则会触发"应用无响应"的报错。你直接使用interaction.channel.send()发送消息,但没有完成Discord要求的交互响应流程,导致平台判定应用无响应,同时未处理的Promise可能引发程序阻塞。

解决方案

1. 直接用interaction.reply()发送Embed

将发送逻辑改为使用interaction.reply(),同时把事件处理函数改为异步函数以支持await:

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    if (interaction.commandName === 'pomoc') {
        const embed = new EmbedBuilder()
            .setTitle('some title')
            .setDescription('some description');

        await interaction.reply({ embeds: [embed] });
    }
});

2. 先延迟响应再发送额外消息

如果需要在频道发送独立消息而非直接回复交互,可以先调用deferReply()告知Discord已收到请求,再执行后续操作:

client.on('interactionCreate', async interaction => {
    if (!interaction.isCommand()) return;

    if (interaction.commandName === 'pomoc') {
        // 先延迟响应,避免超时报错
        await interaction.deferReply({ ephemeral: false });
        
        const embed = new EmbedBuilder()
            .setTitle('some title')
            .setDescription('some description');

        // 在频道发送Embed
        await interaction.channel.send({ embeds: [embed] });
        
        // 可以编辑延迟的响应消息
        await interaction.editReply('已为你展示所有命令');
    }
});

注意事项

  • 所有异步操作必须添加await,避免Promise未处理导致的程序异常。
  • 确保你的机器人拥有发送消息和嵌入内容的频道权限。

内容的提问来源于stack exchange,提问作者Masłosz S

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.30 05:22:39