From 7b45a1adb76fdce223c7680f22e027403e060ac9 Mon Sep 17 00:00:00 2001 From: Jaskowicz1 Date: Fri, 20 Oct 2023 15:32:07 +0100 Subject: [PATCH] feat: added roles to emojis --- include/dpp/emoji.h | 22 ++++++---------------- src/dpp/cluster/emoji.cpp | 14 ++++++++++++-- src/dpp/emoji.cpp | 25 +++++++++++++++++-------- 3 files changed, 35 insertions(+), 26 deletions(-) diff --git a/include/dpp/emoji.h b/include/dpp/emoji.h index 69f8a7b3b5..1f86c75eaa 100644 --- a/include/dpp/emoji.h +++ b/include/dpp/emoji.h @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -71,22 +72,11 @@ class DPP_EXPORT emoji : public managed, public json_interface { json to_json_impl(bool with_id = false) const; public: - /** - * @brief Emoji name - */ - std::string name{}; - /** - * @brief User id who uploaded the emoji - */ - snowflake user_id{0}; - /** - * @brief Flags for the emoji from dpp::emoji_flags - */ - uint8_t flags{0}; - /** - * @brief Image data for the emoji if uploading - */ - std::string image_data{}; + std::string name{}; // roles; //!< roles allowed to use this emoji + user user_obj; //!< user that created this emoji + std::string image_data{}; //!< Image data for the emoji if uploading + uint8_t flags{0}; //!< Flags for the emoji from dpp::emoji_flags /** * @brief Construct a new emoji object diff --git a/src/dpp/cluster/emoji.cpp b/src/dpp/cluster/emoji.cpp index 3e23da45f2..c0f4e8aeaf 100644 --- a/src/dpp/cluster/emoji.cpp +++ b/src/dpp/cluster/emoji.cpp @@ -23,7 +23,7 @@ namespace dpp { void cluster::guild_emoji_create(snowflake guild_id, const class emoji& newemoji, command_completion_event_t callback) { - rest_request(this, API_PATH "/guilds", std::to_string(guild_id), "emojis", m_post, newemoji.build_json(), callback); + rest_request(this, API_PATH "/guilds", std::to_string(guild_id), "emojis", m_post, newemoji.build_json(false), callback); } void cluster::guild_emoji_delete(snowflake guild_id, snowflake emoji_id, command_completion_event_t callback) { @@ -31,7 +31,17 @@ void cluster::guild_emoji_delete(snowflake guild_id, snowflake emoji_id, command } void cluster::guild_emoji_edit(snowflake guild_id, const class emoji& newemoji, command_completion_event_t callback) { - rest_request(this, API_PATH "/guilds", std::to_string(guild_id), "emojis/" + std::to_string(newemoji.id), m_patch, newemoji.build_json(), callback); + + /* Because newemoji.build_json will give more data than discord wants, + * we will just pull the data into a new json object. + */ + json newemoji_json = newemoji.build_json(false); + json j; + + j["name"] = newemoji_json["name"]; + j["roles"] = newemoji_json["roles"]; + + rest_request(this, API_PATH "/guilds", std::to_string(guild_id), "emojis/" + std::to_string(newemoji.id), m_patch, j, callback); } void cluster::guild_emoji_get(snowflake guild_id, snowflake emoji_id, command_completion_event_t callback) { diff --git a/src/dpp/emoji.cpp b/src/dpp/emoji.cpp index b033366e32..be84c65e43 100644 --- a/src/dpp/emoji.cpp +++ b/src/dpp/emoji.cpp @@ -38,9 +38,15 @@ emoji& emoji::fill_from_json_impl(nlohmann::json* j) { id = snowflake_not_null(j, "id"); name = string_not_null(j, "name"); if (j->contains("user")) { - json & user = (*j)["user"]; - user_id = snowflake_not_null(&user, "id"); + user_obj = user().fill_from_json(&((*j)["user"])); } + + if(j->contains("roles")) { + for (const auto& role : (*j)["roles"]) { + this->roles.emplace_back(to_string(role)); + } + } + if (bool_not_null(j, "require_colons")) { flags |= e_require_colons; } @@ -65,6 +71,10 @@ json emoji::to_json_impl(bool with_id) const { if (!image_data.empty()) { j["image"] = image_data; } + j["roles"] = json::array(); + for (const auto& role : roles) { + j["roles"].push_back(role); + } return j; } @@ -94,8 +104,7 @@ emoji& emoji::load_image(std::string_view image_blob, const image_type type) { return *this; } -std::string emoji::format() const -{ +std::string emoji::format() const { return id ? ((is_animated() ? "a:" : "") + name + ":" + std::to_string(id)) : name; } @@ -106,11 +115,11 @@ std::string emoji::get_mention() const { std::string emoji::get_url(uint16_t size, const dpp::image_type format, bool prefer_animated) const { if (this->id) { return utility::cdn_endpoint_url({ i_jpg, i_png, i_webp, i_gif }, - "emojis/" + std::to_string(this->id), - format, size, prefer_animated, is_animated()); - } else { - return std::string(); + "emojis/" + std::to_string(this->id), + format, size, prefer_animated, is_animated()); } + + return ""; }