Search code examples
javascriptnode.jsdiscord.jsbotsembed

Discord bot embed msg error Must be 2048 or fewer in length


I am getting an error when I run the code which is for an embed message's description in discord bot.

"embed.description: Must be 2048 or fewer in length."

The content is taken from an API and it can be more than 6000. How can I make 2 or 3 embed message with different description depending on the data taken from API?


Solution

  • While the channel#send() method accepts an options object where you can set the split property to true to split the content into multiple messages if it exceeds the character limit, setDescription doesn't have it.

    It means, you need to create these "chunks" and send them one by one if you want to have it in the embed's description.

    You can either create your own method or... Discord has a helper method named splitMessage() inside Util that you can use to split a string into multiple chunks at a designated character that do not exceed a specific length. The character it splits by is \n by default. If your huge text doesn't have any newline characters, you will need to update the SplitOptions and change the char to a single space (i.e. splitMessage(text, { char: ' ' })).

    To create the chunks, you can use the following:

    const chunks = Discord.Util.splitMessage(prettyLongText);
    

    It returns an array, so you can iterate over the results. Check out the working code below:

    const chunks = Discord.Util.splitMessage(texts[args[0]]);
    const embed = new Discord.MessageEmbed().setTitle(`Split me!`);
    
    if (chunks.length > 1) {
      chunks.forEach((chunk, i) =>
        message.channel.send(
          embed
            .setDescription(chunk)
            .setFooter(`Part ${i + 1} / ${chunks.length}`),
        ),
      );
    } else {
      message.channel.send(embed.setDescription(chunks[0]));
    }
    

    enter image description here

    If you're using a simple embed object, you need to update like this:

    const chunks = Discord.Util.splitMessage(texts[args[0]]);
    
    if (chunks.length > 1) {
      chunks.forEach((chunk, i) =>
        message.channel.send(
          {
            embed: {
              color: 3447003,
              description: chunk,
              footer: {
                text: `Part ${i + 1} / ${chunks.length}`,
              },
              title: 'Split me!',
            },
          }
        ),
      );
    } else {
      message.channel.send({
        embed: {
          color: 3447003,
          description: chunks[0],
          title: 'Split me!',
        },
      });
    }