You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Of course most people do just iterate over the roles of a member to check for a permission. But there's a helper method for that: dpp::guild::base_permissions gets a member's permission taking into account the server owner and role permissions.
3
+
Of course most people typically iterate over the roles of a member to check for a permission. But there is a helper method for this: dpp::guild::base_permissions retrieves a member's permissions, taking into account role permissions **and** the server owner.
4
4
5
5
For total member permissions including channel overwrites use either the dpp::channel::get_user_permissions or dpp::guild::permission_overwrites method. Both do the same under the hood.
6
6
7
-
They all return a dpp::permission class, which is a wrapper around a permission bitmask containing bits of the dpp::permissions enum.
7
+
They all return a dpp::permission class, which is a wrapper around a permission bitmask with several helpful methods for easier manipulation and checking of permissions. This bitmask contains flags from the dpp::permissions enum.
8
8
9
9
Demonstration:
10
10
@@ -17,11 +17,9 @@ if (c && c->get_user_permissions(member).can(dpp::p_send_messages)) {
17
17
18
18
### Role Hierarchy
19
19
20
-
The recommended and correct way to compare for roles in the hierarchy is using the comparison operators (`<`, `>`) on the \ref dpp::role::operator<(dpp::role, dpp::role) "dpp::role" objects themselves.
21
-
Keep in mind that multiple roles can have the same position number.
22
-
As a result, comparing roles by position alone can lead to subtle bugs when checking for role hierarchy.
20
+
The recommended and correct way to compare for roles in the hierarchy is using the comparison operators (`<`, `>`) on the dpp::role objects themselves. Keep in mind that multiple roles can have the same position number. As a result, comparing roles by position alone can lead to subtle bugs when checking for role hierarchy.
23
21
24
-
For example let's say you have a ban command, and want to make sure that any issuer of the command can only ban members lower position than their own highest role.
22
+
For example let's say you have a ban command, and want to make sure that any issuer of the command can only ban members of lower position than their own highest role:
Discord's intended way to manage permissions for commands is through default member permissions. You set them using dpp::slashcommand::set_default_permissions when creating or updating a command to set the default permissions a user must have to use it. However, server administrators can then overwrite these permissions by their own restrictions.
48
+
Discord's intended way of managing permissions for commands is through "default member permissions". In a nutshell you tell Discord which permissions a user must have to use the command. Discord completely hides the command for members who don't have the required permissions. You set them using dpp::slashcommand::set_default_permissions when creating or updating a command.
51
49
52
50
The corresponding code to create a command with default permissions would look something like this:
53
51
@@ -62,9 +60,15 @@ command.add_option(dpp::command_option(dpp::co_string, "reason", "The reason for
62
60
bot.global_command_create(command);
63
61
```
64
62
63
+
You can set the default member permissions to "0" to disable the command for everyone except admins by default.
64
+
65
+
For more customization for server owners, they can override these permissions by their own restrictions in the server settings. This is why they are referred to as "default" permissions.
66
+
65
67
### Checking Permissions on Your Own
66
68
67
-
If you want to check permissions on your own, the easiest way to check if a member has certain permissions in interaction events is by using the dpp::interaction::get_resolved_permission function. The resolved list contains associated structures for the command and does not use the cache or require any extra API calls. Note that the permissions in the resolved set are pre-calculated by Discord and taking into account channel overwrites, roles and admin privileges. So no need to loop through roles or stuff like that.
69
+
When using default permissions you don't necessarily need to check the issuing user for any permissions in the interaction event as Discord handles all that for you. However, if you don't want server admins to be able to override the command restrictions, you can make those permission checks on your own.
70
+
71
+
To check if a member has certain permissions during interaction events, the easiest way is to use the dpp::interaction::get_resolved_permission function. The resolved list contains associated structures for the command and does not rely on the cache or require any extra API calls. Additionally, the permissions in the resolved set are pre-calculated by Discord and taking into account channel overwrites, roles and admin privileges. So, there's no need to loop through roles or stuff like that.
\note When using default permissions you don't necessarily need to check the issuing user for any permissions in the interaction event as Discord handles all that for you. But if you'd sleep better...
84
-
85
87
### From Parameters
86
88
87
89
The resolved set also contains the permissions of members from command parameters.
When replying to interactions using dpp::interaction_create_t::reply, you do **not** need to manually check whether the bot has permission to send messages. A bot always has permissions to reply to an interaction.
Copy file name to clipboardexpand all lines: include/dpp/channel.h
+14
Original file line number
Diff line number
Diff line change
@@ -633,6 +633,13 @@ class DPP_EXPORT channel : public managed, public json_interface<channel> {
633
633
* @param allowed_permissions bitmask of dpp::permissions you want to allow for this user/role in this channel. Note: You can use the dpp::permission class
634
634
* @param denied_permissions bitmask of dpp::permissions you want to deny for this user/role in this channel. Note: You can use the dpp::permission class
0 commit comments