-1

I’m trying to work out how to split a RichEmbed into two separate messages if the description is larger than 2048 characters.

let embed = new RichEmbed()
                .setColor(cyan)
                .setAuthor(`Urban Dictionary | ${word}`, image)
                .setThumbnail(image)
                .setDescription(stripIndents`**Definition:** 
                ${definition || "No definition"}
                **Example:** 
                ${example || "No example"}
                **Upvotes:** ${thumbs_up || 0}
                **Downvotes:** ${thumbs_down || 0}
                **Link:** [Link to ${word}](${permalink || "https://www.urbandictionary.com/"})`)
                .setFooter(`Requested by: ${message.author.tag} || Author: ${author || "Unknown"}`, message.author.avatarURL)
                .setTimestamp()

                message.channel.send(embed)

Thanks in advance!

R.J. Dunnill
  • 2,049
  • 3
  • 10
  • 21
biscot
  • 37
  • 8

1 Answers1

0

Start by setting your description as a variable. Then, you can use the regex from this answer to split the description up into segments 2,048 characters long. Finally, iterate through the resulting array and send an embed with each segment of the original string.

Example code:

const description = 'Lorem ipsum dolor sit amet.';
const split = description.match(/[\s\S]{1,2048}/g);

for (let i = 0; i < split.length; i++) {
  let embed = new RichEmbed()
    .setDescription(split[i]);

  await message.channel.send(embed) // Async context needed to use 'await.'
    .catch(console.error);  
}
slothiful
  • 5,548
  • 3
  • 12
  • 36