D++ (DPP)  10.0.30
C++ Discord API Bot Library
dpp::commandhandler Class Reference

The commandhandler class represents a group of commands, prefixed or slash commands with handling functions. More...

#include "commandhandler.h"

Collaboration diagram for dpp::commandhandler:

Public Member Functions

 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...
 
commandhandleradd_command (const std::string &command, const parameter_registration_t &parameters, command_handler handler, const std::string &description="", snowflake guild_id=0)
 Add a command to the command handler. More...
 
commandhandleradd_prefix (const std::string &prefix)
 Add a prefix to the command handler. More...
 
commandhandlerregister_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. More...
 
void reply (const dpp::message &m, command_source source, command_completion_event_t callback=utility::log_error())
 Reply to a command. More...
 
void route (const struct dpp::message_create_t &event)
 Route a command from the on_message_create function. More...
 
void route (const struct slashcommand_t &event)
 Route a command from the on_slashcommand function. More...
 
commandhandlerset_owner (class cluster *o)
 Set the application id after construction. More...
 
bool string_has_prefix (std::string &str)
 Returns true if the string has a known prefix on the start. More...
 
void thinking (command_source source, command_completion_event_t callback=utility::log_error())
 Reply to a command without a message, causing the discord client to display "Bot name is thinking...". More...
 

Public Attributes

snowflake app_id
 Application ID. More...
 
std::unordered_map< std::string, command_info_tcommands
 Commands in the handler. More...
 
event_handle interactions
 Interaction event handle. More...
 
event_handle messages
 Message event handle. More...
 
class clusterowner
 Cluster we are attached to for issuing REST calls. More...
 
std::vector< std::string > prefixes
 Valid prefixes. More...
 
bool slash_commands_enabled
 Set to true automatically if one of the prefixes added is "/". More...
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ commandhandler()

dpp::commandhandler::commandhandler ( class cluster o,
bool  auto_hook_events = true,
snowflake  application_id = 0 
)

Construct a new commandhandler object.

Parameters
oOwning cluster to attach to
auto_hook_eventsSet to true to automatically hook the on_slashcommand 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_idThe application id of the bot. If not specified, the class will look within the cluster object and use cluster::me::id instead.

References dpp::unicode_emoji::o, and owner.

◆ ~commandhandler()

dpp::commandhandler::~commandhandler ( )

Destroy the commandhandler object.

References app_id, commands, dpp::snowflake::empty(), dpp::command_info_t::func, dpp::command_info_t::guild_id, dpp::lowercase(), dpp::command_info_t::parameters, and slash_commands_enabled.

Here is the call graph for this function:

Member Function Documentation

◆ add_command()

commandhandler & dpp::commandhandler::add_command ( const std::string &  command,
const parameter_registration_t parameters,
command_handler  handler,
const std::string &  description = "",
snowflake  guild_id = 0 
)

Add a command to the command handler.

Parameters
commandCommand 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.
handlerHandler function
parametersParameters to use for the command
descriptionThe description of the command, shown for slash commands
guild_idThe 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
dpp::logic_exceptionif application ID cannot be determined

References dpp::command_option::add_choice(), dpp::slashcommand::add_option(), dpp::co_boolean, dpp::co_channel, dpp::co_integer, dpp::co_number, dpp::co_role, dpp::co_string, dpp::co_user, dpp::pt_boolean, dpp::pt_channel, dpp::pt_double, dpp::pt_integer, dpp::pt_role, dpp::pt_string, and dpp::pt_user.

Here is the call graph for this function:

◆ add_prefix()

commandhandler & dpp::commandhandler::add_prefix ( const std::string &  prefix)

Add a prefix to the command handler.

Parameters
prefixPrefix to be handled by the command handler
Returns
commandhandler& reference to self

References dpp::err_command_handler_not_ready.

◆ register_commands()

commandhandler & dpp::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.

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.

Returns
commandhandler& Reference to self for chaining method calls

References dpp::utility::utf8len(), and dpp::utility::utf8subview().

Here is the call graph for this function:

◆ reply()

void dpp::commandhandler::reply ( const dpp::message m,
command_source  source,
command_completion_event_t  callback = utility::log_error() 
)

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
mmessage to reply with.
sourcesource of the command
callbackUser function to execute when the api call completes.

References dpp::ir_deferred_channel_message_with_source.

◆ route() [1/2]

void dpp::commandhandler::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.

Parameters
eventmessage create event to parse

References dpp::find_channel(), dpp::find_guild(), dpp::find_role(), dpp::find_user(), dpp::message::guild_id, dpp::lowercase(), dpp::unicode_emoji::m, dpp::guild::members, dpp::message_create_t::msg, dpp::pt_boolean, dpp::pt_channel, dpp::pt_double, dpp::pt_integer, dpp::pt_role, dpp::pt_string, dpp::pt_user, and dpp::unicode_emoji::x.

Here is the call graph for this function:

◆ route() [2/2]

void dpp::commandhandler::route ( const struct slashcommand_t event)

Route a command from the on_slashcommand function.

Call this method from your on_slashcommand with the received dpp::interaction_create_t object if you have disabled automatic registration of events.

Parameters
eventcommand interaction event to parse

◆ set_owner()

commandhandler & dpp::commandhandler::set_owner ( class cluster o)

Set the application id after construction.

Parameters
oOwning 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
strString to check and modify
Returns
true string contained a prefix, prefix removed from string
false string did not contain a prefix

References dpp::message::guild_id, dpp::lowercase(), and dpp::message_create_t::msg.

Here is the call graph for this function:

◆ thinking()

void dpp::commandhandler::thinking ( command_source  source,
command_completion_event_t  callback = utility::log_error() 
)

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
sourcesource of the command
callbackUser function to execute when the api call completes.

Member Data Documentation

◆ app_id

snowflake dpp::commandhandler::app_id

Application ID.

Referenced by ~commandhandler().

◆ commands

std::unordered_map<std::string, command_info_t> dpp::commandhandler::commands

Commands in the handler.

Referenced by ~commandhandler().

◆ interactions

event_handle dpp::commandhandler::interactions

Interaction event handle.

◆ messages

event_handle dpp::commandhandler::messages

Message event handle.

◆ owner

class cluster* dpp::commandhandler::owner

Cluster we are attached to for issuing REST calls.

Referenced by commandhandler().

◆ prefixes

std::vector<std::string> dpp::commandhandler::prefixes

Valid prefixes.

◆ slash_commands_enabled

bool dpp::commandhandler::slash_commands_enabled

Set to true automatically if one of the prefixes added is "/".

Referenced by ~commandhandler().


The documentation for this class was generated from the following files: