Discord.js斜杠命令删除Embed失败:msg.delete不是函数
解决Discord.js斜杠命令
msg.delete is not a function报错 问题根源
- 参数类型错误:斜杠命令的
execute方法接收的是CommandInteraction对象,而非普通的Message对象,你错误地将参数命名为message,不仅导致用户属性引用逻辑混淆,更直接引发后续回复删除的API调用错误。 - 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
相关产品推荐
相关产品推荐

