From 03bb4b29f0323393ccd9a372066cb398ae53a5a7 Mon Sep 17 00:00:00 2001 From: Raghav Narang Date: Tue, 13 Aug 2024 03:24:24 +0530 Subject: [PATCH] feat: add interaction_context_type support --- include/dpp/appcommand.h | 37 +++++++++++++++++++++++++++++++++++++ src/dpp/slashcommand.cpp | 20 +++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/include/dpp/appcommand.h b/include/dpp/appcommand.h index e628bd97f8..f98c70f497 100644 --- a/include/dpp/appcommand.h +++ b/include/dpp/appcommand.h @@ -773,6 +773,26 @@ enum interaction_type { it_modal_submit = 5, }; +/* +* @brief Context type where the interaction can be used or triggered from, e.g. guild, user etc +*/ +enum interaction_context_type { + /** + * @brief Interaction can be used within servers + */ + itc_guild = 0, + + /** + * @brief Interaction can be used within DMs with the app's bot user + */ + itc_bot_dm = 1, + + /** + * @brief Interaction can be used within Group DMs and DMs other than the app's bot user + */ + itc_private_channel = 2, +}; + /** * @brief Right-click context menu types */ @@ -952,6 +972,10 @@ class DPP_EXPORT interaction : public managed, public json_interface context; /** * @brief ID of the application this interaction is for. */ @@ -1420,6 +1444,11 @@ class DPP_EXPORT slashcommand : public managed, public json_interface contexts; + /** * @brief True if this command should be allowed in a DM * D++ defaults this to false. Cannot be set to true in a guild @@ -1543,6 +1572,14 @@ class DPP_EXPORT slashcommand : public managed, public json_interface contexts); + /** * @brief Adds a permission to the command * diff --git a/src/dpp/slashcommand.cpp b/src/dpp/slashcommand.cpp index 77dd4bfebe..616691c2d6 100644 --- a/src/dpp/slashcommand.cpp +++ b/src/dpp/slashcommand.cpp @@ -24,7 +24,8 @@ #include #include #include -#include +#include +#include namespace dpp { @@ -74,6 +75,9 @@ slashcommand& slashcommand::fill_from_json_impl(nlohmann::json* j) { type = (slashcommand_contextmenu_type)int8_not_null(j, "type"); set_object_array_not_null(j, "options", options); // command_option fills recursive + if (auto it = j->find("contexts"); it != j->end()) { + std::copy(it->begin(), it->end(), std::back_inserter(contexts)); + } return *this; } @@ -252,6 +256,11 @@ void to_json(json& j, const slashcommand& p) { } } + // TODO: Maybe a std::optional is better to differentiate + if (p.contexts.size()) { + j["contexts"] = p.contexts; + } + // DEPRECATED // j["default_permission"] = p.default_permission; j["application_id"] = std::to_string(p.application_id); @@ -294,6 +303,11 @@ slashcommand& slashcommand::set_application_id(snowflake i) { return *this; } +slashcommand& slashcommand::set_interaction_contexts(std::vector contexts) { + this->contexts = std::move(contexts); + return *this; +} + slashcommand& slashcommand::add_permission(const command_permission& p) { this->permissions.emplace_back(p); return *this; @@ -728,6 +742,10 @@ void from_json(const nlohmann::json& j, interaction& i) { } } + if (auto it = j.find("context"); it != j.end()) { + i.context = static_cast(*it); + } + if(j.contains("entitlements")) { for (auto& entitle : j["entitlements"]) { i.entitlements.emplace_back(entitlement().fill_from_json(const_cast(&entitle)));