Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:brainboxdotcc/DPP
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Oct 16, 2023
2 parents f1657d0 + 6f45bec commit fa7ff38
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 9 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ jobs:
uses: microsoft/setup-msbuild@1ff57057b5cfdc39105cd07a01d78e9b0ea0c14c # v1.3.1

- name: Install chocolatey packages ${{ matrix.cfg.arch}}
uses: seanmiddleditch/gha-setup-ninja@6263846cf3c17009dfc81604efabae16044fc074 # master
uses: seanmiddleditch/gha-setup-ninja@8b297075da4cd2a5f1fd21fe011b499edf06e9d2 # master

- name: Generate CMake (x64)
if: ${{ matrix.cfg.arch == 'x64' }}
Expand Down
46 changes: 46 additions & 0 deletions docpages/example_code/components3_rolemenu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <dpp/dpp.h>

int main() {
dpp::cluster bot("token");

bot.on_log(dpp::utility::cout_logger());

/* The event is fired when someone issues your commands */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "select") {
/* Create a message */
dpp::message msg(event.command.channel_id, "This text has a select menu!");

/* Add an action row, and a select menu within the action row.
*
* By default, max values is 1, meaning people can only pick 1 option.
* We're changing this to two, so people can select multiple options!
* We'll also set the min_values to 2 so people have to pick another value!
*/
msg.add_component(
dpp::component().add_component(
dpp::component()
.set_type(dpp::cot_role_selectmenu)
.set_min_values(2)
.set_max_values(2)
.set_id("myselectid")
)
);

/* Reply to the user with our message. */
event.reply(msg);
}
});

bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register a command when the bot is ready */
bot.global_command_create(dpp::slashcommand("select", "Select something at random!", bot.me.id));
}
});

bot.start(dpp::st_wait);

return 0;
}
47 changes: 47 additions & 0 deletions docpages/example_code/default_select_value.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <dpp/dpp.h>
#include <dpp/unicode_emoji.h>

int main() {
dpp::cluster bot("token");

bot.on_log(dpp::utility::cout_logger());

/* The event is fired when someone issues your commands */
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
/* Check which command they ran */
if (event.command.get_command_name() == "select") {
/* Create a message */
dpp::message msg(event.command.channel_id, "This text has a select menu!");

/* Add an action row, and a select menu within the action row.
*
* Your default values are limited to max_values,
* meaning you can't add more default values than the allowed max values.
*/
msg.add_component(
dpp::component().add_component(
dpp::component()
.set_type(dpp::cot_role_selectmenu)
.set_min_values(2)
.set_max_values(2)
.add_default_value(dpp::snowflake{667756886443163648}, dpp::cdt_role)
.set_id("myselectid")
)
);

/* Reply to the user with our message. */
event.reply(msg);
}
});

bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_bot_commands>()) {
/* Create and register a command when the bot is ready */
bot.global_command_create(dpp::slashcommand("select", "Select something at random!", bot.me.id));
}
});

bot.start(dpp::st_wait);

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Components are anything that can be attached to a message or a \ref modal-dialog-interactions "modal" and interacted with, for example buttons and select menus. Due to being \ref interactions-and-components "interactions", they benefit from a low rate limit and are much more efficient than the old ways of using reactions for this purpose.

* \subpage components "Button components"
* \subpage components2 "Advanced button components"
* \subpage components3 "Select menu components"
* \subpage components
* \subpage components2
* \subpage components3
* \subpage default_select_value
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
\page components3 Using select menu components

This example demonstrates creating a select menu, receiving select menu clicks and sending a response message.
This tutorial will cover creating two types of select menus:
- A generic select menu with just text
- An auto-populated role select menu.

This first example demonstrates creating a select menu, receiving select menu clicks, and sending a response message.

\include{cpp} components3.cpp

This second example demonstrates creating a role select menu that is auto-populated by discord, and allowing people to select two options!

\note This type of select menu, along with other types (these types being: user, role, mentionable, and channel), always auto-fill. You never need to define the data in these types of select menus. All select menu types allow you to select multiple options.

\include{cpp} components3_rolemenu.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
\page default_select_value Setting default values on select menus.

This tutorial will cover how to set the default value for a select menu (that isn't a text select menu)!

\note **This only works for the following types of select menus: user, role, mentionable, and channel.** The default type of a select menu (as shown in \ref components3 "this page") does not work for this, as that supports a "placeholder".

\include{cpp} default_select_value.cpp

If all went well, you should have something like this!

\image html default_select_value.png

\image html default_select_value_2.png
Binary file added docpages/images/default_select_value.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added docpages/images/default_select_value_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 4 additions & 4 deletions include/dpp/appcommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -326,14 +326,14 @@ struct DPP_EXPORT interaction_response : public json_interface<interaction_respo
* @param j JSON to fill from
* @return interaction_response& Reference to self
*/
interaction_response& fill_from_json_impl(nlohmann::json* j);
virtual interaction_response& fill_from_json_impl(nlohmann::json* j);

/**
* @brief Build json for this object
*
* @return json JSON object
*/
json to_json_impl(bool with_id = false) const;
virtual json to_json_impl(bool with_id = false) const;

public:
/**
Expand Down Expand Up @@ -415,15 +415,15 @@ struct DPP_EXPORT interaction_modal_response : public interaction_response, publ
* @param j JSON to fill from
* @return interaction_response& Reference to self
*/
interaction_modal_response& fill_from_json_impl(nlohmann::json* j);
virtual interaction_modal_response& fill_from_json_impl(nlohmann::json* j);

/**
* @brief Build a json for this object
* @param with_id include id in json output
*
* @return json JSON object
*/
json to_json_impl(bool with_id = false) const;
virtual json to_json_impl(bool with_id = false) const;

public:
/**
Expand Down
40 changes: 40 additions & 0 deletions include/dpp/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,31 @@ enum component_style : uint8_t {
cos_link
};

/**
* Represents the type of a dpp::component_default_value
*
* @note They're different to discord's value types
*/
enum component_default_value_type: uint8_t {
cdt_user = 0,
cdt_role = 1,
cdt_channel = 2,
};

/**
* @brief A Default value structure for components
*/
struct DPP_EXPORT component_default_value {
/**
* @brief The type this default value represents
*/
component_default_value_type type;
/**
* @brief Default value. ID of a user, role, or channel
*/
dpp::snowflake id;
};

/**
* @brief An option for a select component
*/
Expand Down Expand Up @@ -295,6 +320,13 @@ class DPP_EXPORT component : public json_interface<component> {
*/
std::vector<uint8_t> channel_types;

/**
* List of default values for auto-populated select menu components. The amount of default values must be in the range defined by dpp::component::min_value and dpp::component::max_values.
*
* @note Only available for auto-populated select menu components, which include dpp::cot_user_selectmenu, dpp::cot_role_selectmenu, dpp::cot_mentionable_selectmenu, and dpp::cot_channel_selectmenu components.
*/
std::vector<component_default_value> default_values;

/** Disabled flag (for buttons)
*/
bool disabled;
Expand Down Expand Up @@ -503,6 +535,14 @@ class DPP_EXPORT component : public json_interface<component> {
*/
component& add_component(const component& c);

/**
* @brief Add a default value.
*
* @param id Default value. ID of a user, role, or channel
* @param type The type this default value represents
*/
component& add_default_value(const snowflake id, const component_default_value_type type);

/**
* @brief Set the emoji of the current sub-component.
* Only valid for buttons. Adding an emoji to a component
Expand Down
46 changes: 46 additions & 0 deletions src/dpp/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ component& component::fill_from_json_impl(nlohmann::json* j) {
if (j->contains("max_values") && j->at("max_values").is_number_integer()) {
max_values = j->at("max_values").get<int32_t>();
}
if (j->contains("default_values") && !j->at("default_values").is_null()) {
for (auto const &v : j->at("default_values")) {
component_default_value d;
d.id = snowflake_not_null(&v, "id");
d.type = static_cast<component_default_value_type>(int8_not_null(&v, "type"));
default_values.push_back(d);
}
}
if (type == cot_action_row) {
set_object_array_not_null<component>(j, "components", components);
} else if (type == cot_button) { // button specific fields
Expand Down Expand Up @@ -338,6 +346,21 @@ void to_json(json& j, const component& cp) {
if (cp.max_values >= 0) {
j["max_values"] = cp.max_values;
}
if (!cp.default_values.empty()) {
j["default_values"] = json::array();
for (auto const &v : cp.default_values) {
json o;
o["id"] = v.id;
if (v.type == dpp::cdt_role) {
o["type"] = "role";
} else if (v.type == dpp::cdt_channel) {
o["type"] = "channel";
} else if (v.type == dpp::cdt_user) {
o["type"] = "user";
}
j["default_values"].push_back(o);
}
}
} else if (cp.type == cot_channel_selectmenu) {
j["custom_id"] = cp.custom_id;
j["disabled"] = cp.disabled;
Expand All @@ -356,6 +379,21 @@ void to_json(json& j, const component& cp) {
j["channel_types"].push_back(type);
}
}
if (!cp.default_values.empty()) {
j["default_values"] = json::array();
for (auto const &v : cp.default_values) {
json o;
o["id"] = v.id;
if (v.type == dpp::cdt_role) {
o["type"] = "role";
} else if (v.type == dpp::cdt_channel) {
o["type"] = "channel";
} else if (v.type == dpp::cdt_user) {
o["type"] = "user";
}
j["default_values"].push_back(o);
}
}
}
}

Expand Down Expand Up @@ -439,6 +477,14 @@ component& component::add_select_option(const select_option &option) {
return *this;
}

component &component::add_default_value(const snowflake id, const component_default_value_type type) {
component_default_value default_value;
default_value.id = id;
default_value.type = type;
this->default_values.push_back(default_value);
return *this;
}

embed::~embed() = default;

embed::embed() : timestamp(0), color(0) {
Expand Down

0 comments on commit fa7ff38

Please sign in to comment.