Discord.js - 让用户最后一次活动? [英] Discord.js - Getting users last activity?

查看:25
本文介绍了Discord.js - 让用户最后一次活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试找出是否可以使用 discord.js

I'm trying to find out if its possible to get the time/information of users last activity retrospectively using discord.js

说我有类似的东西

  client.guilds.find('id', 'SERVER ID').fetchMembers().then(members => {
        const role = members.roles.find('name', 'Newbies')

        role.members.forEach(member => {
              console.log(member.user.lastMessage) // null
        })
  })

除非会员已经发帖,因为客户端在监听,所以lastMessage始终为null.

Unless the member has posted, since the client is listening, the lastMessage is always null.

有没有办法找到最后一个活动?还是一种解决方法,例如返回所有用户消息的查询,然后我可以从中获取最新消息?

Is there a way to find the last activity? or a workaround, like a query to return all the users messages, which I can then take the most recent one from?

实际上,我想知道用户上次发布的日期/时间,以便我们可以监控非贡献帐户.

Effectively I want to know what date/time the user last posted so we can monitor non-contributing accounts.

谢谢

推荐答案

查看文档后,我也没有找到任何东西,所以我想出了一个手动搜索功能.

After looking thought the documentation I didn't find something neither so I came up with a manual search function.

基本上,它会扫描每个频道,直到找到来自 X 用户的消息,或者频道中的消息结束.然后它会比较来自每个渠道的用户的最后一条消息并打印最后一条.

Basically, it will scan every channels until finding a message from X user, or the end of the messages in the channel. It then compare the last messages of the users from every channels and print the last one.

如果用户很久没有写,它可能会很长.当然,您必须在尝试之前检查 lastMessage.

It can be very long if the user hasn't write since a long time. Of course, you have to check lastMessage before trying this.

我可能会添加一个时间限制.因为如果您有数千条消息,该函数将永远运行.

I would add a time limit maybe. Because if you have thousand of messages, the function will run eternally.

如果找到的最后一条消息在接受的时间内不被踢/做任何事情,您可以停止该功能.

You can stop the function if the last message found is in the accepted time to not be kick/do whatever.

如果在 fetched messaged 包中找到的第一条消息超过了封禁限制,我就停止了搜索,但是,如果第一条消息不旧,请记住它意味着另一个,所以我们仍然需要检查它们(也可以通过检查包的最后一条消息来避免).

I made the search stop if the first message found in the pack of fetched messaged is older than the ban limit, however, if the first message is not older, remember that it means for the other, so we still need to check them (it can be avoided by checking the last message of the pack as well).

async function fetchMessageUser(chan, id, res) {
  let option = {};
  if (typeof res !== 'undefined'){
    option = {before: res.id};
  }
  return await chan.fetchMessages(option)
      .then(async msgs => {
    if (msgs.size === 0){
      return {continue: false, found: false};
    };
    if ((Date.now() - (msgs.first().createdTimestamp)) > 86400000 ) { // 1 day
      return {continue: false, found: false};
    }
    let msgByAuthor = msgs.find(msg => {
      return msg.author.id === id;
    });
    if (msgByAuthor === null){
      return {continue: true, id: msgs.last().id};
    } else {
      return {continue: false, found: true, timestamp: msgByAuthor.createdTimestamp};
    }
      })
      .catch(err => console.log('ERR>>', err));
}


client.on('message', async (msg) => {
  let timestamp = [];
  for (let [id, chan] of msg.guild.channels){
    if (chan.type !== 'text'){ continue; }
    let id = '587692527826763788'; // id of the user, here a non verified account
    let res;
    do {
      res = await fetchMessageUser(chan, id, res);
    } while(res.continue);
    if (res.found) {
      timestamp.push(res.timestamp);
    }
  }
  console.log(timestamp);
  let first = timestamp.sort((a,b) => (b-a))[0];
  console.log(new Date(first));
});

更好的变体是为一组用户运行它,检查来自每个频道的所有 50 条最新消息,如果他写了一条,则将每个用户与他最近的消息相关联,并这样做直到所有消息中的所有消息频道太旧,无法避免踢/无论如何.然后为所有没有关联消息的用户做一些事情.

A better variant would be to run it for an array of users, checking all 50 last messages from every channel, and associating each users with his most recent messages if he wrote one, and doing this until all the messages in all the channels are too old to avoid a kick/whatever. And then do something for all the users who don't have an associated messages.

这篇关于Discord.js - 让用户最后一次活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆