From 800179fbbe14f11da5750427414dd4b28ec3118f Mon Sep 17 00:00:00 2001 From: Phil B Date: Sun, 24 Sep 2023 21:30:59 +0200 Subject: [PATCH] feat: added component default values --- include/dpp/message.h | 40 ++++++++++++++++++++++++++++++++++++++++ src/dpp/message.cpp | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/include/dpp/message.h b/include/dpp/message.h index 5f11536f98..5fe427c985 100644 --- a/include/dpp/message.h +++ b/include/dpp/message.h @@ -80,6 +80,31 @@ enum component_style : uint8_t { cos_link }; +/** + * Represents the type of a dpp::component_default_value + * + * @note They're different to discord's value types + */ +enum component_default_value_type: uint8_t { + cdt_user = 0, + cdt_role = 1, + cdt_channel = 2, +}; + +/** + * @brief A Default value structure for components + */ +struct DPP_EXPORT component_default_value { + /** + * @brief The type this default value represents + */ + component_default_value_type type; + /** + * @brief Default value. ID of a user, role, or channel + */ + dpp::snowflake id; +}; + /** * @brief An option for a select component */ @@ -282,6 +307,13 @@ class DPP_EXPORT component : public json_interface { */ std::vector channel_types; + /** + * List of default values for auto-populated select menu components. The amount of default values must be in the range defined by dpp::component::min_value and dpp::component::max_values. + * + * @note Only available for auto-populated select menu components, which include dpp::cot_user_selectmenu, dpp::cot_role_selectmenu, dpp::cot_mentionable_selectmenu, and dpp::cot_channel_selectmenu components. + */ + std::vector default_values; + /** Disabled flag (for buttons) */ bool disabled; @@ -490,6 +522,14 @@ class DPP_EXPORT component : public json_interface { */ component& add_component(const component& c); + /** + * @brief Add a default value. + * + * @param id Default value. ID of a user, role, or channel + * @param type The type this default value represents + */ + component& add_default_value(const snowflake id, const component_default_value_type type); + /** * @brief Set the emoji of the current sub-component. * Only valid for buttons. Adding an emoji to a component diff --git a/src/dpp/message.cpp b/src/dpp/message.cpp index 3f4331ad05..97f99606c5 100644 --- a/src/dpp/message.cpp +++ b/src/dpp/message.cpp @@ -57,6 +57,14 @@ component& component::fill_from_json(nlohmann::json* j) { if (j->contains("max_values") && j->at("max_values").is_number_integer()) { max_values = j->at("max_values").get(); } + if (j->contains("default_values") && !j->at("default_values").is_null()) { + for (auto const &v : j->at("default_values")) { + component_default_value d; + d.id = snowflake_not_null(&v, "id"); + d.type = static_cast(int8_not_null(&v, "type")); + default_values.push_back(d); + } + } if (type == cot_action_row) { for (json sub_component : (*j)["components"]) { dpp::component new_component; @@ -345,6 +353,15 @@ void to_json(json& j, const component& cp) { if (cp.max_values >= 0) { j["max_values"] = cp.max_values; } + if (!cp.default_values.empty()) { + j["default_values"] = json::array(); + for (auto const &v : cp.default_values) { + json o; + o["id"] = v.id; + o["type"] = v.type; + j["default_values"].push_back(o); + } + } } else if (cp.type == cot_channel_selectmenu) { j["custom_id"] = cp.custom_id; j["disabled"] = cp.disabled; @@ -363,6 +380,15 @@ void to_json(json& j, const component& cp) { j["channel_types"].push_back(type); } } + if (!cp.default_values.empty()) { + j["default_values"] = json::array(); + for (auto const &v : cp.default_values) { + json o; + o["id"] = v.id; + o["type"] = v.type; + j["default_values"].push_back(o); + } + } } } @@ -446,6 +472,14 @@ component& component::add_select_option(const select_option &option) { return *this; } +component &component::add_default_value(const snowflake id, const component_default_value_type type) { + component_default_value default_value; + default_value.id = id; + default_value.type = type; + this->default_values.push_back(default_value); + return *this; +} + embed::~embed() = default; embed::embed() : timestamp(0), color(0) {