javascript - 查找最活跃的用户(每天)
问题描述
我正在尝试向我的不和谐机器人添加一个命令,以查找当天最活跃的用户。我可以保存聊天记录并通过它们进行搜索,但我很确定有一种更简单的方法。
解决方案
假设每条消息都被缓存,您可以使用Collection.prototype.reduce()
const object = guild.members.cache.reduce(
(acc, member) => (
// add up the sum of messages sent in each channel by this member and add it to the object
(acc[member.id] = guild.channels.cache.reduce((acc, channel) =>
// filter only messages sent by the current member
acc + channel.messages.cache.filter(
(message) =>
message.author === member &&
// make sure the message was sent within the last day
message.createdTimestamp > new Date(new Date().getDate() - 1)
).size, 0
)),
acc
),
{}
);
// sort the object and return the ID of the most active member
const mostActive = Object.entries(object).sort(([, a], [, b]) => b - a)[0][0]
推荐阅读
- ios - 当应用程序在后台时,显示带有 NotificationHub 的 Xamarin iOS 通知而不调用 DidReceiveRemoteNotification
- wordpress - 将所有子 url 重定向到主 url
- vb.net - 运行时错误 System.Security.Cryptography.CryptographicException:'错误数据。' 在 vb.net 中尝试导入 RSA 参数时
- symfony - 仅针对某些实体的 Log Doctrine Insert/Update Statements
- php - 为什么php还是报错?
- c++ - 标准 - 包括
或创建我自己的定义会导致编译时出现 C2953 错误 - python - 嵌套字典到具有属性的类似对象的字典
- java - 模拟已标记为可访问的私有方法的返回值
- c# - 删除 Autofac 后无法在运行时加载文件或程序集错误,但与 Assembly.Load('"MyCompany.MyAdaptoer") 一起工作正常
- google-apps-script - Google Sheets Webhooks 1 周后停止工作