From 6efbe5fb8d702d1f0b7f31548a83e226bc588163 Mon Sep 17 00:00:00 2001 From: Arkrissym Date: Sun, 13 Oct 2024 17:39:05 +0200 Subject: [PATCH] add fields nsfw,integration_types, contexts, handler. remove deprecated dm_permission --- Discord.C++/ApplicationCommand.cpp | 32 +++++++++++++++++-- Discord.C++/ApplicationCommand.h | 51 +++++++++++++++++++++++++----- test_bot/main.cpp | 14 +++++--- 3 files changed, 83 insertions(+), 14 deletions(-) diff --git a/Discord.C++/ApplicationCommand.cpp b/Discord.C++/ApplicationCommand.cpp index 7a4b82c..8c9ce0d 100644 --- a/Discord.C++/ApplicationCommand.cpp +++ b/Discord.C++/ApplicationCommand.cpp @@ -13,9 +13,25 @@ DiscordCPP::ApplicationCommand::ApplicationCommand(const json& data, const std:: name = data.at("name").get(); description = data.at("description").get(); - dm_permission = get_or_else(data, "dm_permission", true); + nsfw = get_or_else(data, "nsfw", false); version = data.at("version").get(); + if (has_value(data, "integration_types")) { + for (const json& integration_type : data.at("integration_types")) { + integration_types.push_back(static_cast(integration_type.get())); + } + } + + if (has_value(data, "contexts")) { + for (const json& context : data.at("contexts")) { + contexts.push_back(static_cast(context.get())); + } + } + + if (has_value(data, "handler")) { + handler = static_cast(data.at("handler").get()); + } + if (has_value(data, "options")) { for (const json& option : data.at("options")) { options.push_back(DiscordCPP::ApplicationCommandOptionHelper::application_command_option_from_json(option)); @@ -28,7 +44,19 @@ json DiscordCPP::ApplicationCommand::to_json() { data["type"] = type; data["name"] = name; data["description"] = description; - data["dm_permission"] = dm_permission; + data["nsfw"] = nsfw; + + for (auto& integration_type : integration_types) { + data["integration_types"].push_back(integration_type); + } + + for (auto& context : contexts) { + data["contexts"].push_back(context); + } + + if (handler.has_value()) { + data["handler"] = handler.value(); + } for (auto& option : options) { data["options"].push_back(std::visit([](auto v) { return v.to_json(); }, option)); diff --git a/Discord.C++/ApplicationCommand.h b/Discord.C++/ApplicationCommand.h index ff7a2cb..719d982 100644 --- a/Discord.C++/ApplicationCommand.h +++ b/Discord.C++/ApplicationCommand.h @@ -14,7 +14,24 @@ class ApplicationCommand : public DiscordObject { enum Type { CHAT_INPUT = 1, USER = 2, - MESSAGE = 3 + MESSAGE = 3, + PRIMARY_ENTRY_POINT = 4 + }; + + enum IntegrationType { + GUILD_INSTALL = 0, + USER_INSTALL = 1 + }; + + enum ContextType { + GUILD = 0, + BOT_DM = 1, + PRIVATE_CHANNEL = 2 + }; + + enum HandlerType { + APP_HANDLER = 1, + DISCORD_LAUNCH_ACTIVITY = 2 }; private: @@ -33,10 +50,16 @@ class ApplicationCommand : public DiscordObject { /// Parameters for the command, max of 25. std::vector options; // default_member_permissions - /// Indicates wether the command is enabled in DMs. Defaults to true. - bool dm_permission = true; + /// Indicates whether the command is age-restricted, defaults to false. + bool nsfw = false; + /// Installation contexts where the command is available, only for globally-scoped commands. Defaults to your app's configured contexts. + std::vector integration_types; + /// Interaction context(s) where the command can be used, only for globally-scoped commands. Defaults to all. + std::vector contexts; /// Autoincrementing version identifier updated during substantial record changes. std::string version; + /// Determines whether the interaction is handled by the app's interactions handler or by Discord. Use when type is PRIMARY_ENTRY_POINT. + std::optional handler; public: DLL_EXPORT ApplicationCommand() = default; @@ -59,10 +82,16 @@ class ApplicationCommand : public DiscordObject { DLL_EXPORT Type get_type() { return type; } /// @return Parameters for the command, max of 25. DLL_EXPORT std::vector get_options() { return options; } - /// @return Indicates wether the command is enabled in DMs. Defaults to true. - DLL_EXPORT bool has_dm_permission() { return dm_permission; } + /// @return Indicates whether the command is age-restricted, defaults to false. + DLL_EXPORT bool is_nsfw() { return nsfw; } + /// @return Installation contexts where the command is available. + DLL_EXPORT std::vector get_integration_types() { return integration_types; } + /// @return Interaction context(s) where the command can be used. + DLL_EXPORT std::vector get_contexts() { return contexts; } /// @return Autoincrementing version identifier updated during substantial record changes. DLL_EXPORT std::string get_version() { return version; } + /// @return The handler type for PRIMARY_ENTRY_POINT. + DLL_EXPORT std::optional get_handler() { return handler; } /// Set the guild of the command. DLL_EXPORT void set_guild_id(std::string guild_id) { this->guild_id.emplace(guild_id); } @@ -74,7 +103,13 @@ class ApplicationCommand : public DiscordObject { DLL_EXPORT void set_type(Type type) { this->type = type; } /// Add parameters for the command, max of 25. DLL_EXPORT void add_option(ApplicationCommandOptionVariant option) { options.push_back(option); } - /// Set indicator wether the command is enabled in DMs. - DLL_EXPORT void set_dm_permission(bool dm_permission) { this->dm_permission = dm_permission; } + /// Set whether the command is age-restricted, defaults to false. + DLL_EXPORT void set_nsfw(bool nsfw) { this->nsfw = nsfw; } + /// Add installation context where the command is available. + DLL_EXPORT void add_integration_types(IntegrationType integration_type) { integration_types.push_back(integration_type); } + /// Add interaction context where the command can be used. + DLL_EXPORT void add_contexts(ContextType context) { contexts.push_back(context); } + /// Set the handler type for PRIMARY_ENTRY_POINT. + DLL_EXPORT void get_handler(HandlerType handler) { this->handler.emplace(handler); } }; -} // namespace DiscordCPP \ No newline at end of file +} // namespace DiscordCPP diff --git a/test_bot/main.cpp b/test_bot/main.cpp index e9c2466..416653f 100644 --- a/test_bot/main.cpp +++ b/test_bot/main.cpp @@ -2,6 +2,8 @@ #include #include +#include "ApplicationCommand.h" + #ifndef _WIN32 #include #endif @@ -53,12 +55,18 @@ class Client : public Discord { ping.set_name("ping"); ping.set_description("Ping the bot"); ping.set_type(ApplicationCommand::Type::CHAT_INPUT); + ping.add_integration_types(ApplicationCommand::IntegrationType::GUILD_INSTALL); + ping.add_integration_types(ApplicationCommand::IntegrationType::USER_INSTALL); + ping.add_contexts(ApplicationCommand::ContextType::BOT_DM); + ping.add_contexts(ApplicationCommand::ContextType::PRIVATE_CHANNEL); create_application_command(ping); ApplicationCommand update = ApplicationCommand(); update.set_name("update"); update.set_description("Responds with a message and updates it after 5 seconds"); update.set_type(ApplicationCommand::Type::CHAT_INPUT); + update.add_integration_types(ApplicationCommand::IntegrationType::GUILD_INSTALL); + update.add_contexts(ApplicationCommand::ContextType::GUILD); create_application_command(update); ApplicationCommand msg = ApplicationCommand(); @@ -103,13 +111,11 @@ class Client : public Discord { } void on_user_ban(User user, Guild guild) override { - log.info("User " + string(user) + " has been banned from Guild " + - string(guild)); + log.info("User " + string(user) + " has been banned from Guild " + string(guild)); } void on_user_unban(User user, Guild guild) override { - log.info("User " + string(user) + " has been unbanned from Guild " + - string(guild)); + log.info("User " + string(user) + " has been unbanned from Guild " + string(guild)); } void on_user_join(Member member, Guild guild) override {