From 09abcabdd5fafea8be1e4afb203fc22a65a6cec9 Mon Sep 17 00:00:00 2001 From: Archie Date: Sun, 14 Jul 2024 13:27:13 +0100 Subject: [PATCH] feat: commandhandler now has functions to edit,get,delete responses --- include/dpp/commandhandler.h | 53 ++++++++++++++++++++++++++++++++++-- src/dpp/commandhandler.cpp | 51 ++++++++++++++++++++++++++++++++-- 2 files changed, 99 insertions(+), 5 deletions(-) diff --git a/include/dpp/commandhandler.h b/include/dpp/commandhandler.h index 724f38f577..9196fb06a5 100644 --- a/include/dpp/commandhandler.h +++ b/include/dpp/commandhandler.h @@ -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()); }; diff --git a/src/dpp/commandhandler.cpp b/src/dpp/commandhandler.cpp index e337b0eee9..24c832e900 100644 --- a/src/dpp/commandhandler.cpp +++ b/src/dpp/commandhandler.cpp @@ -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 file_names{}; + std::vector file_contents{}; + std::vector 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