查找最活跃的用户(每日)



我正试图向我的discord bot添加一个命令,以查找当天最活跃的用户。我可以保存聊天日志并搜索它们,但我确信有一种更简单的方法。

假设每个消息都被缓存,则可以使用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]

最新更新