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 url getter for emojis #682

Merged
merged 3 commits into from
May 14, 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
13 changes: 13 additions & 0 deletions include/dpp/emoji.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,19 @@ class DPP_EXPORT emoji : public managed, public json_interface<emoji> {
* @return std::string mention
*/
std::string get_mention() const;

/**
* @brief Get the custom emoji url
*
* @param size The size of the emoji in pixels. It can be any power of two between 16 and 4096,
* otherwise the default sized emoji is returned.
* @param format The format to use for the emoji. It can be one of `i_webp`, `i_jpg`, `i_png` or `i_gif`.
* Passing `i_gif` might result in an invalid url for non-animated emojis. Consider using the `prefer_animated` parameter instead.
* @param prefer_animated Whether you prefer gif format.
* If true, it'll return gif format whenever the emoji is available as animated.
* @return std::string emoji url or an empty string, if the id is not set
*/
std::string get_url(uint16_t size = 0, const image_type format = i_png, bool prefer_animated = true) const;
braindigitalis marked this conversation as resolved.
Show resolved Hide resolved
};

/**
Expand Down
22 changes: 22 additions & 0 deletions src/dpp/emoji.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@ std::string emoji::get_mention() const {
return utility::emoji_mention(name,id,is_animated());
}

std::string emoji::get_url(uint16_t size, const dpp::image_type format, bool prefer_animated) const {
static const std::map<image_type, std::string> extensions = {
{ i_gif, "gif" },
{ i_jpg, "jpg" },
{ i_png, "png" },
{ i_webp, "webp" },
};

if (extensions.find(format) == extensions.end()) {
return std::string();
}

if (this->id) {
return utility::cdn_host + "/emojis/" +
std::to_string(this->id) + "." +
(is_animated() && prefer_animated ? "gif" : extensions.find(format)->second) +
utility::avatar_size(size);
} else {
return std::string();
}
}


};

50 changes: 50 additions & 0 deletions src/unittest/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,14 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
);
}

{ // emoji url getter
dpp::emoji emoji;
emoji.id = 825407338755653641;

set_test("EMOJI.GET_URL", false);
set_test("EMOJI.GET_URL", emoji.get_url() == dpp::utility::cdn_host + "/emojis/825407338755653641.png");
}

{ // channel methods
set_test("CHANNEL.SET_TYPE", false);
dpp::channel c;
Expand Down Expand Up @@ -614,6 +622,48 @@ Markdown lol \\|\\|spoiler\\|\\| \\~\\~strikethrough\\~\\~ \\`small \\*code\\* b
});
}

set_test("REQUEST_GET_IMAGE", false);
if (!offline) {
bot.request("https://dpp.dev/DPP-Logo.png", dpp::m_get, [&bot](const dpp::http_request_completion_t &callback) {
if (callback.status != 200) {
return;
}
set_test("REQUEST_GET_IMAGE", true);

dpp::emoji emoji;
emoji.load_image(callback.body, dpp::i_png);
emoji.name = "dpp";

// emoji unit test with the requested image
set_test("EMOJI_CREATE", false);
set_test("EMOJI_GET", false);
set_test("EMOJI_DELETE", false);
bot.guild_emoji_create(TEST_GUILD_ID, emoji, [&bot](const dpp::confirmation_callback_t &event) {
if (event.is_error()) {
return;
}
set_test("EMOJI_CREATE", true);

auto created = event.get<dpp::emoji>();
bot.guild_emoji_get(TEST_GUILD_ID, created.id, [&bot, created](const dpp::confirmation_callback_t &event) {
if (event.is_error()) {
return;
}
auto fetched = event.get<dpp::emoji>();
if (created.id == fetched.id && created.name == fetched.name && created.flags == fetched.flags && created.user_id == fetched.user_id) {
set_test("EMOJI_GET", true);
}

bot.guild_emoji_delete(TEST_GUILD_ID, fetched.id, [](const dpp::confirmation_callback_t &event) {
if (!event.is_error()) {
set_test("EMOJI_DELETE", true);
}
});
});
});
});
}

set_test("AUTOMOD_RULE_CREATE", false);
set_test("AUTOMOD_RULE_GET", false);
set_test("AUTOMOD_RULE_GET_ALL", false);
Expand Down
5 changes: 5 additions & 0 deletions src/unittest/unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ std::map<std::string, test_t> tests = {
{"UTILITY.USER_MENTION", {tt_offline, "utility::user_mention", false, false}},
{"UTILITY.ROLE_MENTION", {tt_offline, "utility::role_mention", false, false}},
{"UTILITY.EMOJI_MENTION", {tt_offline, "utility::emoji_mention", false, false}},
{"EMOJI.GET_URL", {tt_offline, "emoji::get_url", false, false}},
{"ROLE.COMPARE", {tt_offline, "role::operator<", false, false}},
{"ROLE_CREATE", {tt_online, "cluster::role_create", false, false}},
{"ROLE_EDIT", {tt_online, "cluster::role_edit", false, false}},
Expand All @@ -129,6 +130,10 @@ std::map<std::string, test_t> tests = {
{"AUTOMOD_RULE_GET", {tt_online, "cluster::automod_rule_get", false, false}},
{"AUTOMOD_RULE_GET_ALL", {tt_online, "cluster::automod_rules_get", false, false}},
{"AUTOMOD_RULE_DELETE", {tt_online, "cluster::automod_rule_delete", false, false}},
{"REQUEST_GET_IMAGE", {tt_online, "using the cluster::request method to fetch an image", false, false}},
{"EMOJI_CREATE", {tt_online, "cluster::guild_emoji_create", false, false}},
{"EMOJI_GET", {tt_online, "cluster::guild_emoji_get", false, false}},
{"EMOJI_DELETE", {tt_online, "cluster::guild_emoji_delete", false, false}},
};

double start = dpp::utility::time_f();
Expand Down