Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: commandhandler now has functions to edit/get/delete responses #1199

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these all need a check like in thinking, e.g.:

if (!source.command_token.empty() && source.command_id) {

they cant do anything if its a message command.

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we're adding these, shouldnt they also work for message commands?

this is the point of commandhandler, and why we dont really add much to it

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
Loading