javascript Discord.js - 获取用户上次活动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50087758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 08:54:48  来源:igfitidea点击:

Discord.js - Getting users last activity?

javascriptnode.jsdiscorddiscord.js

提问by Owen

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

我试图找出是否有可能使用追溯获取用户上次活动的时间/信息 discord.js

Say I have something like

说我有类似的东西

  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
        })
  })

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

除非该成员已发布,否则由于客户端正在侦听,lastMessage 始终为空。

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.

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

Thanks

谢谢

回答by HymanRed

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

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

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.

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

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

如果用户很长时间没有写过,它可能会很长。当然,您必须lastMessage在尝试之前进行检查。

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.

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

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).

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

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));
});

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.

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

回答by Sjoerd Dal

I think what you need is one of Discord's built in features, namely: pruning. This feature will grab inactive members and lets you kick them. Luckily, discord.js has an API call for it and even lets you get the number of members first without actually kicking them by setting the dryparameter to true. The feature will also allow you to specify the amount of days a user has to be inactive.

我认为您需要的是 Discord 的内置功能之一,即:修剪。此功能将抓取非活动成员并让您踢他们。幸运的是,discord.js 有一个 API 调用,甚至可以通过将dry参数设置为true. 该功能还允许您指定用户必须处于非活动状态的天数。

Have a look at the docs: https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=pruneMembers

看看文档:https: //discord.js.org/#/docs/main/stable/class/Guild?scrollTo =pruneMembers

Hope that helps out!

希望有所帮助!