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: added component default values #889

Merged
merged 7 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
11 changes: 11 additions & 0 deletions include/dpp/user.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class DPP_EXPORT user : public managed, public json_interface<user> {
std::string global_name;
/** Avatar hash */
utility::iconhash avatar;
/** Avatar decoration hash */
utility::iconhash avatar_decoration;
/** Flags built from a bitmask of values in dpp::user_flags */
uint32_t flags;
/** Discriminator (aka tag), 4 digits usually displayed with leading zeroes.
Expand Down Expand Up @@ -157,6 +159,15 @@ class DPP_EXPORT user : public managed, public json_interface<user> {
*/
std::string get_default_avatar_url() const;

/**
* @brief Get the avatar decoration url of the user if they have one, otherwise returns an empty string.
*
* @param size The size of the avatar decoration in pixels. It can be any power of two between 16 and 4096,
* otherwise the default sized avatar decoration is returned.
* @return std::string avatar url or an empty string
*/
std::string get_avatar_decoration_url(uint16_t size = 0) const;

/**
* @brief Return a ping/mention for the user
*
Expand Down
46 changes: 46 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,21 @@ 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;
if (v.type == dpp::cdt_role) {
o["type"] = "role";
} else if (v.type == dpp::cdt_channel) {
o["type"] = "channel";
} else if (v.type == dpp::cdt_user) {
o["type"] = "user";
}
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 +386,21 @@ 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;
if (v.type == dpp::cdt_role) {
o["type"] = "role";
} else if (v.type == dpp::cdt_channel) {
o["type"] = "channel";
} else if (v.type == dpp::cdt_user) {
o["type"] = "user";
}
j["default_values"].push_back(o);
}
}
}
}

Expand Down Expand Up @@ -446,6 +484,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
12 changes: 12 additions & 0 deletions src/dpp/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,16 @@ std::string user::get_default_avatar_url() const {
}
}

std::string user::get_avatar_decoration_url(uint16_t size) const {
if (this->id) {
return utility::cdn_endpoint_url_hash({ i_png },
"avatar-decorations/" + std::to_string(this->id), this->avatar_decoration.to_string(),
Jaskowicz1 marked this conversation as resolved.
Show resolved Hide resolved
i_png, size);
} else {
return std::string();
}
}

std::string user::format_username() const {
return username + '#' + leading_zeroes(discriminator, 4);
}
Expand Down Expand Up @@ -279,6 +289,8 @@ void from_json(const nlohmann::json& j, user& u) {
}
u.avatar = av;

u.avatar_decoration = string_not_null(&j, "avatar_decoration");

u.discriminator = (uint16_t)snowflake_not_null(&j, "discriminator");

u.flags |= bool_not_null(&j, "bot") ? dpp::u_bot : 0;
Expand Down