Skip to content

Commit 9ef17d4

Browse files
feat: support for pre-generated component json, in the same way we support pre-generated embed json (#1427)
1 parent cfb5b75 commit 9ef17d4

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

include/dpp/message.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2389,6 +2389,12 @@ struct DPP_EXPORT message : public managed, json_interface<message> {
23892389
*/
23902390
std::vector<dpp::component> components;
23912391

2392+
/**
2393+
* @brief Pre-generated components as JSON. This overrides the components
2394+
* array if it is set.
2395+
*/
2396+
json components_json;
2397+
23922398
/**
23932399
* @brief When this message was sent.
23942400
*/
@@ -2827,6 +2833,17 @@ struct DPP_EXPORT message : public managed, json_interface<message> {
28272833
*/
28282834
message& add_component(const component& c);
28292835

2836+
/**
2837+
* @brief Add pre-generated components to a message
2838+
*
2839+
* @note This is intended to accept pre-generated component
2840+
* json from external tools such as https://discord.builders
2841+
*
2842+
* @param json components json to add. The JSON will be validated
2843+
* @return message& reference to self
2844+
*/
2845+
message& add_json_components(const std::string& json);
2846+
28302847
/**
28312848
* @brief Add a component to a message
28322849
*

src/dpp/message.cpp

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -880,6 +880,12 @@ message& message::add_component(const component& c) {
880880
return *this;
881881
}
882882

883+
message& message::add_json_components(const std::string& json) {
884+
set_flags(flags | m_using_components_v2);
885+
components_json = json::parse(json);
886+
return *this;
887+
}
888+
883889
message& message::add_component_v2(const component& c) {
884890
/* This is just an alias for readability in apps */
885891
return add_component(c);
@@ -1378,20 +1384,25 @@ json message::to_json(bool with_id, bool is_interaction_response) const {
13781384
}
13791385
}
13801386

1381-
if ((flags & m_using_components_v2) == 0) {
1382-
j["components"] = json::array();
1383-
for (auto & component : components) {
1384-
json n;
1385-
n["type"] = cot_action_row;
1386-
n["components"] = {};
1387-
for (auto & subcomponent : component.components) {
1388-
json sn = subcomponent;
1389-
n["components"].push_back(sn);
1387+
if (!components_json.empty()) {
1388+
j["components"] = components_json;
1389+
} else {
1390+
1391+
if ((flags & m_using_components_v2) == 0) {
1392+
j["components"] = json::array();
1393+
for (auto &component: components) {
1394+
json n;
1395+
n["type"] = cot_action_row;
1396+
n["components"] = {};
1397+
for (auto &subcomponent: component.components) {
1398+
json sn = subcomponent;
1399+
n["components"].push_back(sn);
1400+
}
1401+
j["components"].push_back(n);
13901402
}
1391-
j["components"].push_back(n);
1403+
} else {
1404+
recurse_components(j, components);
13921405
}
1393-
} else {
1394-
recurse_components(j, components);
13951406
}
13961407

13971408
j["attachments"] = json::array();

0 commit comments

Comments
 (0)