【问题标题】:[Discord.js]MessageEmbed field values must be non-empty strings[Discord.js]MessageEmbed 字段值必须是非空字符串
【发布时间】:2022-01-16 01:44:59
【问题描述】:

我有一些问题。
我花了三天时间试图解决这个问题,但我没有成功。
我需要你的帮助。

我的错误代码是

C:\Discord Bot\My BOT\node_modules\discord.js\src\util\Util.js:415
    if (typeof data !== 'string') throw new error(errorMessage);
                                        ^

RangeError [EMBED_FIELD_VALUE]: MessageEmbed field values must be non-empty strings.
    at Function.verifyString (C:\Discord Bot\My BOT\node_modules\discord.js\src\util\Util.js:415:41)
    at Function.normalizeField (C:\Discord Bot\My BOT\node_modules\discord.js\src\structures\MessageEmbed.js:518:19)
    at C:\Discord Bot\My BOT\node_modules\discord.js\src\structures\MessageEmbed.js:539:14
    at Array.map (<anonymous>)
    at Function.normalizeFields (C:\Discord Bot\My BOT\node_modules\discord.js\src\structures\MessageEmbed.js:538:8)
    at MessageEmbed.addFields (C:\Discord Bot\My BOT\node_modules\discord.js\src\structures\MessageEmbed.js:324:42)
    at MessageEmbed.addField (C:\Discord Bot\My BOT\node_modules\discord.js\src\structures\MessageEmbed.js:315:17)
    at C:\Discord Bot\My BOT\src\commands\vatsim\atc.js:57:6
    at processTicksAndRejections (node:internal/process/task_queues:96:5) {
  [Symbol(code)]: 'EMBED_FIELD_VALUE'
}

我的代码是

const Discord = require("discord.js")
const fetch = require('node-fetch');

module.exports.run = async (client, message, args) => {
    var ICAO = message.content.split(' ').slice(1).join(' ').toUpperCase()
    
    fetch(`https://data.vatsim.net/v3/vatsim-data.json`)
    .then(response => response.json())
    .then(data => {

    let atc = data.controllers.find(Data => Data.callsign === `${ICAO}`);
    let cid = atc.cid
    let name = atc.name
    let facility = atc.facility
    let callsign = atc.callsign
    let frequency = atc.frequency
    let rating = atc.rating
    let server = atc.server
    let range = atc.visual_range
    let atis = atc.text_atis
    let last_update = atc.last_updated
    let logon_time = atc.logon_time
    
    const ok = new Discord.MessageEmbed()
    .setColor('GREEN')
    .setTitle(`????️ **${ICAO} - ATC INFO**`)
    .addField('???? CID', cid, true)
    .addField('???? NAME', name, true)
    .addField('???? CALLSIGN', callsign, true)
    .addField('????️ FREQUENCY', frequency, true)
    .addField('✅ RATING', rating, true)
    .addField('???? FACILITY', `FACILITY : ${fac}`, true)
    .addField('????️ SERVER', `SERVER : ${server}`, true)
    .addField('????️ RANGE', `RANGE : ${range}`, true)
    .addField('✉️ TEXT ATIS', atis)
    .addField('⏲️ LOGON_TIME', logon_time, true)
    .addField('⏲️ LAST_UPDATE', last_update, true)
    .setFooter(`Requested by ${message.author.username}#${message.author.discriminator} • Via VATSIM API`);
    message.channel.send({embeds: [ok]})
    
        }).catch(e => {
            const fail = new Discord.MessageEmbed()
              .setColor('RED')
              .setTitle('❌ ERROR')
              .setDescription(`The controller you found ${ICAO} is not online. Please check the callsign again!`)
              .setFooter(`Requested by ${message.author.username}#${message.author.discriminator}`);

            return message.channel.send({embeds: [fail]})
    })
}

module.exports.config = {
    name: "atc",
    aliases: ['ATC']
}

我的 Discord.js 版本是 13.4.0 DEV

我尝试安装node-fetch ver 3.X 或以上,失败了。(使用Import)所以我目前使用的是2.6.1版本。

感谢您的帮助

【问题讨论】:

  • 您知道,在 StackOverflow 上,语法高亮使用标签来查找正确的语言。

标签: javascript discord.js


【解决方案1】:

将字段值转换为字符串

const ok = new Discord.MessageEmbed()
    .setColor('GREEN')
    .setTitle(`?️ **${ICAO} - ATC INFO**`)
    .addField('? CID', cid?.toString(), true)
    .addField('? NAME', name.toString(), true)
    .addField('? CALLSIGN', callsign.toString(), true)
    .addField('?️ FREQUENCY', frequency.toString(), true)
    .addField('✅ RATING', rating.toString(), true)
    .addField('? FACILITY', `FACILITY : ${fac.toString()}`, true)
    .addField('?️ SERVER', `SERVER : ${server.toString()}`, true)
    .addField('?️ RANGE', `RANGE : ${range.toString()}`, true)
    .addField('✉️ TEXT ATIS', atis.toString())
    .addField('⏲️ LOGON_TIME', logon_time.toString(), true)
    .addField('⏲️ LAST_UPDATE', last_update.toString(), true)
    .setFooter(`Requested by ${message.author.username}#${message.author.discriminator} • Via VATSIM API`);

【讨论】:

    【解决方案2】:

    提取的一些值是数字,但嵌入仅将非空 字符串 作为字段的 2 个第一个参数。

    您需要获取未连接到字符串的 2 个值的字符串,如下所示:

    const Discord = require("discord.js")
    const fetch = require('node-fetch');
    
    module.exports.run = async (client, message, args) => {
        var ICAO = message.content.split(' ').slice(1).join(' ').toUpperCase()
        
        fetch(`https://data.vatsim.net/v3/vatsim-data.json`)
        .then(response => response.json())
        .then(data => {
    
        let atc = data.controllers.find(Data => Data.callsign === `${ICAO}`);
        let cid = atc.cid
        let name = atc.name
        let facility = atc.facility
        let callsign = atc.callsign
        let frequency = atc.frequency
        let rating = atc.rating
        let server = atc.server
        let range = atc.visual_range
        let atis = atc.text_atis
        let last_update = atc.last_updated
        let logon_time = atc.logon_time
        
        const ok = new Discord.MessageEmbed()
        .setColor('GREEN')
        .setTitle(`?️ **${ICAO} - ATC INFO**`)
        .addField('? CID', String(cid), true) // Here
        .addField('? NAME', name, true)
        .addField('? CALLSIGN', callsign, true)
        .addField('?️ FREQUENCY', frequency, true)
        .addField('✅ RATING', String(rating), true) // Here
        .addField('? FACILITY', `FACILITY : ${fac}`, true)
        .addField('?️ SERVER', `SERVER : ${server}`, true)
        .addField('?️ RANGE', `RANGE : ${range}`, true)
        .addField('✉️ TEXT ATIS', atis)
        .addField('⏲️ LOGON_TIME', logon_time, true)
        .addField('⏲️ LAST_UPDATE', last_update, true)
        .setFooter(`Requested by ${message.author.username}#${message.author.discriminator} • Via VATSIM API`);
        message.channel.send({embeds: [ok]})
        
            }).catch(e => {
                const fail = new Discord.MessageEmbed()
                  .setColor('RED')
                  .setTitle('❌ ERROR')
                  .setDescription(`The controller you found ${ICAO} is not online. Please check the callsign again!`)
                  .setFooter(`Requested by ${message.author.username}#${message.author.discriminator}`);
    
                return message.channel.send({embeds: [fail]})
        })
    }
    
    module.exports.config = {
        name: "atc",
        aliases: ['ATC']
    }
    

    【讨论】:

    • 很遗憾,这没用。
    猜你喜欢
    • 2022-07-15
    • 2021-12-03
    • 2021-12-04
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 2021-09-04
    • 2021-11-23
    • 1970-01-01
    相关资源
    最近更新 更多