javascript——我想让检测在线成员的命令成为一个特定的角色

eoxn13cs  于 2021-09-13  发布在  Java
关注(0)|答案(1)|浏览(260)

我需要你的帮助

"TypeError: Cannot read property 'cache' of undefined"

因此,基本上,我试图发出一个命令来检测具有特定角色的在线成员,但是当我想要过滤获取时,我得到了这个错误。

if (command === 't') {
        let guild = bot.guilds.cache.get('750637822343512085');
        let collection = guild.members.fetch()
        collection.filter(guild.members.roles.cache.has("837754186048208907")).then(console.log)
    }

如果你能给我一个展示在线会员具体角色的方式,我将不胜感激。
(注意:代码未完成,我只能过滤角色)

w80xi6nr

w80xi6nr1#

要实现这一点,您需要获得 Guild ,然后 Role 从上述 Guild ,最后,现在您可以访问 members 性质 Role 并按其状态筛选成员。

// Getting the Guild.
const guild = client.guilds.cache.get('GuildID')
// Getting the Role.
const role = guild.roles.cache.get('RoleID')
// Getting the Members in the Role and filtering them by the status. (Making sure their status is anything other than 'offline' (e.g: idle, do not disturb (dnd), online))
const members = role.members.filter(member => member.presence.status !== 'offline')

console.log(`There are ${members.size} online members with the role ${role.name}.`)

如果你的机器人在有更多在线成员的情况下看到0个在线成员,请确保你拥有 PRESENCE INTENT 已启用,如下面的屏幕截图所示:

相关问题