【问题标题】:How to split a richembed description into two messages?如何将richembed 描述拆分为两条消息?
【发布时间】:2019-07-19 01:30:41
【问题描述】:

如果描述大于 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)

提前致谢!

【问题讨论】:

    标签: javascript node.js discord.js


    【解决方案1】:

    首先将您的描述设置为变量。然后,您可以使用 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);  
    }
    

    【讨论】:

      猜你喜欢
      • 2017-01-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-15
      • 1970-01-01
      • 1970-01-01
      • 2012-07-02
      • 1970-01-01
      相关资源
      最近更新 更多