让过去的消息停止工作的函数.有办法解决吗?



我从另一个stackoverflow问题中得到了这个函数。我通过将channel.fetchMessages更改为channel.messages.fetch来修复它与Discord.js v12一起工作。该函数一开始工作,一切都很好,但是有一次当我启动我的程序时,它开始显示这个错误:&;TypeError:无法读取属性'id'的未定义&;这个错误发生在第55行,这是last_id = messages.last().id;,我根本没有改变函数,它只是停止工作。什么好主意吗?

async function lots_of_messages_getter(channel, limit = 6000) {
const sum_messages = [];
let last_id;
while (true) {
const options = { limit: 100 };
if (last_id) {
options.before = last_id;
}
const messages = await channel.messages.fetch(options);
sum_messages.push(...messages.array());
last_id = messages.last().id;
if (messages.size != 100 || sum_messages >= limit) {
break;
}
}
return sum_messages;
}

因为sum_messages永远不会大于或等于limit,因为它不是一个数字,它必须是sum_messages.length,并且在获得消息后检查if(messages.size === 0)也不会伤害

async function lots_of_messages_getter(channel, limit = 6000) {
const sum_messages = [];
let last_id;
while (true) {
const options = { limit: 100 };
if (last_id) {
options.before = last_id;
}
const messages = await channel.messages.fetch(options);
if (messages.size === 0) {
break;
}
sum_messages.push(...messages.array());
last_id = messages.last().id;
if (messages.size != 100 || sum_messages.length >= limit) {
break;
}
}
return sum_messages;
}

最新更新