diff --git a/docpages/example_code/resolved_objects.cpp b/docpages/example_code/resolved_objects.cpp new file mode 100644 index 0000000000..6727851478 --- /dev/null +++ b/docpages/example_code/resolved_objects.cpp @@ -0,0 +1,46 @@ +#include + +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(event.get_parameter("user")); + dpp::snowflake role_id = std::get(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()) { + + 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; +} diff --git a/docpages/example_programs/interactions_and_components.md b/docpages/example_programs/interactions_and_components.md index e371a6d792..022a156ba5 100644 --- a/docpages/example_programs/interactions_and_components.md +++ b/docpages/example_programs/interactions_and_components.md @@ -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 diff --git a/docpages/example_programs/interactions_and_components/resolved_objects.md b/docpages/example_programs/interactions_and_components/resolved_objects.md new file mode 100644 index 0000000000..cbe5134114 --- /dev/null +++ b/docpages/example_programs/interactions_and_components/resolved_objects.md @@ -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 diff --git a/include/dpp/cluster.h b/include/dpp/cluster.h index e59331ad76..736802ddae 100644 --- a/include/dpp/cluster.h +++ b/include/dpp/cluster.h @@ -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. diff --git a/include/dpp/cluster_coro_calls.h b/include/dpp/cluster_coro_calls.h index a6d0604eec..b98d914642 100644 --- a/include/dpp/cluster_coro_calls.h +++ b/include/dpp/cluster_coro_calls.h @@ -2061,6 +2061,16 @@ */ [[nodiscard]] async 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 co_thread_get(snowflake thread_id); + /** * @brief Edit current (bot) user * diff --git a/include/dpp/cluster_sync_calls.h b/include/dpp/cluster_sync_calls.h index 7ae7d3f54a..0b7ff5069f 100644 --- a/include/dpp/cluster_sync_calls.h +++ b/include/dpp/cluster_sync_calls.h @@ -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 * diff --git a/src/dpp/channel.cpp b/src/dpp/channel.cpp index 00a4db96a6..7f90c7eaa2 100644 --- a/src/dpp/channel.cpp +++ b/src/dpp/channel.cpp @@ -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) { diff --git a/src/dpp/cluster/thread.cpp b/src/dpp/cluster/thread.cpp index 54f4a1b79b..8c0dab0fce 100644 --- a/src/dpp/cluster/thread.cpp +++ b/src/dpp/cluster/thread.cpp @@ -160,4 +160,8 @@ void cluster::thread_member_remove(snowflake thread_id, snowflake user_id, comma rest_request(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(this, API_PATH "/channels", std::to_string(thread_id), "", m_get, "", callback); +} + } // namespace dpp diff --git a/src/dpp/cluster_coro_calls.cpp b/src/dpp/cluster_coro_calls.cpp index 5dbdf9ef00..32edde0e17 100644 --- a/src/dpp/cluster_coro_calls.cpp +++ b/src/dpp/cluster_coro_calls.cpp @@ -687,6 +687,10 @@ async cluster::co_thread_member_remove(snowflake thread return async{ this, static_cast(&cluster::thread_member_remove), thread_id, user_id }; } +async cluster::co_thread_get(snowflake thread_id) { + return async{ this, static_cast(&cluster::thread_get), thread_id }; +} + async cluster::co_current_user_edit(const std::string &nickname, const std::string& image_blob, const image_type type) { return async{ this, static_cast(&cluster::current_user_edit), nickname, image_blob, type }; } diff --git a/src/dpp/cluster_sync_calls.cpp b/src/dpp/cluster_sync_calls.cpp index 1e45f6d736..0cd703d095 100644 --- a/src/dpp/cluster_sync_calls.cpp +++ b/src/dpp/cluster_sync_calls.cpp @@ -685,6 +685,10 @@ confirmation cluster::thread_member_remove_sync(snowflake thread_id, snowflake u return dpp::sync(this, static_cast(&cluster::thread_member_remove), thread_id, user_id); } +thread cluster::thread_get_sync(snowflake thread_id) { + return dpp::sync(this, static_cast(&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(this, static_cast(&cluster::current_user_edit), nickname, image_blob, type); }