Debian Codegraph @

docpages/example_programs/misc/checking-member-permissions.md

raw symbols SCIP
1 \page checking-member-permissions Checking Permissions
2
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
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
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
9 Demonstration:
10
11 ```cpp
12 dpp::channel* c = dpp::find_channel(some_channel_id);
13 if (c && c->get_user_permissions(member).can(dpp::p_send_messages)) {
14 //...
15 }
16 ```
17
18 ### Role Hierarchy
19
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.
21
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:
23
24 ```cpp
25 bot.on_interaction_create([](const dpp::interaction_create_t& event) {
26 dpp::snowflake target_id = std::get<dpp::snowflake>(event.get_parameter("user"));
27 dpp::guild_member target = event.command.get_resolved_member(target_id);
28
29 for (dpp::snowflake issuer_role_id : event.command.member.get_roles()) {
30 auto issuer_role = dpp::find_role(issuer_role_id);
31 if (issuer_role == nullptr) continue;
32 for (dpp::snowflake target_role_id : target.get_roles()) {
33 auto target_role = dpp::find_role(target_role_id);
34 if (target_role == nullptr) continue;
35 if (target_role > issuer_role) {
36 event.reply("You can't ban someone whose role is higher than yours!");
37 return;
38 }
39 }
40 }
41 });
42 ```
43
44 ## Permissions in Interaction Events
45
46 ### Default Command Permissions
47
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.
49
50 The corresponding code to create a command with default permissions would look something like this:
51
52 ```cpp
53 dpp::slashcommand command("ban", "Ban a member", bot.me.id);
54
55 command.set_default_permissions(dpp::p_ban_members); // set permissions that are required by default here
56
57 command.add_option(dpp::command_option(dpp::co_user, "user", "The user to ban", true));
58 command.add_option(dpp::command_option(dpp::co_string, "reason", "The reason for banning", true));
59
60 bot.global_command_create(command);
61 ```
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
67 ### Checking Permissions on Your Own
68
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.
72
73 Let's imagine the following scenario:
74
75 You have a ban command and want to make sure the issuer has the ban permission.
76
77 ```cpp
78 bot.on_interaction_create([](const dpp::interaction_create_t& event) {
79 dpp::permission perms = event.command.get_resolved_permission(event.command.usr.id);
80 if (!perms.can(dpp::p_ban_members)) {
81 event.reply("You don't have the required permissions to ban someone!");
82 return;
83 }
84 });
85 ```
86
87 ### From Parameters
88
89 The resolved set also contains the permissions of members from command parameters.
90
91 For example, let's say you want to prohibit people from banning server admins with your ban command.
92
93 Get the user ID from the parameters and pass it to the `get_resolved_permission` method:
94
95 ```cpp
96 bot.on_interaction_create([](const dpp::interaction_create_t& event) {
97 dpp::snowflake user_id = std::get<dpp::snowflake>(event.get_parameter("user"));
98 dpp::permission perms = event.command.get_resolved_permission(user_id);
99 if (perms.has(dpp::p_administrator)) {
100 event.reply("You can't ban Admins!");
101 return;
102 }
103 });
104 ```
105
106 ### The Bot's Permissions
107
108 You also might want to check if the bot itself has the ban permission before processing the command further. You can access the bot's permissions in the dpp::interaction::app_permissions field.
109
110 ```cpp
111 bot.on_interaction_create([](const dpp::interaction_create_t& event) {
112 if (!event.command.app_permissions.can(dpp::p_ban_members)) {
113 event.reply("The bot doesn't have the required permission to ban anyone!");
114 return;
115 }
116 });
117 ```
118
119 ### Things to Keep in Mind
120
121 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.