izy521 / discord.io

A small, single-file library for creating DiscordApp clients from Node.js or the browser
https://discord.gg/0MvHMfHcTKVVmIGP
MIT License
535 stars 155 forks source link

How to get a list of users that have the same role. #298

Open munio76 opened 5 years ago

munio76 commented 5 years ago

Hi, and first of all thank you for this great client! I've read the documentation at https://izy521.github.io/discord.io-docs/Discord.Client.html, but since I'm not a developer I don't know how to figure out this: in my discord server I have 8 roles, correspondig to "levels" in a game; I need my bot to list all users/members with one of the roles. Could you kindly give me a hint? Thank you.

Peacerekam commented 5 years ago
Object.values(bot.servers[event.d.guild_id].members)
   .filter(m => m.roles.includes('246666176464158721'))
   .map(m => m.username + "#" + m.discriminator)

will return array like this:

["Madokami#6496","MimeeDevBot#6944","Charlemagne#3214","PatchBot#0303"]

how it works:

bot.servers[event.d.guild_id].members has all members of our guild member.roles is an array of role IDs that the user has Object.values() is there to enable a .filter() (and .map()) function we use .filter() to get only the objects that have have a role with ID 246666176464158721, we use .includes() on an array for that at the end to make it "readable" i included an extra .map() function that will output only the username#discriminator rather than the whole member object

that's probably the easiest approach

munio76 commented 5 years ago

Thank you for your reply, I'll try it asap!