[英]How can I get the last 1000 messages sent by a certain user?
I've tried channel.fetchMessages()
, but the limit is 100 messages.我试过
channel.fetchMessages()
,但限制是 100 条消息。 It's for a bot supposed to simulate users using machine learning.它适用于应该使用机器学习来模拟用户的机器人。
Taking into account what @jsejcksn is mentioning about the rate limits and how you can fetch the channel messages, here is a working implementation of it:考虑到@jsejcksn 提到的速率限制以及如何获取通道消息,这是它的工作实现:
Example Implementation![]() |
Run in Fusebit ![]() |
---|
let messagesCounter = 0;
const channel = await findChannel(discordClient);
// Channel messages are limited up to 100 messages per request.
const channelMessages = await getMessages(discordClient, channel.id);
// Accumulate fetched messages
let fetchedMessages = channelMessages;
// Fetch discord until you get MAX_NUMBER_OF_MESSAGES (1000)
let newMessages = channelMessages;
// If messages are returned, keep getting more until having 1000 messages
while (newMessages.length && messagesCounter < MAX_NUMBER_OF_MESSAGES) {
// Keep a counter to prevent an infinite loop
messagesCounter += channelMessages.length;
// Using the before filter, you can get oldest messages by using the last returned message
const oldestMessage = newMessages[channelMessages.length - 1];
newMessages = await getMessages(discordClient, channel.id, oldestMessage.id);
fetchedMessages = fetchedMessages.concat(newMessages);
}
// Return all the channel messages (up to 1000 messages!)
ctx.body = `There are ${fetchedMessages.length} messages in the ${DISCORD_CHANNEL_NAME} channel`;
Based on the returned messages, you can easily filter by user id根据返回的消息,您可以轻松地按用户 ID 过滤
The limit of returned messages is
100
, with the default being50
.返回消息的限制为
100
条,默认为50
条。 See the actual API docs for details.有关详细信息,请参阅实际的API 文档。
By default, you get the most recent messages, but you can specify additional query criteria when fetching messages .默认情况下,您会获取最新消息,但您可以在获取消息时指定其他查询条件。 You'll need to repeat your fetch requests, adjusting the query criteria each time, in order to collect successively older messages.
您需要重复获取请求,每次调整查询条件,以便连续收集较旧的消息。 After each request, you can analyze what you've collected so far to determine if you need to keep fetching or not.
在每个请求之后,您可以分析到目前为止收集的内容,以确定是否需要继续获取。
Beware of the rate limits .当心速率限制。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.