Skip to content

Commit

Permalink
feat: banners can now be uploaded to bots
Browse files Browse the repository at this point in the history
  • Loading branch information
Jaskowicz1 committed Jul 13, 2024
1 parent dc68a90 commit 5903abd
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 13 deletions.
8 changes: 5 additions & 3 deletions include/dpp/cluster.h
Original file line number Diff line number Diff line change
Expand Up @@ -3259,13 +3259,15 @@ class DPP_EXPORT cluster {
* Fires a `Guild Member Update` Gateway event.
* @see https://discord.com/developers/docs/resources/user#modify-current-user
* @param nickname Nickname to set
* @param image_blob Avatar data to upload (NOTE: Very heavily rate limited!)
* @param type Type of image for avatar. It can be one of `i_gif`, `i_jpg` or `i_png`.
* @param avatar_blob Avatar data to upload (NOTE: Very heavily rate limited!)
* @param avatar_type Type of image for avatar. It can be one of `i_gif`, `i_jpg` or `i_png`.
* @param banner_blob Banner data to upload (NOTE: Very heavily rate limited!)
* @param banner_type Type of image for Banner. It can be one of `i_gif`, `i_jpg` or `i_png`.
* @param callback Function to call when the API call completes.
* On success the callback will contain a dpp::user object in confirmation_callback_t::value. On failure, the value is undefined and confirmation_callback_t::is_error() method will return true. You can obtain full error details with confirmation_callback_t::get_error().
* @throw dpp::length_exception Image data is larger than the maximum size of 256 kilobytes
*/
void current_user_edit(const std::string &nickname, const std::string& image_blob = "", const image_type type = i_png, command_completion_event_t callback = utility::log_error());
void current_user_edit(const std::string &nickname, const std::string& avatar_blob = "", const image_type avatar_type = i_png, const std::string& banner_blob = "", const image_type banner_type = i_png, command_completion_event_t callback = utility::log_error());

/**
* @brief Get current user DM channels
Expand Down
29 changes: 19 additions & 10 deletions src/dpp/cluster/user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,32 @@

namespace dpp {

void cluster::current_user_edit(const std::string &nickname, const std::string& image_blob, const image_type type, command_completion_event_t callback) {
void cluster::current_user_edit(const std::string &nickname, const std::string& avatar_blob, const image_type avatar_type, const std::string& banner_blob, const image_type banner_type, command_completion_event_t callback) {
json j = json::parse("{\"nickname\": null}");
if (!nickname.empty()) {
j["nickname"] = nickname;
}
if (!image_blob.empty()) {
static const std::map<image_type, std::string> mimetypes = {
{ i_gif, "image/gif" },
{ i_jpg, "image/jpeg" },
{ i_png, "image/png" },
{ i_webp, "image/webp" },
};
if (image_blob.size() > MAX_EMOJI_SIZE) {

static const std::map<image_type, std::string> mimetypes = {
{ i_gif, "image/gif" },
{ i_jpg, "image/jpeg" },
{ i_png, "image/png" },
};

if (!avatar_blob.empty()) {
if (avatar_blob.size() > MAX_EMOJI_SIZE) {
throw dpp::length_exception(err_icon_size, "User icon file exceeds discord limit of 256 kilobytes");
}
j["avatar"] = "data:" + mimetypes.find(type)->second + ";base64," + base64_encode((unsigned char const*)image_blob.data(), (unsigned int)image_blob.length());
j["avatar"] = "data:" + mimetypes.find(avatar_type)->second + ";base64," + base64_encode((unsigned char const*)avatar_blob.data(), static_cast<unsigned int>(avatar_blob.length()));
}

if (!banner_blob.empty()) {
if (avatar_blob.size() > MAX_EMOJI_SIZE) {
throw dpp::length_exception(err_icon_size, "User banner file exceeds discord limit of 256 kilobytes");
}
j["banner"] = "data:" + mimetypes.find(banner_type)->second + ";base64," + base64_encode((unsigned char const*)banner_blob.data(), static_cast<unsigned int>(banner_blob.length()));
}

rest_request<user>(this, API_PATH "/users", "@me", "", m_patch, j.dump(-1, ' ', false, json::error_handler_t::replace), callback);
}

Expand Down

0 comments on commit 5903abd

Please sign in to comment.