【发布时间】:2021-05-22 16:51:37
【问题描述】:
我正在尝试在 DiscordJS 中获取超过 100 条消息。我找到了这段代码here,但它不起作用:
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.fetch(options);
sum_messages.push(...messages.array());
last_id = messages.last().id;
if (messages.size != 100 || sum_messages >= limit) {
break;
}
}
return sum_messages;
}
client.on("message", async message => {
const channel = client.channels.cache.get("12345");
if (message.content.startsWith(prefix+"random")){
console.log(lots_of_messages_getter());
}
});
它给了我这个错误:
(node:6312) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'fetch' of undefined
如何解决这个问题?我对 Node.js 有点陌生。
【问题讨论】:
-
您尝试从
channel读取fetch。channel是您的lots_of_messages_getter函数的参数,但是当您调用该函数时,您永远不会传递任何参数。你认为channel来自哪里?
标签: javascript node.js discord discord.js