embed.fields[1].value: Must be 1024 or fewer in length. embed.fields[2].value: Must be 1024 or fewer in length

Viewed 3851

I am creating an event for messageUpdate, and everything is working "perfectly" fine, except when someone sends a message that contains more than 1024 characters, it returns an error as addField has a character limit.
How could I possibly make the bot send an error if the message that has been edited has reached the limit ?

if(oldMessage.cleanContent.length >= 1000) return channel.send("Too long");
if(newMessage.cleanContent.length >= 1000) return channel.send("Too long");

const embed = new Discord.MessageEmbed()
   .addField("Old Message", `${oldMessage.cleanContent}`, true)
   .addField("New Message", `${newMessage.cleanContent}`, true)
channel.send(embed)
2 Answers

(Posting this message so the question can get answered)

Judging from what you posted in the comments, this should be right. (I'm not the greatest dev so, feel free to yell at me if it's wrong)

const oldMessage = oldMessage.cleanContent
const newMessage = newMessage.cleanContent

if (oldMessageContent.length >= 1000) return channel.send("Too long");
if (newMessageContent.length >= 1000) return channel.send("Too long");

const embed = new Discord.MessageEmbed()
   .addField("Old Message", `${oldMessage.cleanContent}`, true)
   .addField("New Message", `${newMessage.cleanContent}`, true)
channel.send(embed)

You can also use a loop to add more fields for every 1024 characters

Example:

for (let i = 0; i < oldMessage.cleanContent.length; i += 2000) {
    const cont = oldMessage.cleanContent.substring(i, Math.min(oldMessage.cleanContent.length, i + 2000));
    embed.addField("Old Message", cont);
}

for (let i = 0; i < newMessage.cleanContent.length; i += 2000) {
    const cont = newMessage.cleanContent.substring(i, Math.min(newMessage.cleanContent.length, i + 2000));
    embed.addField("New Message", cont);
}
Related