Skip to content
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
17 changes: 17 additions & 0 deletions include/dpp/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -2389,6 +2389,12 @@ struct DPP_EXPORT message : public managed, json_interface<message> {
*/
std::vector<dpp::component> components;

/**
* @brief Pre-generated components as JSON. This overrides the components
* array if it is set.
*/
json components_json;

/**
* @brief When this message was sent.
*/
Expand Down Expand Up @@ -2827,6 +2833,17 @@ struct DPP_EXPORT message : public managed, json_interface<message> {
*/
message& add_component(const component& c);

/**
* @brief Add pre-generated components to a message
*
* @note This is intended to accept pre-generated component
* json from external tools such as https://discord.builders
*
* @param json components json to add. The JSON will be validated
* @return message& reference to self
*/
message& add_json_components(const std::string& json);

/**
* @brief Add a component to a message
*
Expand Down
35 changes: 23 additions & 12 deletions src/dpp/message.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,12 @@ message& message::add_component(const component& c) {
return *this;
}

message& message::add_json_components(const std::string& json) {
set_flags(flags | m_using_components_v2);
components_json = json::parse(json);
return *this;
}

message& message::add_component_v2(const component& c) {
/* This is just an alias for readability in apps */
return add_component(c);
Expand Down Expand Up @@ -1378,20 +1384,25 @@ json message::to_json(bool with_id, bool is_interaction_response) const {
}
}

if ((flags & m_using_components_v2) == 0) {
j["components"] = json::array();
for (auto & component : components) {
json n;
n["type"] = cot_action_row;
n["components"] = {};
for (auto & subcomponent : component.components) {
json sn = subcomponent;
n["components"].push_back(sn);
if (!components_json.empty()) {
j["components"] = components_json;
} else {

if ((flags & m_using_components_v2) == 0) {
j["components"] = json::array();
for (auto &component: components) {
json n;
n["type"] = cot_action_row;
n["components"] = {};
for (auto &subcomponent: component.components) {
json sn = subcomponent;
n["components"].push_back(sn);
}
j["components"].push_back(n);
}
j["components"].push_back(n);
} else {
recurse_components(j, components);
}
} else {
recurse_components(j, components);
}

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