Skip to content

Commit

Permalink
feat: added component default values
Browse files Browse the repository at this point in the history
  • Loading branch information
Commandserver committed Sep 24, 2023
1 parent 3e8bdd9 commit 800179f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 0 deletions.
40 changes: 40 additions & 0 deletions include/dpp/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -282,6 +307,13 @@ class DPP_EXPORT component : public json_interface<component> {
*/
std::vector<uint8_t> 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<component_default_value> default_values;

/** Disabled flag (for buttons)
*/
bool disabled;
Expand Down Expand Up @@ -490,6 +522,14 @@ class DPP_EXPORT component : public json_interface<component> {
*/
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
Expand Down
34 changes: 34 additions & 0 deletions src/dpp/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<int32_t>();
}
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<component_default_value_type>(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;
Expand Down Expand Up @@ -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;
Expand All @@ -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);
}
}
}
}

Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 800179f

Please sign in to comment.