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.
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.
They all return a dpp::permission class, which is a wrapper around a permission bitmask containing bits of the dpp::permissions enum.
Demonstration:
}
A definition of a discord channel. There are one of these for every channel type except threads....
Definition: channel.h:257
permission get_user_permissions(const class user *user) const
Get the overall permissions for a member in this channel, including channel overwrites,...
constexpr bool can(T... values) const noexcept
Check for certain permissions, taking into account administrator privileges. It uses the Bitwise AND ...
Definition: permissions.h:145
DPP_EXPORT class channel * find_channel(snowflake id)
@ p_send_messages
allows for sending messages in a channel
Definition: permissions.h:44
Permissions in Interaction events
Default Command Permissions
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-Admins can then overwrite these permissions by their own restrictions.
The corresponding code to create a command with default permissions would look something like this:
bot.global_command_create(command);
Represents an application command, created by your bot either globally, or on a guild.
Definition: appcommand.h:999
@ p_ban_members
allows banning members
Definition: permissions.h:35
@ co_user
Definition: appcommand.h:61
@ co_string
Definition: appcommand.h:55
Each command option is a command line parameter. It can have a type (see dpp::command_option_type),...
Definition: appcommand.h:155
Checking permissions on your own
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.
Let's imagine the following scenario:
You have a ban command and want to make sure the issuer has the ban permission.
event.reply("You don't have the required permissions to ban someone!");
return;
}
});
user usr
User object for the invoking user.
Definition: appcommand.h:709
snowflake id
Unique ID of object set by Discord. This value contains a timestamp, worker ID, internal server ID,...
Definition: managed.h:39
Represents a permission bitmask (refer to enum dpp::permissions) which are held in an uint64_t.
Definition: permissions.h:89
Create interaction.
Definition: dispatcher.h:363
interaction command
command interaction
Definition: dispatcher.h:585
- 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...
From Parameters
The resolved set also contains the permissions of members from command parameters.
For example, let's say you want to prohibit people from banning server admins with your ban command.
Get the user ID from the parameters and pass it to the get_resolved_permission
method:
event.reply("You can't ban Admins!");
return;
}
});
constexpr bool has(T... values) const noexcept
Check for permission flags set. It uses the Bitwise AND operator.
Definition: permissions.h:183
A container for a 64 bit unsigned value representing many things on discord. This value is known in d...
Definition: snowflake.h:49
@ p_administrator
allows all permissions and bypasses channel permission overwrites
Definition: permissions.h:36
virtual command_value get_parameter(const std::string &name) const
Get a slashcommand parameter.
The Bot's permissions
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.
event.reply("The bot doesn't have the required permission to ban anyone!");
return;
}
});
permission app_permissions
Permissions of the bot in the channel/guild where this command was issued.
Definition: appcommand.h:706