【发布时间】:2021-08-12 16:34:14
【问题描述】:
我在运行用于在 discord bot 中嵌入消息描述的代码时遇到错误。
“embed.description:长度不得超过 2048。”
内容取自一个API,可能超过6000条。如何根据取自API的数据制作2或3条不同描述的嵌入消息?
【问题讨论】:
标签: javascript node.js discord.js bots embed
我在运行用于在 discord bot 中嵌入消息描述的代码时遇到错误。
“embed.description:长度不得超过 2048。”
内容取自一个API,可能超过6000条。如何根据取自API的数据制作2或3条不同描述的嵌入消息?
【问题讨论】:
标签: javascript node.js discord.js bots embed
虽然channel#send() 方法接受options object,您可以将split 属性设置为true,以便在超过字符限制时将内容拆分为多条消息,而setDescription 没有它。
这意味着,如果您想在嵌入的描述中包含这些“块”并一一发送。
您可以创建自己的方法或... Discord 在Util 内有一个名为splitMessage() 的辅助方法,您可以使用该方法将字符串拆分为不超过特定长度的指定字符的多个块。默认情况下,它分割的字符是\n。如果您的大文本没有任何换行符,您需要更新SplitOptions 并将char 更改为一个空格(即splitMessage(text, { char: ' ' }))。
要创建块,您可以使用以下内容:
const chunks = Discord.Util.splitMessage(prettyLongText);
它返回一个数组,因此您可以遍历结果。查看下面的工作代码:
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!',
},
});
}
【讨论】:
embed: { Title: "Session Details", color: 3447003, description: ${msg1}, 那么我如何在这里设置 foofter,我使用了这个`setFooter:Part ${i + 1} / ${s_str.length}`但是什么都没有
text 属性的footer 对象。我刚刚更新了上面的答案,请检查一下。