【发布时间】:2021-02-19 15:30:17
【问题描述】:
我正在编辑嵌入消息,以通过反应将“注册”到列表的用户更新。但是,在我的数组长度达到 2 后,它会在添加新条目之前开始组合整个数组的字符串。这是我的代码:
let newParticipant = getNickname(guildMembers) || user.tag;
//this line exists because "value" cannot be empty in an embed field
//so this removes the placeholder on the first entry
if (newEmbed.fields[2].value[0] === "0") {
newEmbed.fields[2].value = [
`${newEmbed.fields[2].value.length}. ${newParticipant}`,
];
} else {
let participants = [newEmbed.fields[2].value];
let newEntry = participants.length + 1 + ". " + newParticipant;
participants.push(newEntry);
newEmbed.fields[2] = { name: "Participants", value: participants };
console.log(newEmbed.fields[2].value);
}
但是,这是我在 3 次反应后得到的输出:
[ '1. Cardinal' ]
[ '1. Cardinal', '2. Cardinal' ]
[ '1. Cardinal\n2. Cardinal', '2. Cardinal' ]
这是不和谐的东西吗?我的逻辑不好吗?在引入数组和其他一些东西时,我尝试使用扩展运算符...
【问题讨论】:
-
这是因为
newEmbed.fields[2].value返回 one 单个字符串。但是我不明白这会是什么问题,因为它标有换行符,因此您可以使用它。你试过做你想做的事吗? -
我试图让它与数组一起工作,这样我就可以逐渐增加数字,所以它是一个编号列表。
-
那么你可以分割你从嵌入中得到的字符串,这样你就有一个包含更多元素的数组
-
该解决方案有效!贴在下面。谢谢。 KISS 的好课
标签: javascript arrays discord.js