简体   繁体   English

查找具有两个或多个角色的用户 discord.js

[英]Finding users with two or more roles discord.js

I'll post my entire code because on other forums people were confused:我将发布我的整个代码,因为在其他论坛上人们感到困惑:

I am trying to make a discord bot that can display a list of users that have a specific role.我正在尝试制作一个可以显示具有特定角色的用户列表的不和谐机器人。 I know I need to list all roles in an array and then compare the user inputs to see if they match the strings in the array.我知道我需要列出数组中的所有角色,然后比较用户输入以查看它们是否与数组中的字符串匹配。 Then it will check for users who have all of the user input roles and display them.然后它将检查具有所有用户输入角色的用户并显示它们。

Example: if Spookybot and SpookySeed both have the role of admin but only Spookybot has the role of moderator as well, if i type role admin moderator the result should only display Spookybot since he has both roles.示例:如果 Spookybot 和 SpookySeed 都具有管理员角色,但只有 Spookybot 也具有主持人角色,如果我键入角色管理员主持人,结果应该只显示 Spookybot,因为他同时拥有这两个角色。 Any suggestions on how to accomplish this?关于如何实现这一点的任何建议?

const Discord = require ('discord.js')
const { MessageEmbed } = require ('discord.js')
const { type } = require('os')
const { receiveMessageOnPort } = require('worker_threads')
const client = new Discord.Client()

client.on('ready', () => {
    console.log("Connected as " + client.user.tag)

    client.user.setActivity("you senpai!", {type: "LISTENING"})
})

client.on('message', (receivedMessage) => {
    if (receivedMessage.author == client.user) {
        return
    }
    if (receivedMessage.content.startsWith("``")) {
        processCommand(receivedMessage)
    }
})

function processCommand(receivedMessage) {
    let fullCommand = receivedMessage.content.substr(2)
    let splitCommand = fullCommand.split(" ")
    let primaryCommand = splitCommand[0]
    let argument = splitCommand.slice(1)

    if (primaryCommand == "help") {
        helpCommand(argument, receivedMessage)
    }
    else if (primaryCommand == "role") {
            roleDisplayCommand(argument, receivedMessage)
    } 
    else {
        receivedMessage.channel.send("Uknown command")
    }
}

function helpCommand(argument, receivedMessage) {
    if (arguments.length > 0) {
        receivedMessage.channel.send("It looks like you might need help with but i am still in beta sorry!")
    } else {
        receivedMessage.channel.send("Unknown command")
    }
}

function roleDisplayCommand(arguments, receivedMessage) {
    if (arguments.length > 0) {
        const roleNames = receivedMessage.content.split(" ").slice(1);

        receivedMessage.author.send(`These users have the ${roleNames.join(" ")} role(s)` )
        
        const userList = roleNames.map(roleName => receivedMessage.guild.roles.cache.find(r => r.name === roleName).members.map(m=>m.user.tag).join("\n"))
        
        const sortedList = userList.join().split(",").filter((item, index, self) => self.indexOf(item) === index);
        receivedMessage.author.send(sortedList.join("\n"))
        
    }
     else {
        receivedMessage.channel.send("Unknown command")
    }
}
client.login("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX") 

You can use filter on your guild users list:您可以在公会用户列表上使用过滤器:

const specificsUsers = users.filter(user =>
    user.roles.has(role1.id) && user.roles.has(role2.id)
)

I'd iterate over the roles and filter out the users without the roles.我会遍历角色并过滤掉没有角色的用户。 Something like this should work.像这样的事情应该有效。

function roleDisplayCommand(arguments, receivedMessage) {
    ...
    const roleNames = receivedMessage.content.split(" ").slice(1);

    receivedMessage.author.send(`These users have the ${roleNames.join(" ")} role(s)` )
       
    let users = receivedMessage.guild.members.cache;
    const userList = roleNames.every(
      role => {
        users = users.filter(u => u.roles.cache.some(r => r.name === role));
      });

    // Either
    receivedMessage.author.send(Array.from(users.values()).join("\n"));
    // or, but not tested and might have some weird behaviors
    receivedMessage.author.send(Array.prototype.join.call(users, "\n"));
    ...
}

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

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