Discord.js 14 Slash命令createEmbed报错ExpectedConstraintError求助
解决Discord.js 14 Slash命令格式错误问题
错误原因分析
从报错信息里的正则规则/^[\p{Ll}\p{Lm}\p{Lo}\p{N}\p{sc=Devanagari}\p{sc=Thai}_-]+$/u可以明确看出,Discord对Slash命令名称有强制格式要求:
- 必须全部使用小写字母
- 仅允许包含小写字母、数字、下划线
_、连字符-
你的代码里命令名createEmbed包含大写字母E,直接违反了这个规则,这是触发ExpectedConstraintError的核心原因。另外还有一个拼写错误:excute应该是execute,虽然这不是当前报错的原因,但后续运行会导致方法无法被正确调用,需要一并修正。
修复步骤
修正命令名称
将.setName("createEmbed")修改为符合格式的名称,比如.setName("create_embed")或.setName("createembed")。修正方法拼写
把async excute (interaction, client)改为async execute (interaction, client)。
修复后的完整代码
const { SlashCommandBuilder, CommandInteraction, EmbedBuilder, Client } = require('discord.js'); module.exports = { data: new SlashCommandBuilder() .setName("create_embed") .setDescription("Create new test Embed!"), /** * * @param {CommandInteraction} interaction * @param {Client} client */ async execute (interaction, client) { const exampleEmbed = new EmbedBuilder() .setColor(0x0099FF) .setTitle('Some title') .setAuthor({ name: 'Some name' }) .setDescription('Some description here') .addFields( { name: 'Regular field title', value: 'Some value here' }, { name: '\u200B', value: '\u200B' }, { name: 'Inline field title', value: 'Some value here', inline: true }, { name: 'Inline field title', value: 'Some value here', inline: true }, ) .addFields({ name: 'Inline field title', value: 'Some value here', inline: true }) .setTimestamp() .setFooter({ text: 'Some footer text here' }); await interaction.reply({ embeds: [exampleEmbed] }); }, };
后续操作
修改完成后,重新部署你的Slash命令:如果是全局命令需要等待Discord缓存刷新(通常1小时内),如果是服务器专属命令则重新同步到目标服务器,之后即可正常使用该命令创建Embed。
内容的提问来源于stack exchange,提问作者감기여요
相关产品推荐
相关产品推荐

