Skip to content

Commit

Permalink
feat: new premium upsell button style (#1175)
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis authored Jun 19, 2024
2 parents 02966ec + f945d83 commit 501131f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 17 deletions.
3 changes: 3 additions & 0 deletions include/dpp/appcommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,9 @@ enum interaction_response_type {
* @see https://discord.com/developers/docs/monetization/entitlements#premiumrequired-interaction-response
* @note Not available for autocomplete and ping interactions.
* @warning This response does not support using `content`, `embeds`, or `attachments`, so reply with no data when using this!
*
* @depreciated Replaced with buttons with a style of cos_premium
* This interaction type may stop working at any point
*/
ir_premium_required = 10,
};
Expand Down
37 changes: 31 additions & 6 deletions include/dpp/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,29 +166,36 @@ enum text_style_type : uint8_t {
*/
enum component_style : uint8_t {
/**
* @brief Blurple
* @brief Blurple; Primary
*/
cos_primary = 1,

/**
* @brief Grey
* @brief Grey; Secondary
*/
cos_secondary,

/**
* @brief Green
* @brief Green; Success
*/
cos_success,

/**
* @brief Red
* @brief Red; danger
*/
cos_danger,

/**
* @brief An external hyperlink to a website
* @brief An external hyperlink to a website, requires url to be set
* @note Will not work unless url is set
*/
cos_link
cos_link,

/**
* @brief Premium upsell button, requires sku_id to be set
* @note Will not work unless sku is set
*/
cos_premium,
};

/**
Expand Down Expand Up @@ -387,6 +394,11 @@ class DPP_EXPORT component : public json_interface<component> {
*/
std::string url;

/**
* @brief The SKU ID for premium upsell buttons
*/
dpp::snowflake sku_id;

/**
* @brief Placeholder text for select menus and text inputs (max 150 characters)
*/
Expand Down Expand Up @@ -489,6 +501,17 @@ class DPP_EXPORT component : public json_interface<component> {
*/
component& set_type(component_type ct);

/**
* @brief Set the SKU ID for a premium upsell button
* This is only valid for premium upsell buttons of type
* cos_premium. It indicates which premium package to
* link to when the button is clicked.
*
* @param sku The SKU ID
* @return component& reference to self
*/
component& set_sku_id(dpp::snowflake sku);

/**
* @brief Set the text style of a text component
* @note Sets type to `cot_text`
Expand Down Expand Up @@ -1869,6 +1892,8 @@ enum message_type {

/**
* @brief Interaction premium upsell
* @depreciated Replaced with buttons with a style of cos_premium
* This message type may stop working at any point
*/
mt_interaction_premium_upsell = 26,

Expand Down
26 changes: 15 additions & 11 deletions src/dpp/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,11 @@
************************************************************************************/
#include <algorithm>
#include <dpp/message.h>
#include <dpp/user.h>
#include <dpp/channel.h>
#include <dpp/guild.h>
#include <dpp/cache.h>
#include <dpp/json.h>
#include <dpp/discordevents.h>
#include <dpp/stringops.h>
#include <dpp/exception.h>
#include <dpp/cluster.h>



namespace dpp {

using json = nlohmann::json;
Expand Down Expand Up @@ -235,6 +228,12 @@ component& component::set_max_length(uint32_t max_l)
return *this;
}

component& component::set_sku_id(dpp::snowflake sku)
{
sku_id = sku;
return *this;
}

void to_json(json& j, const attachment& a) {
if (a.id) {
j["id"] = a.id;
Expand Down Expand Up @@ -274,15 +273,20 @@ void to_json(json& j, const component& cp) {
}
}
if (cp.type == cot_button) {
j["label"] = cp.label;
if (!cp.label.empty()) {
j["label"] = cp.label;
}
j["style"] = int(cp.style);
if (cp.type == cot_button && cp.style != cos_link && !cp.custom_id.empty()) {
/* Links cannot have a custom id */
if (cp.style != cos_link && cp.style != cos_premium && !cp.custom_id.empty()) {
/* Links and premium upsell cannot have a custom id */
j["custom_id"] = cp.custom_id;
}
if (cp.type == cot_button && cp.style == cos_link && !cp.url.empty()) {
if (cp.style == cos_link && !cp.url.empty()) {
j["url"] = cp.url;
}
if (cp.style == cos_premium && !cp.sku_id.empty()) {
j["sku_id"] = cp.sku_id;
}
j["disabled"] = cp.disabled;

if (cp.emoji.id || !cp.emoji.name.empty()) {
Expand Down

0 comments on commit 501131f

Please sign in to comment.