Skip to content

Commit

Permalink
feat: add interaction_context_type support
Browse files Browse the repository at this point in the history
  • Loading branch information
raxyte committed Aug 13, 2024
1 parent 5e5ee60 commit 03bb4b2
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
37 changes: 37 additions & 0 deletions include/dpp/appcommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -952,6 +972,10 @@ class DPP_EXPORT interaction : public managed, public json_interface<interaction
virtual json to_json_impl(bool with_id = false) const;

public:
/**
* @brief Context where the interaction was triggered from
*/
std::optional<interaction_context_type> context;
/**
* @brief ID of the application this interaction is for.
*/
Expand Down Expand Up @@ -1420,6 +1444,11 @@ class DPP_EXPORT slashcommand : public managed, public json_interface<slashcomma
*/
permission default_member_permissions;

/**
* @brief Interaction context(s) where the command can be used, only for globally-scoped commands. By default, all interaction context types included for new commands.
*/
std::vector<interaction_context_type> 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
Expand Down Expand Up @@ -1543,6 +1572,14 @@ class DPP_EXPORT slashcommand : public managed, public json_interface<slashcomma
*/
slashcommand& set_application_id(snowflake i);

/**
* @brief Set the interaction contexts for the command
*
* @param contexts the contexts to set
* @return slashcommand& reference to self for chaining of calls
*/
slashcommand& set_interaction_contexts(std::vector<interaction_context_type> contexts);

/**
* @brief Adds a permission to the command
*
Expand Down
20 changes: 19 additions & 1 deletion src/dpp/slashcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
#include <dpp/json.h>
#include <dpp/stringops.h>
#include <dpp/cache.h>
#include <iostream>
#include <algorithm>
#include <iterator>

namespace dpp {

Expand Down Expand Up @@ -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<command_option>(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;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -294,6 +303,11 @@ slashcommand& slashcommand::set_application_id(snowflake i) {
return *this;
}

slashcommand& slashcommand::set_interaction_contexts(std::vector<interaction_context_type> contexts) {
this->contexts = std::move(contexts);
return *this;
}

slashcommand& slashcommand::add_permission(const command_permission& p) {
this->permissions.emplace_back(p);
return *this;
Expand Down Expand Up @@ -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<interaction_context_type>(*it);
}

if(j.contains("entitlements")) {
for (auto& entitle : j["entitlements"]) {
i.entitlements.emplace_back(entitlement().fill_from_json(const_cast<json*>(&entitle)));
Expand Down

0 comments on commit 03bb4b2

Please sign in to comment.