The commandhandler class represents a group of commands, prefixed or slash commands with handling functions.
More...
#include <commandhandler.h>
|
bool | string_has_prefix (std::string &str) |
| Returns true if the string has a known prefix on the start. Modifies string to remove prefix if it returns true. More...
|
|
| commandhandler (class cluster *o, bool auto_hook_events=true, snowflake application_id=0) |
| Construct a new commandhandler object. More...
|
|
| ~commandhandler () |
| Destroy the commandhandler object. More...
|
|
commandhandler & | set_owner (class cluster *o) |
| Set the application id after construction. More...
|
|
commandhandler & | add_prefix (const std::string &prefix) |
| Add a prefix to the command handler. More...
|
|
commandhandler & | add_command (const std::string &command, const parameter_registration_t ¶meters, command_handler handler, const std::string &description="", snowflake guild_id=0) |
| Add a command to the command handler. More...
|
|
commandhandler & | register_commands () |
| Register all slash commands with Discord This method must be called at least once if you are using the "/" prefix to mark the end of commands being added to the handler. Note that this uses bulk registration and will replace any existing slash commands. More...
|
|
void | route (const struct dpp::message_create_t &event) |
| Route a command from the on_message_create function. Call this method from within your on_message_create with the received dpp::message object if you have disabled automatic registration of events. More...
|
|
void | route (const struct interaction_create_t &event) |
| Route a command from the on_interaction_create function. Call this method from your on_interaction_create with the received dpp::interaction_create_t object if you have disabled automatic registration of events. More...
|
|
void | reply (const dpp::message &m, command_source source, command_completion_event_t callback={}) |
| Reply to a command. You should use this method rather than cluster::message_create as the way you reply varies between slash commands and message commands. Note you should ALWAYS reply. Slash commands will emit an ugly error to the user if you do not emit some form of reply within 3 seconds. More...
|
|
void | thinking (command_source source, command_completion_event_t callback={}) |
| Reply to a command without a message, causing the discord client to display "Bot name is thinking...". The "thinking" message will persist for a maximum of 15 minutes. This counts as a reply for a slash command. Slash commands will emit an ugly error to the user if you do not emit some form of reply within 3 seconds. More...
|
|
The commandhandler class represents a group of commands, prefixed or slash commands with handling functions.
It can automatically register slash commands, and handle routing of messages and interactions to separated command handler functions.
- Deprecated:
- commandhandler and message commands are deprecated and dpp::slashcommand is encouraged as a replacement.
◆ commandhandler()
dpp::commandhandler::commandhandler |
( |
class cluster * |
o, |
|
|
bool |
auto_hook_events = true , |
|
|
snowflake |
application_id = 0 |
|
) |
| |
Construct a new commandhandler object.
- Parameters
-
o | Owning cluster to attach to |
auto_hook_events | Set to true to automatically hook the on_interaction_create and on_message events. You should not need to set this to false unless you have a specific use case, as D++ supports multiple listeners to an event, so will allow the commandhandler to hook to your command events without disrupting other uses for the events you may have. |
application_id | The application id of the bot. If not specified, the class will look within the cluster object and use cluster::me::id instead. |
◆ ~commandhandler()
dpp::commandhandler::~commandhandler |
( |
| ) |
|
Destroy the commandhandler object.
◆ add_command()
Add a command to the command handler.
- Parameters
-
command | Command to be handled. Note that if any one of your prefixes is "/" this will attempt to register a global command using the API and you will receive notification of this command via an interaction event. |
handler | Handler function |
parameters | Parameters to use for the command |
description | The description of the command, shown for slash commands |
guild_id | The guild ID to restrict the command to. For slash commands causes registration of a guild command as opposed to a global command. |
- Returns
- commandhandler& reference to self
- Exceptions
-
◆ add_prefix()
commandhandler & dpp::commandhandler::add_prefix |
( |
const std::string & |
prefix | ) |
|
Add a prefix to the command handler.
- Parameters
-
prefix | Prefix to be handled by the command handler |
- Returns
- commandhandler& reference to self
◆ register_commands()
Register all slash commands with Discord This method must be called at least once if you are using the "/" prefix to mark the end of commands being added to the handler. Note that this uses bulk registration and will replace any existing slash commands.
Note that if you have previously registered your commands and they have not changed, you do not need to call this again. Discord retains a cache of previously added commands.
- Note
- Registration of global slash commands can take up to an hour to appear on Discord. This is a Discord API limitation. For rapid testing use guild specific commands by specifying a guild ID when declaring the command.
- Returns
- commandhandler& Reference to self for chaining method calls
◆ reply()
Reply to a command. You should use this method rather than cluster::message_create as the way you reply varies between slash commands and message commands. Note you should ALWAYS reply. Slash commands will emit an ugly error to the user if you do not emit some form of reply within 3 seconds.
- Parameters
-
m | message to reply with. |
source | source of the command |
callback | User function to execute when the api call completes. |
◆ route() [1/2]
Route a command from the on_message_create function. Call this method from within your on_message_create with the received dpp::message object if you have disabled automatic registration of events.
- Parameters
-
event | message create event to parse |
◆ route() [2/2]
Route a command from the on_interaction_create function. Call this method from your on_interaction_create with the received dpp::interaction_create_t object if you have disabled automatic registration of events.
- Parameters
-
event | command interaction event to parse |
◆ set_owner()
Set the application id after construction.
- Parameters
-
o | Owning cluster to attach to |
◆ string_has_prefix()
bool dpp::commandhandler::string_has_prefix |
( |
std::string & |
str | ) |
|
Returns true if the string has a known prefix on the start. Modifies string to remove prefix if it returns true.
- Parameters
-
str | String to check and modify |
- Returns
- true string contained a prefix, prefix removed from string
-
false string did not contain a prefix
◆ thinking()
Reply to a command without a message, causing the discord client to display "Bot name is thinking...". The "thinking" message will persist for a maximum of 15 minutes. This counts as a reply for a slash command. Slash commands will emit an ugly error to the user if you do not emit some form of reply within 3 seconds.
- Parameters
-
source | source of the command |
callback | User function to execute when the api call completes. |
◆ app_id
◆ commands
std::unordered_map<std::string, command_info_t> dpp::commandhandler::commands |
◆ interactions
Interaction event handle.
◆ messages
◆ owner
class cluster* dpp::commandhandler::owner |
Cluster we are attached to for issuing REST calls.
◆ prefixes
std::vector<std::string> dpp::commandhandler::prefixes |
◆ slash_commands_enabled
bool dpp::commandhandler::slash_commands_enabled |
Set to true automatically if one of the prefixes added is "/".