如 cmets 中所述,您需要将大文本拆分为多个部分,然后您可以使用分页来循环浏览它们。
因此,为了完成这项工作,我们将从分割文本开始。一种方法是将整个文本拆分为一个数组,然后将该数组拆分为 2048 个字符的块。
const text = "some text"; // this is your large text.
const array = text.split(''); // here we split the text with each char being a new element.
const largeArray = []; // this is the array we will place the batches in.
var i, j, temparray, chunk = 2048; // here we define some variables and also the length of each batch.
// now we iterate over the array and split it into the defined batches
// then we join them back together and place them in the new array
for (i = 0, j = array.length; i < j; i += chunk) {
temparray = array.slice(i, i + chunk);
largeArray.push(temparray.join(''));
}
现在我们有一个数组,每个元素的长度不超过 2048 个字符。现在我们需要创建一个页面解析器。我们在这里这样做的方式是反应。所以首先我们需要创建一个函数来处理我们的反应。
// this needs to be an async function since we need to await the reactions
// using the author here ensures that only the person who sends the command
// can leaf through the pages
async function reactMessage(message, author, time, validReactions) {
// the time is converted to milliseconds
time *= 1000;
// now we cycle through the valid reactions and add them to the message
for (const reaction of validReactions) await message.react(reaction);
// now we use a reaction filter to grab the used reaction
const filter = (reaction, user) => validReactions.includes(reaction.emoji.name) && user.id === author.id;
// here we return the reaction used
return message
.awaitReactions(filter, { max: 1, time: time })
.then(collected => collected.first() && collected.first().emoji.name);
}
现在我们有一个函数来处理我们的反应,我们需要创建一个处理分页的函数。此函数将使用先前的函数来创建所需的反应。这是一个更大的,所以留在我身边。基本上,它的作用是获取一个迭代器和数组长度,以所需的反应对消息做出反应,然后返回迭代器以再次使用。
async function pageParser(message, msg, i, time, chooseArr, array_length) {
// create the reaction arrays for the first and last pages
const chooseArrfirst = [chooseArr[1], chooseArr[2]];
const chooseArrlast = [chooseArr[0], chooseArr[1]];
// this part handles the first page
if (i === 0) {
var reaction = await reactMessage(msg, message.author, time, chooseArrfirst);
if (reaction === chooseArr[2]) {
msg.delete();
i++;
return i;
} else if (reaction === chooseArr[1]) {
i = array_length + 1;
msg.delete();
return;
}
// this part handles the last page
} else if (i === array_length - 1) {
var reaction = await reactMessage(msg, message.author, time, chooseArrlast);
if (reaction === chooseArr[0]) {
msg.delete();
i--;
return i;
} else if (reaction === chooseArr[1]) {
i = array_length + 1;
msg.delete();
return;
}
// this part handles the pages in the middle
} else {
var reaction = await reactMessage(msg, message.author, time, chooseArr);
if (reaction === chooseArr[0]) {
msg.delete();
i--;
return i;
} else if (reaction === chooseArr[2]) {
msg.delete();
i++;
return i;
} else if (reaction === chooseArr[1]) {
i = array_length + 1;
msg.delete();
return;
}
}
}
太棒了!现在我们已经拥有了我们需要的所有组件,让我们将它们放在一起。
首先,我们创建迭代器和包含反应的数组。
var i = 0; // this is the iterator
var a = true;
const chooseArr = ['◀', '⏹', '▶']; // these are the reactions in order
现在我们使用while 循环来确保页面得到更新。还记得之前的largeArray 吗?现在我们再次需要它。我们创建一个嵌入并将描述设置为数组中的迭代器索引。这会更新描述。然后我们可以发送消息并使用我们的pageParser 函数。
while (a && i < largeArray.length) {
const embed = new Discord.MessageEmbed()
.setDescription(largeArray[i])
// since our function returns something, the iterator, we need to overwrite
// the previous iterator
i = await message.channel.send(embed).then(msg => pageParser(message, msg, i, 240000, chooseArr, largeArray.length))
}