discord.js开发报EMBED_FIELD_NAME错误,字段非空却提示空如何解决
错误原因
- 方法参数不匹配:你使用的
addField()方法仅支持传入三个独立参数:字段名(字符串)、字段值(字符串)、是否行内显示(可选布尔值)。你直接向该方法传入对象,会导致discord.js把整个对象当作第一个参数(字段名)处理,校验字符串时不通过就抛出对应报错。 - 属性拼写错误:最后一个help字段的属性名
value被误写为valuse,即便后续修正方法调用,该错误也会导致字段值识别失败。 - 回复格式错误:
interaction.reply需要传入配置对象,嵌入消息必须放在embeds数组中,直接传递embed对象属于非法格式。
修复方案
两种修改方式二选一即可:
方式1:保留addField调用,调整参数格式
case 'help': let command = interaction.options.getString('cmd', false); if(command === null) try{ let embed = new Discord.MessageEmbed() .setTitle('Help') .setColor('ORANGE') .setFooter('Use /help cmd:<command> for more informations on that command!') .addField('kill', 'hi') // 拆分参数,去掉对象包裹 .addField('ping', 'Get the time delay between when you send the message and when the bot detects it.') .addField('snek', 'snek.') .addField('stalk', 'Get notified when the user whith the specified id logs in. Only works with this server\'s members.') .addField('oc get', 'Get infos about an oc. Needs to have fetched the oc to the database from the message beforehand. See /help cmd:ocmessage.') .addField('oc edit', 'Allows for the owner of the oc to edit in the database in case the data is wrong.') .addField('quote', 'Starts a quizz about a quote. Guess the character who said that quote!') .addField('fac, flip a coin', 'Flips a swiss coin. Warning: There is 1 in 100000000000000000 chance that the piece lands on its side. Be careful!') .addField('hybridgen', 'A hybrid generator for you!') .addField('oc message', 'Adds a message to the database') .addField('help', 'Shows this message!'); // 修正value拼写错误 interaction.reply({ embeds: [embed] }); // 修正回复格式 } catch (e) { console.warn(e); } }
方式2:改用addFields方法,保留对象写法
如果想继续用对象形式定义字段,把所有addField替换为单个addFields调用,传入所有字段对象即可:
case 'help': let command = interaction.options.getString('cmd', false); if(command === null) try{ let embed = new Discord.MessageEmbed() .setTitle('Help') .setColor('ORANGE') .setFooter('Use /help cmd:<command> for more informations on that command!') .addFields( // 替换为addFields,多对象传参 { name: 'kill', value: 'hi' }, { name: 'ping', value: 'Get the time delay between when you send the message and when the bot detects it.' }, { name: 'snek', value: 'snek.' }, { name: 'stalk', value: 'Get notified when the user whith the specified id logs in. Only works with this server\'s members.' }, { name: 'oc get', value: 'Get infos about an oc. Needs to have fetched the oc to the database from the message beforehand. See /help cmd:ocmessage.' }, { name: 'oc edit', value: 'Allows for the owner of the oc to edit in the database in case the data is wrong.' }, { name: 'quote', value: 'Starts a quizz about a quote. Guess the character who said that quote!' }, { name: 'fac, flip a coin', value: 'Flips a swiss coin. Warning: There is 1 in 100000000000000000 chance that the piece lands on its side. Be careful!' }, { name: 'hybridgen', value: 'A hybrid generator for you!' }, { name: 'oc message', value: 'Adds a message to the database' }, { name: 'help', value: 'Shows this message!' } // 修正拼写错误 ) interaction.reply({ embeds: [embed] }); // 修正回复格式 } catch (e) { console.warn(e); } }
内容的提问来源于stack exchange,提问作者Dråfølin
相关产品推荐
相关产品推荐

