問題描述
我正在嘗試找出一種方法,通過 fetchMesasges()
和 before 使用循環(huán)來獲取不和諧的舊消息.我想使用循環(huán)獲得超過 100 的限制,但我無法弄清楚,我能找到的每篇文章都只討論如何使用循環(huán)刪除超過 100 的限制,我只需要檢索它們.
I'm trying to figure out a way to use loops to get old messages on discord using fetchMesasges()
and before. I'd like to get more than the 100 limit using a loop but I cannot figure it out, and every post I can find only discuss how to use loops to DELETE more than the 100 limit, I just need to retrieve them.
我是編碼新手,尤其是 javascript,所以我希望有人可以幫助我朝著正確的方向前進(jìn).
I'm new to coding and javascript in particular so I'm hoping someone can give me a nudge in the right direction.
這是我能夠設(shè)法檢索超過 100 條消息的唯一方法(在多次嘗試使用循環(huán)失敗之后):
Here is the only way I could manage to retrieve messages that are farther than 100 back(after many failed attempts at using loops):
channel.fetchMessages({ limit: 100 })
.then(msg => {
let toBeArray = msg;
let firstLastPost = toBeArray.last().id;
receivedMessage.channel
.fetchMessages({ limit: 100, before: firstLastPost })
.then(msg => {
let secondToBeArray = msg;
let secondLastPost = secondToBeArray.last().id;
receivedMessage.channel
.fetchMessages({ limit: 100, before: secondLastPost })
.then(msg => {
let thirdArray = msg;
let thirdLastPost = thirdArray.last().id;
receivedMessage.channel
.fetchMessages({ limit: 100, before: thirdLastPost })
.then(msg => {
let fourthArray = msg;
});
});
});
});
推薦答案
你可以做的是使用 async/await 函數(shù)和一個循環(huán)來發(fā)出順序請求
What you can do is use an async/await function and a loop to make sequntial requests
async function lots_of_messages_getter(channel, limit = 500) {
const sum_messages = [];
let last_id;
while (true) {
const options = { limit: 100 };
if (last_id) {
options.before = last_id;
}
const messages = await channel.fetchMessages(options);
sum_messages.push(...messages.array());
last_id = messages.last().id;
if (messages.size != 100 || sum_messages >= limit) {
break;
}
}
return sum_messages;
}
這篇關(guān)于獲取超過 100 條消息的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!