Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 查找具有两个或多个角色的用户discord.js_Javascript_Discord.js - Fatal编程技术网

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

Javascript 查找具有两个或多个角色的用户discord.js,javascript,discord.js,Javascript,Discord.js,我将发布我的全部代码,因为在其他论坛上,人们感到困惑: 我正在尝试制作一个discord机器人,它可以显示具有特定角色的用户列表。我知道我需要列出数组中的所有角色,然后比较用户输入,看看它们是否与数组中的字符串匹配。然后,它将检查具有所有用户输入角色的用户并显示它们 示例:如果Spookybot和SpookySeed都具有管理员角色,但只有Spookybot具有版主角色,则如果我键入role admin moderator,则结果应仅显示Spookybot,因为他同时具有这两个角色。关于如何做到

我将发布我的全部代码,因为在其他论坛上,人们感到困惑:

我正在尝试制作一个discord机器人,它可以显示具有特定角色的用户列表。我知道我需要列出数组中的所有角色,然后比较用户输入,看看它们是否与数组中的字符串匹配。然后,它将检查具有所有用户输入角色的用户并显示它们

示例:如果Spookybot和SpookySeed都具有管理员角色,但只有Spookybot具有版主角色,则如果我键入role admin moderator,则结果应仅显示Spookybot,因为他同时具有这两个角色。关于如何做到这一点有什么建议吗

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

您可以在帮会用户列表中使用过滤器:

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

我会迭代角色,过滤掉没有角色的用户。这样的办法应该行得通

函数roleDisplayCommand(参数,receivedMessage){
...
const roleNames=receivedMessage.content.split(“”).slice(1);
receivedMessage.author.send(`这些用户拥有${roleNames.join(“”)}角色`
让用户=receivedMessage.guild.members.cache;
const userList=roleNames.every(
角色=>{
users=users.filter(u=>u.roles.cache.some(r=>r.name==role));
});
//或者
receivedMessage.author.send(Array.from(users.values()).join(“\n”);
//或者,但是没有测试,可能有一些奇怪的行为
receivedMessage.author.send(Array.prototype.join.call(users,“\n”));
...
}

我想他想对任何数量的角色都使用此功能,因此您必须迭代角色并过滤掉没有此功能的角色。欢迎使用stackoverflow!尽量使您的代码片段更简洁,只包含问题的相关部分,在这种情况下,我认为仅使用
roleDisplayCommand
就足够了。我试用了您的代码,它给了我一个类型错误,说users.filter不是函数,它是
GuildCollectionManager
,我把这件事记下来了。使用缓存,它是一个集合。(相应地编辑)。我也不想这样做。你和我真的很感谢你的帮助,但它得到了另一个类型错误,即
u.role.has
不是一个函数,这是我在浏览器中从头顶上写代码时没有类型指示的功能。这是另一个管理器,同样,您需要缓存,这与我编写的有点不同,我将编辑。TypeError:u.roles.cache.any不是包含此新代码段的函数