Skip to content

Commit

Permalink
feat: added roles to emojis
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaskowicz1 committed Oct 20, 2023
1 parent 7150796 commit 7b45a1a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
22 changes: 6 additions & 16 deletions include/dpp/emoji.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <dpp/managed.h>
#include <dpp/utility.h>
#include <dpp/json_fwd.h>
#include <dpp/user.h>
#include <unordered_map>
#include <dpp/json_interface.h>

Expand Down Expand Up @@ -71,22 +72,11 @@ class DPP_EXPORT emoji : public managed, public json_interface<emoji> {
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{}; //<! emoji name
std::vector<dpp::snowflake> 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
Expand Down
14 changes: 12 additions & 2 deletions src/dpp/cluster/emoji.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,25 @@
namespace dpp {

void cluster::guild_emoji_create(snowflake guild_id, const class emoji& newemoji, command_completion_event_t callback) {
rest_request<emoji>(this, API_PATH "/guilds", std::to_string(guild_id), "emojis", m_post, newemoji.build_json(), callback);
rest_request<emoji>(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) {
rest_request<confirmation>(this, API_PATH "/guilds", std::to_string(guild_id), "emojis/" + std::to_string(emoji_id), m_delete, "", callback);
}

void cluster::guild_emoji_edit(snowflake guild_id, const class emoji& newemoji, command_completion_event_t callback) {
rest_request<emoji>(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<emoji>(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) {
Expand Down
25 changes: 17 additions & 8 deletions src/dpp/emoji.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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 "";
}


Expand Down

0 comments on commit 7b45a1a

Please sign in to comment.