Discord bot 嵌入消息错误长度必须为 2048 或更少

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

当我 运行 用于在 discord bot 中嵌入消息的描述的代码时出现错误。

"embed.description:长度必须为 2048 或更少。"

内容取自API,可以超过6000条。如何根据取自API的数据制作2或3个不同描述的嵌入消息?

虽然 channel#send() 方法接受 options object,您可以在其中将 split 属性 设置为 true 以将内容拆分为多条消息,如果它超出字符数限制,setDescription 没有。

这意味着,如果你想在嵌入的描述中包含它们,你需要创建这些“块”并将它们一个一个地发送。

您可以创建自己的方法或... Discord 有一个名为 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 的辅助方法,并将 char 更改为单个 space(即 splitMessage(text, { char: ' ' }) ).

要创建区块,您可以使用以下方法:

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

它 returns 一个数组,因此您可以迭代结果。查看下面的工作代码:

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

如果您使用的是简单的嵌入对象,则需要像这样更新:

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!',
    },
  });
}