discord.js v14.7.1创建Embed时出现类继承TypeError错误
修复discord.js v14.7.1中/help斜杠命令的错误
错误原因
Command类不存在:discord.js v14没有内置的Command基类,你之前的继承写法是旧版本第三方框架(比如discord-akairo)的用法,直接导入会得到undefined,导致继承报错。MessageEmbed已被移除:v14中MessageEmbed替换为EmbedBuilder。- 消息发送格式变更:发送Embed时不再使用
embed字段,而是用embeds数组包裹。 - 斜杠命令定义方式更新:v14通过
SlashCommandBuilder来定义斜杠命令,而非自定义类继承。
修复后的完整代码
const { SlashCommandBuilder, EmbedBuilder } = require('discord.js'); module.exports = { // 定义斜杠命令的基本信息 data: new SlashCommandBuilder() .setName('help') .setDescription('List all available commands.'), // 执行命令的逻辑 async execute(interaction) { const helpEmbed = new EmbedBuilder() .setColor('#F8F7D8') .setTitle('TITLE') .setURL('URL') .setAuthor({ name: 'NAME', iconURL: 'URL', url: 'URL', }) .setDescription('Commands for NAME') .setThumbnail('URL') .addFields( { name: '/play', value: 'Used to play the music' }, { name: '\u200B', value: '\u200B' }, { name: '/pause', value: 'Used to pause the music', inline: true }, { name: '/mp3', value: 'Used to convert a youtube link to an mp3', inline: true }, { name: '/skip', value: 'Used to skip the music', inline: true } ) .setImage('URL') .setTimestamp() .setFooter({ text: 'NAME', iconURL: 'URL', }); // 回复斜杠命令交互,发送Embed await interaction.reply({ embeds: [helpEmbed] }); }, };
关键修改说明
- 用
SlashCommandBuilder替代自定义类,正确定义斜杠命令的名称和描述。 - 替换
MessageEmbed为EmbedBuilder,适配v14的API。 - 将
message.channel.send({ embed: help })改为interaction.reply({ embeds: [helpEmbed] }),因为斜杠命令的交互对象是Interaction而非普通Message,且v14要求Embed放在数组中。
内容的提问来源于stack exchange,提问作者Christian
相关产品推荐
相关产品推荐

