Discord.js - 让用户最后一次活动?

2022-11-11前端开发问题
5

本文介绍了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 - 让用户最后一次活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13