简体   繁体   English

如何获取某个用户发送的最后 1000 条消息?

[英]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 在 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 being 50 .返回消息的限制为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.

相关问题 如何防止用户超出JSXGraph中的某些坐标? - How can I prevent user to get outside certain coordinates in JSXGraph? 如何将特定用户发送的消息存储在discordjs中? - How to store messages sent by specific user in discordjs? 我怎样才能让这个滑块在一定的延迟下自动循环? IE。 1000ms - How can I make this slider loop automatically with a certain delay? ie. 1000ms 发送消息后如何通知用户 - How can I notify the user when I sent the message 如何动态获取最后 1000 行并使用 GAS 对它们进行排序? - How to get the last 1000 rows dynamically and sort them using GAS? 如何获取发送邮件的用户的ID? - How do I get the ID of a user that sent the message? 如何强制某些字符串在javascript中排在最后? - How can I force certain strings to be sorted last in javascript? 如果最后一个字符是某个字符,如何将其包装在元素中? - How can I wrap the last character in an element if it's a certain character? 当用户提出某些请求时,我如何获得我的 Alexa 技能来发送 Gmail? - How Can I Get My Alexa Skill To Send Gmails When The User Makes Certain Requests? 我将如何 go 关于编辑 aa discord 机器人发送的消息? 但只有当具有特定角色的用户做出反应时? - How would I go about editing a message sent by a a discord bot? But only when reacted to by a user with a certain role?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM