Skip to content

Commit

Permalink
feat: commandhandler now has functions to edit,get,delete responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaskowicz1 committed Jul 14, 2024
1 parent dc68a90 commit 09abcab
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 5 deletions.
53 changes: 51 additions & 2 deletions include/dpp/commandhandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,14 +414,63 @@ class DPP_EXPORT commandhandler {
* seconds.
*
* @param source source of the command
* @param ephemeral Should the "thinking" message be ephemeral?
* @param callback User function to execute when the api call completes.
*/
void thinking(command_source source, command_completion_event_t callback = utility::log_error());
void thinking(command_source source, bool ephemeral = false, command_completion_event_t callback = utility::log_error());

/**
* @brief Easter egg (redefinition of dpp::commandhandler::thinking).
*/
void thonk(command_source source, command_completion_event_t callback = utility::log_error());
void thonk(command_source source, bool ephemeral = false, command_completion_event_t callback = utility::log_error());

/**
* @brief Edit the response for this interaction
*
* @param m Message object to send. Not all fields are supported by Discord.
* @param source source of the command
* @param callback User function to execute when the api call completes.
* On success the callback will contain a dpp::confirmation object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error().
*/
void edit_response(const message& m, command_source source, command_completion_event_t callback = utility::log_error());

/**
* @brief Edit the response for this interaction
*
* @param mt The string value to send, for simple text only messages
* @param source source of the command
* @param callback User function to execute when the api call completes.
* On success the callback will contain a dpp::confirmation object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error().
*/
void edit_response(const std::string& mt, command_source source, command_completion_event_t callback = utility::log_error());

/**
* @brief Get original response message for this interaction
*
*@param source source of the command
* @param callback Function to call when the API call completes.
* On success the callback will contain a dpp::message object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error().
*/
void get_original_response(command_source source, command_completion_event_t callback = utility::log_error());

/**
* @brief Edit original response message for this interaction
*
* @param m Message object to send. Not all fields are supported by Discord.
* @param source source of the command
* @param callback Function to call when the API call completes.
* On success the callback will contain a dpp::message object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error().
*/
void edit_original_response(const message& m, command_source source, command_completion_event_t callback = utility::log_error());

/**
* @brief Delete original response message for this interaction. This cannot be used on an ephemeral interaction response.
*
* @param source source of the command
* @param callback Function to call when the API call completes.
* On success the callback will contain a dpp::confirmation object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error().
*/
void delete_original_response(command_source source, command_completion_event_t callback = utility::log_error());

};

Expand Down
51 changes: 48 additions & 3 deletions src/dpp/commandhandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,20 +421,65 @@ void commandhandler::reply(const dpp::message &m, command_source source, command
}
}

void commandhandler::thinking(command_source source, command_completion_event_t callback)
void commandhandler::thinking(command_source source, bool ephemeral, command_completion_event_t callback)
{
dpp::message msg(this->owner);
msg.content = "*";
msg.guild_id = source.guild_id;
msg.channel_id = source.channel_id;
if (ephemeral) {
msg.set_flags(dpp::m_ephemeral);
}
if (!source.command_token.empty() && source.command_id) {
owner->interaction_response_create(source.command_id, source.command_token, dpp::interaction_response(ir_deferred_channel_message_with_source, msg), callback);
}
}

void commandhandler::thonk(command_source source, command_completion_event_t callback)
void commandhandler::thonk(command_source source, bool ephemeral, command_completion_event_t callback)
{
thinking(source, callback);
thinking(source, ephemeral, callback);
}

void commandhandler::edit_response(const message &m, command_source source, command_completion_event_t callback) {
owner->interaction_response_edit(source.command_token, m, std::move(callback));
}

void commandhandler::edit_response(const std::string &mt, command_source source, command_completion_event_t callback) {
this->edit_response(dpp::message(source.channel_id, mt, mt_application_command), source, std::move(callback));
}

void commandhandler::get_original_response(command_source source, command_completion_event_t callback) {
owner->post_rest(API_PATH "/webhooks", owner->me.id.str(), source.command_token + "/messages/@original", m_get, "", [creator = owner, cb = std::move(callback)](json& j, const http_request_completion_t& http) {
if (cb) {
cb(confirmation_callback_t(creator, message().fill_from_json(&j), http));
}
});
}

void commandhandler::edit_original_response(const message &m, command_source source, command_completion_event_t callback) {
std::vector<std::string> file_names{};
std::vector<std::string> file_contents{};
std::vector<std::string> file_mimetypes{};

for(message_file_data data : m.file_data) {
file_names.push_back(data.name);
file_contents.push_back(data.content);
file_mimetypes.push_back(data.mimetype);
}

owner->post_rest_multipart(API_PATH "/webhooks", owner->me.id.str(), source.command_token + "/messages/@original", m_patch, m.build_json(), [creator = owner, cb = std::move(callback)](json& j, const http_request_completion_t& http) {
if (cb) {
cb(confirmation_callback_t(creator, message().fill_from_json(&j), http));
}
}, m.file_data);
}

void commandhandler::delete_original_response(command_source source, command_completion_event_t callback) {
owner->post_rest(API_PATH "/webhooks", owner->me.id.str(), source.command_token + "/messages/@original", m_delete, "", [creator = owner, cb = std::move(callback)](const json &, const http_request_completion_t& http) {
if (cb) {
cb(confirmation_callback_t(creator, confirmation(), http));
}
});
}

} // namespace dpp

0 comments on commit 09abcab

Please sign in to comment.