Roles and Permissions
Last updated
Last updated
Roles are a powerful feature in Discord, and admittedly have been one of the hardest parts to master in discord.js. This walk through aims at explaining how roles and permissions work. We'll also explore how to use roles to protect your commands.
Let's start with a basic overview of the hierarchy of roles in Discord.
... or actually not, they already explain it better than I care to: . Read up on that, then come back here. I'll wait. (Yeah I know that's cheesy, so sue me).
Let's get down to brass tacks. You want to know how to use roles and permissions in your bot.
This is the "easy" part once you actually get used to it. It's just like getting any other Collection element, but here's a reminder anyway!
In a messageCreate
handler, you have access to checking the GuildMember class of the message author:
Alright, now that you have roles, you probably want to add a member to a role. Simple enough! Discord.js provides 2 handy methods to add, and remove, a role. Let's look at them!
Alright I feel like I have to add a little precision here on implementation:
You can not add or remove a role that is higher than the bot's. This should be obvious.
The bot requires MANAGE_ROLES
permissions for this. You can check for it using the code further down this page.
Because of global rate limits, you cannot do 2 role "actions" immediately one after the other. The first action will work, the second will not. You can go around that by using <GuildMember>.roles.set([array, of, roles])
. This will overwrite all existing roles and only apply the ones in the array so be careful with it.
To check for a single permission override on a channel:
Just as easy, woah!
ezpz, right?
Now get to coding!
To grab members and users in different ways see the .
We pass false
for the checkAdmin parameter because Administrator channel overwrites don't implicitly grant any permissions, unlike in Roles or when you are the Guild Owner. (The API will allow you to create an overwrite with Administrator, and even tell D.JS that a channel overwrite has had Administrator permissions set. Discord developers have stated this is .)
Click for the full list of internal permission names, used for .has(name)
in the above examples