如何将丰富的描述分为两个消息?

时间:2019-07-18 10:55:39

标签: javascript node.js discord.js

如果描述超过2048个字符,我正在尝试找出如何将RichEmbed拆分为两个单独的消息。

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)

谢谢!

1 个答案:

答案 0 :(得分:0)

首先将描述设置为变量。然后,您可以使用this answer中的正则表达式将说明分成长度为2,048个字符的段。最后,遍历结果数组,并在原始字符串的每个段中发送一个嵌入。

示例代码:

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);  
}