Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:brainboxdotcc/DPP
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Oct 9, 2023
2 parents daa99bb + 1bca602 commit 29b051f
Show file tree
Hide file tree
Showing 10 changed files with 108 additions and 1 deletion.
46 changes: 46 additions & 0 deletions docpages/example_code/resolved_objects.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <dpp/dpp.h>

int main() {
dpp::cluster bot("token");

bot.on_log(dpp::utility::cout_logger());

/* The event is fired when someone issues your commands */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {

/* Check which command they ran */
if (event.command.get_command_name() == "addrole") {

/* Fetch a parameter value from the command options */
dpp::snowflake user_id = std::get<dpp::snowflake>(event.get_parameter("user"));
dpp::snowflake role_id = std::get<dpp::snowflake>(event.get_parameter("role"));

/* Get member object from resolved list */
dpp::guild_member resolved_member = event.command.get_resolved_member(user_id);

resolved_member.add_role(role_id);
bot.guild_edit_member(resolved_member);

event.reply("Added role");
}
});

/* Attach on_ready event */
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {

dpp::slashcommand add_role("addrole", "Give user a role", bot.me.id);

/* Add user and role type command options to the slash command */
add_role.add_option(dpp::command_option(dpp::co_user, "user", "User to give role to", true));
add_role.add_option(dpp::command_option(dpp::co_role, "role", "Role to give", true));

bot.global_command_create(add_role);
}
});

/* Start bot */
bot.start(dpp::st_wait);

return 0;
}
1 change: 1 addition & 0 deletions docpages/example_programs/interactions_and_components.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ The example programs listed here demonstrate lots of things to do with interacti
* \subpage discord-application-command-file-upload "Using file parameters in slash commands"
* \subpage private-messaging
* \subpage making_threads
* \subpage resolved-objects
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
\page resolved-objects Using Resolved Objects

If your slash command accepts options like user, channel, or role you can get their value, as specified by the user in the command, from parameters. Though parameter gives you only the snowflake id of the passed value.

If you need object of that snowflake, you can get that from the resolved set using its snowflake id.

Below is an example showing how to get a member, passed in command options, using resolved set.

\include{cpp} resolved_objects.cpp
9 changes: 9 additions & 0 deletions include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -3312,6 +3312,15 @@ class DPP_EXPORT cluster {
*/
void threads_get_joined_private_archived(snowflake channel_id, snowflake before_id, uint16_t limit, command_completion_event_t callback);

/**
* @brief Get the thread specified by thread_id. This uses the same call as dpp::cluster::channel_get but returns a thread object.
* @see https://discord.com/developers/docs/resources/channel#get-channel
* @param thread_id The id of the thread to obtain.
* @param callback Function to call when the API call completes
* On success the callback will contain a dpp::thread 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 thread_get(snowflake thread_id, command_completion_event_t callback);

/**
* @brief Create a sticker in a guild
* @note This method supports audit log reasons set by the cluster::set_audit_reason() method.
Expand Down
10 changes: 10 additions & 0 deletions include/dpp/cluster_coro_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -2061,6 +2061,16 @@
*/
[[nodiscard]] async<confirmation_callback_t> co_thread_member_remove(snowflake thread_id, snowflake user_id);

/**
* @brief Get the thread specified by thread_id. This uses the same call as dpp::cluster::channel_get but returns a thread object.
* @see dpp::cluster::thread_get
* @see https://discord.com/developers/docs/resources/channel#get-channel
* @param thread_id The id of the thread to obtain.
* @return thread returned object on completion
* \memberof dpp::cluster
*/
[[nodiscard]] async<confirmation_callback_t> co_thread_get(snowflake thread_id);

/**
* @brief Edit current (bot) user
*
Expand Down
13 changes: 13 additions & 0 deletions include/dpp/cluster_sync_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -2535,6 +2535,19 @@ confirmation thread_member_add_sync(snowflake thread_id, snowflake user_id);
*/
confirmation thread_member_remove_sync(snowflake thread_id, snowflake user_id);

/**
* @brief Get the thread specified by thread_id. This uses the same call as dpp::cluster::channel_get but returns a thread object.
* @see dpp::cluster::thread_get
* @see https://discord.com/developers/docs/resources/channel#get-channel
* @param thread_id The id of the thread to obtain.
* @return thread returned object on completion
* \memberof dpp::cluster
* @throw dpp::rest_exception upon failure to execute REST function
* @warning This function is a blocking (synchronous) call and should only be used from within a separate thread.
* Avoid direct use of this function inside an event handler.
*/
thread thread_get_sync(snowflake thread_id);

/**
* @brief Edit current (bot) user
*
Expand Down
9 changes: 8 additions & 1 deletion src/dpp/channel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,14 @@ channel& channel::fill_from_json(json* j) {
std::string thread::build_json(bool with_id) const {
json j = json::parse(channel::build_json(with_id));
j["type"] = (flags & CHANNEL_TYPE_MASK);
j["thread_metadata"] = this->metadata;
j["archived"] = this->metadata.archived;
j["auto_archive_duration"] = this->metadata.auto_archive_duration;
j["locked"] = this->metadata.locked;

if(this->get_type() == dpp::channel_type::CHANNEL_PRIVATE_THREAD) {
j["invitable"] = this->metadata.invitable;
}

if (!this->applied_tags.empty()) {
j["applied_tags"] = json::array();
for (auto &tag_id: this->applied_tags) {
Expand Down
4 changes: 4 additions & 0 deletions src/dpp/cluster/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,8 @@ void cluster::thread_member_remove(snowflake thread_id, snowflake user_id, comma
rest_request<confirmation>(this, API_PATH "/channels", std::to_string(thread_id), "/thread-members/" + std::to_string(user_id), m_delete, "", callback);
}

void cluster::thread_get(snowflake thread_id, command_completion_event_t callback) {
rest_request<thread>(this, API_PATH "/channels", std::to_string(thread_id), "", m_get, "", callback);
}

} // namespace dpp
4 changes: 4 additions & 0 deletions src/dpp/cluster_coro_calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,6 +687,10 @@ async<confirmation_callback_t> cluster::co_thread_member_remove(snowflake thread
return async{ this, static_cast<void (cluster::*)(snowflake, snowflake, command_completion_event_t)>(&cluster::thread_member_remove), thread_id, user_id };
}

async<confirmation_callback_t> cluster::co_thread_get(snowflake thread_id) {
return async{ this, static_cast<void (cluster::*)(snowflake, command_completion_event_t)>(&cluster::thread_get), thread_id };
}

async<confirmation_callback_t> cluster::co_current_user_edit(const std::string &nickname, const std::string& image_blob, const image_type type) {
return async{ this, static_cast<void (cluster::*)(const std::string &, const std::string&, const image_type, command_completion_event_t)>(&cluster::current_user_edit), nickname, image_blob, type };
}
Expand Down
4 changes: 4 additions & 0 deletions src/dpp/cluster_sync_calls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,10 @@ confirmation cluster::thread_member_remove_sync(snowflake thread_id, snowflake u
return dpp::sync<confirmation>(this, static_cast<void (cluster::*)(snowflake, snowflake, command_completion_event_t)>(&cluster::thread_member_remove), thread_id, user_id);
}

thread cluster::thread_get_sync(snowflake thread_id) {
return dpp::sync<thread>(this, static_cast<void (cluster::*)(snowflake, command_completion_event_t)>(&cluster::thread_get), thread_id);
}

user cluster::current_user_edit_sync(const std::string &nickname, const std::string& image_blob, const image_type type) {
return dpp::sync<user>(this, static_cast<void (cluster::*)(const std::string &, const std::string&, const image_type, command_completion_event_t)>(&cluster::current_user_edit), nickname, image_blob, type);
}
Expand Down

0 comments on commit 29b051f

Please sign in to comment.