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

Discord.js斜杠命令删除Embed失败:msg.delete不是函数

解决Discord.js斜杠命令msg.delete is not a function报错

问题根源

  1. 参数类型错误:斜杠命令的execute方法接收的是CommandInteraction对象,而非普通的Message对象,你错误地将参数命名为message,不仅导致用户属性引用逻辑混淆,更直接引发后续回复删除的API调用错误。
  2. API使用错误:Discord.js v14中,interaction.reply()返回的是CommandInteractionReply实例,而非Message实例,这个对象没有delete()方法,必须使用对应交互的删除API。

修正后的完整代码

const { SlashCommandBuilder, ActionRowBuilder, ButtonBuilder, EmbedBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('br')
        .setDescription('Battle to the death with your friends'),
    async execute(interaction) {
        const row = new ActionRowBuilder()
            .addComponents(
                new ButtonBuilder()
                    .setCustomId('joinbutton')
                    .setLabel('JOIN BATTLE ROYALE')
                    .setStyle(1),
            );
            
        const embed = new EmbedBuilder()
            .setTitle(`BATTLE ROYALE STARTING`)
            .setColor("0090FF")
            .setDescription(`<@${interaction.user.id}> is starting a battle royale match. Use the button below to join!`)
            .setFooter({ text: interaction.user.username, iconURL: interaction.user.displayAvatarURL() })
            .setTimestamp();

        // 发送回复后延迟5秒删除
        await interaction.reply({ embeds: [embed], components: [row] });
        setTimeout(() => interaction.deleteReply(), 5000);
    },
};

关键修改点

  • 修正参数名:将execute的参数从message改为interaction,贴合Discord.js斜杠命令的交互逻辑。
  • 更新用户属性引用:所有message.user替换为interaction.user,并使用displayAvatarURL()替代avatarURL()(v14版本官方推荐用法)。
  • 替换删除逻辑:使用interaction.deleteReply()方法删除回复,配合setTimeout实现5秒延迟,这是删除斜杠命令回复的标准方式。

可选方案(获取消息实例后删除)

如果需要对回复消息做额外操作,可以先获取消息实例再执行删除:

await interaction.reply({ embeds: [embed], components: [row] });
const replyMessage = await interaction.fetchReply();
setTimeout(() => replyMessage.delete(), 5000);

注意事项

  • 确保机器人拥有ManageMessages权限,否则无法执行删除操作。
  • 所有异步操作需使用await保证执行顺序,避免逻辑混乱。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.19 07:25:19