简体   繁体   English

在线用户计数(discord.js v13)

[英]Online user counting (discord.js v13)

In the serverinfo command, as for me, how many users are online and offline is the most important, and not only, there are many important things, everything for better design and better recognition of information.在serverinfo命令中,对于我来说,有多少用户在线和离线是最重要的,不仅有很多重要的东西,一切都是为了更好的设计和更好的信息识别。
The topic now is precisely counting.现在的话题正在精确计数。 I know how to do it.我知道该怎么做。 But I have a problem that appeared after updating to discord.js v13 and still has not disappeared.但是我更新到discord.js v13后出现了一个问题,仍然没有消失。 I have been updated to the new version for quite some time.我已经更新到新版本很长一段时间了。
The bot stopped counting offline users.该机器人停止计算离线用户。 Sometimes it happens that the bot has to be turned off.有时会发生必须关闭机器人的情况。 After turning it on, I noticed that the offline users equals 0, although on the server on which I check the command of people offline, more than half of the server.开启后,我发现离线用户数为0,虽然在我查看离线用户命令的服务器上,超过了服务器的一半。 The number changes if some user appears online first.如果某个用户首先出现在线,则该数字会发生变化。
Not everything went smoothly with the rest of the statuses either: online, idle, dnd. rest 的状态并非一切顺利:在线、空闲、dnd。
My bot now has the status idle, so it turns out that users with statuses are idle not 6, but 5. With other statuses, on the contrary, let's say there are 6 online users, but the bot messages that there are 7 of them.我的机器人现在的状态是idle,所以原来状态的用户不是6个,而是5个。如果是其他状态,相反,假设有6个在线用户,但是bot消息中有7个.

const lang = require('../bot/languageSelector');
const { MessageEmbed } = require("discord.js");
module.exports = {
    name: 'serverinfo',
    description: 'Shows information of this Discord server',
    run: async (bot, interaction, colorMe) => {
        let embed = new MessageEmbed();
        let members = interaction.guild.members.cache;
        embed.setColor(colorMe);
        embed.setTitle(`${lang(interaction.user.id, "about")} ${interaction.guild.name}`);
        embed.setFields({
            name: `${lang(interaction.user.id, "byStatuses")}`,
            value: `
                ${lang(interaction.user.id, "online")}: ${members.filter(member => member.presence?.status == "online").size}
                ${lang(interaction.user.id, "idle")}: ${members.filter(member => member.presence?.status == "idle").size}
                ${lang(interaction.user.id, "dnd")}: ${members.filter(member => member.presence?.status == "dnd").size}
                ${lang(interaction.user.id, "offline")}: ${members.filter(member => member.presence?.status == "offline").size}
            `,
            inline: true
        });
        interaction.followUp({embeds: [embed]});
    }
}

Hmm, I'm not too sure about online, dnd and idle, but I know for the offline ones嗯,我不太确定online,dnd和idle,但我知道离线的

You see, if a member is offline, they have no presence.你看,如果一个成员离线,他们就没有存在感。 This goes for members who are invisible as well.这也适用于不可见的成员。 So members.filter(mem => mem.presence?.status === "offline") will ALWAYS return an empty collection.所以members.filter(mem => mem.presence?.status === "offline")总是会返回一个空集合。

Since they don't have presence, mem.presence will be null.由于它们没有存在, mem.presence将是 null。 You are also using optional chaining, so it will stop right there.您还使用了可选链接,所以它会停在那里。 So what your code is basically doing is members.filter(mem => null === "offline") which we know will return an empty collection.所以你的代码基本上是在做members.filter(mem => null === "offline")我们知道它会返回一个空集合。

However, to bypass this, you can use members.filter(mem =>.mem.presence) which means that filter members who DON'T have any presences, in other words, filter members that are offline.但是,要绕过这一点,您可以使用members.filter(mem =>.mem.presence) ,这意味着过滤没有任何存在的成员,换句话说,过滤离线的成员。

For other statuses, it could be because you are using .cache .对于其他状态,可能是因为您使用的是.cache Which means that the members / users aren't stored in the cache UNLESS they send a message when the bot is online, or use one of it's command.这意味着成员/用户不会存储在缓存中,除非他们在机器人在线时发送消息,或者使用其中一个命令。 To bypass this you can do:要绕过这个,你可以这样做:

// other code...
let members = await interaction.guild.members.fetch()
// other code...

This will fetch all members from the server.这将从服务器获取所有成员。 This also will store the members in cache so you don't have to fetch them every time.这也将成员存储在缓存中,因此您不必每次都获取它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM