discord.js embeds: UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body embed.fields[0].value: 长度必须小于等于 1024

时间:2021-01-08 13:33:43

标签: javascript node.js discord.js

我正在创建一个不和谐机器人,它将从网站获取数据,并将其显示在嵌入中。 我将数据存储在名为 boxArr 的数组中。然后我迭代它,临时存储数据,并将其分配给正确的嵌入字段的属性(即标题和值)。
它适用于某些页面,但不适用于其他页面。每当失败时,我都会得到一个

[UnhandledPromiseRejectionWarning: DiscordAPIError: Invalid Form Body embed.fields[0].value: Must be 1024 or fewer in length.] 警告。
然而,嵌入值的长度永远不会超过 1024 个字符,所以我很困惑为什么我会收到这个警告。

这是我的代码:

//Create the embed
var exampleEmbed = new Discord.MessageEmbed()
    .setColor('#2D2D2D')
    .setTitle(str)
    .setURL('/*page link here*/'+str)
    .setThumbnail(th)
    .addField('‌‌', def + `[Read More...](/*page link here*/${str})`);

    //Variable to hold the decision if titles are even or odd
    var toOdd = true;
    for(i = 0; (boxArr[i] != undefined || boxArr[i] != null); i++) {
        if(boxArr[i] === '' || boxArr[i].startsWith('"')) {
            if(isOdd(i) === 1) { //If boxArr[i] is nothing and i is odd, titles' location is when i is even
                toOdd = false;
                continue;
            } else { //If boxArr[i] is nothing and i is even, titles' location is when i is odd
                toOdd = true;
                continue;
            }
        }



        if(toOdd) {
            if(isOdd(i) === 1) {
                title = boxArr[i]; //Assign title its value
                i++; //Increment i
                val = boxArr[i]; 
   
                if(val === undefined || title === undefined) { //Check if one of the two is undefined. If yes then break out without tampering with the embed.
                  break;
                }
                Embed.addFields({name: title, value: val, inline: true});
                if(boxArr[i+1] === undefined) { //This is unnecessary but I added it nevertheless
                    break;
                }
                continue;  
            }
        } else if(!toOdd) { //Similar to if toOdd === true
            if(isOdd(i) != 1) {
                title = boxArr[i];
                i++;
                console.log('\n\nVALUE: '+ boxArr[i]);
                val = boxArr[i];
                if(val === undefined || title === undefined) {
                    break;
                }

                Embed.addFields({name: title, value: val, inline: true});
                if(boxArr[i+1] === undefined) {
                    break;
                }
                continue;  
            }
        }
                
    }
    
    message.channel.send(Embed); //send the embed

感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

所以我发现了问题。
显然问题在于嵌入本身有超过 1024 个字符。
[Read More...](/*page link here*/${str}) 中删除 .addField('‌‌', def + [Read More...](/*page link here*/${str})); 解决了问题。

编辑:另一个修正是将 str 限制为 500 个字符。