Skip to content

Commit

Permalink
feat: user app commands and test program
Browse files Browse the repository at this point in the history
  • Loading branch information
braindigitalis committed Oct 21, 2024
1 parent 7a0e5f2 commit 72ab773
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 5 deletions.
2 changes: 1 addition & 1 deletion doxygen-awesome-css
26 changes: 25 additions & 1 deletion include/dpp/appcommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,30 @@ class DPP_EXPORT interaction : public managed, public json_interface<interaction
* is not for a command.
*/
std::string get_command_name() const;

/**
* @brief Get the user who installed the application for a given type.
* @param type Type of installation for the command, e.g. dpp::ait_guild_install or
* dpp::ait_user_install.
* @return The snowflake of the user. In the event this type is not allowed for the
* given command, this will return a default-initialised snowflake with value 0.
*/
dpp::snowflake get_authorizing_integration_owner(application_integration_types type) const;

/**
* @brief Returns true if this interaction occurred as a user-app interaction, e.g.
* within a DM or group DM, added to the user not a guild.
* @return true if a user-app interaction
*/
bool is_user_app_interaction() const;

/**
* @brief Returns true if this interaction occurred as a guild-invited interaction, e.g.
* within a guild's channel, or a DM of a user in that guild.
* @return true if a guild interaction
*/
bool is_guild_interaction() const;

};

/**
Expand Down Expand Up @@ -1466,7 +1490,7 @@ class DPP_EXPORT slashcommand : public managed, public json_interface<slashcomma
* D++ defaults this to false. Cannot be set to true in a guild
* command, only a global command.
*/
[[deprecated("Use contexts instead.")]] bool dm_permission;
bool dm_permission;

/**
* @brief Indicates whether the command is [age-restricted](https://discord.com/developers/docs/interactions/application-commands#agerestricted-commands).
Expand Down
2 changes: 1 addition & 1 deletion include/dpp/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -2229,7 +2229,7 @@ struct DPP_EXPORT message : public managed, json_interface<message> {
/**
* @brief Reference to an interaction
*/
DPP_DEPRECATED("Use interaction_metadata instead.") struct message_interaction_struct{
struct message_interaction_struct {
/**
* @brief ID of the interaction.
*/
Expand Down
1 change: 0 additions & 1 deletion src/dpp/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
************************************************************************************/
#include <dpp/application.h>
#include <dpp/discordevents.h>
#include <dpp/integration.h>
#include <dpp/json.h>

namespace dpp {
Expand Down
24 changes: 23 additions & 1 deletion src/dpp/slashcommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,11 @@ void from_json(const nlohmann::json& j, interaction& i) {
}

if (auto it = j.find("authorizing_integration_owners"); it != j.end()) {
it->get_to(i.authorizing_integration_owners);
for (auto owner = it->begin(); owner != it->end(); ++owner) {
auto type = static_cast<application_integration_types>(from_string<int>(owner.key()));
std::string owner_flake = owner.value();
i.authorizing_integration_owners[type] = dpp::snowflake(owner_flake);
}
}

if(j.contains("entitlements")) {
Expand All @@ -765,6 +769,24 @@ void from_json(const nlohmann::json& j, interaction& i) {
}
}

dpp::snowflake interaction::get_authorizing_integration_owner(application_integration_types type) const {
dpp::snowflake rv;
auto i = this->authorizing_integration_owners.find(type);
if (i != this->authorizing_integration_owners.end()) {
rv = i->second;
}
return rv;
}

bool interaction::is_user_app_interaction() const {
return this->authorizing_integration_owners.find(ait_user_install) != this->authorizing_integration_owners.end();
}

bool interaction::is_guild_interaction() const {
return this->authorizing_integration_owners.find(ait_guild_install) != this->authorizing_integration_owners.end();
}


interaction_response& interaction_response::add_autocomplete_choice(const command_option_choice& achoice) {
if (autocomplete_choices.size() < AUTOCOMPLETE_MAX_CHOICES) {
this->autocomplete_choices.emplace_back(achoice);
Expand Down
65 changes: 65 additions & 0 deletions src/userapptest/userapp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/************************************************************************************
*
* D++, A Lightweight C++ library for Discord
*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2021 Craig Edwards and D++ contributors
* (https://github.com/brainboxdotcc/DPP/graphs/contributors)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
************************************************************************************/

#include <dpp/dpp.h>
#include <iostream>

int main() {
char* t = getenv("DPP_UNIT_TEST_TOKEN");

if (!t) {
std::cerr << "Missing DPP_UNIT_TEST_TOKEN\n";
exit(1);
}

dpp::cluster bot(t, dpp::i_default_intents);
bot.on_log(dpp::utility::cout_logger());

bot.on_ready([&bot](const auto& event) {
if (dpp::run_once<struct boot_t>()) {
/**
* Create a slash command which has interaction context 'itc_private_channel'.
* This is a user-app command which can be executed anywhere and is added to the user's profile.
*/
bot.global_bulk_command_create({
dpp::slashcommand("userapp", "Test user app command", bot.me.id)
.set_interaction_contexts({dpp::itc_guild, dpp::itc_bot_dm, dpp::itc_private_channel})
});
}
});

bot.on_slashcommand([](const auto& e) {
/**
* Simple test output that shows the context of the command
*/
if (e.command.get_command_name() != "userapp") {
return;
}
e.reply("This is the `/userapp` command." + std::string(
e.command.is_user_app_interaction() ?
" Executing as a user interaction owned by user: <@" + e.command.get_authorizing_integration_owner(dpp::ait_user_install).str() + ">" :
" Executing as a guild interaction on guild id " + e.command.guild_id.str()
));
});

bot.start(dpp::st_wait);
}

0 comments on commit 72ab773

Please sign in to comment.