Here is my code in its entirety as I have received some confusion on other forums:
I am working on creating a Discord bot that can list users with a specific role. The plan is to create an array of roles, compare user inputs to the array, and display users who have all specified roles.
For example, if Spookybot and SpookySeed both have the admin role but only Spookybot has the moderator role, typing "role admin moderator" should only display Spookybot. Any ideas on how to achieve 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")