Skip to content

Commit

Permalink
add fields nsfw,integration_types, contexts, handler. remove deprecat…
Browse files Browse the repository at this point in the history
…ed dm_permission
  • Loading branch information
Arkrissym committed Oct 13, 2024
1 parent 8719a93 commit 6efbe5f
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 14 deletions.
32 changes: 30 additions & 2 deletions Discord.C++/ApplicationCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,25 @@ DiscordCPP::ApplicationCommand::ApplicationCommand(const json& data, const std::

name = data.at("name").get<std::string>();
description = data.at("description").get<std::string>();
dm_permission = get_or_else<bool>(data, "dm_permission", true);
nsfw = get_or_else<bool>(data, "nsfw", false);
version = data.at("version").get<std::string>();

if (has_value(data, "integration_types")) {
for (const json& integration_type : data.at("integration_types")) {
integration_types.push_back(static_cast<DiscordCPP::ApplicationCommand::IntegrationType>(integration_type.get<int>()));
}
}

if (has_value(data, "contexts")) {
for (const json& context : data.at("contexts")) {
contexts.push_back(static_cast<DiscordCPP::ApplicationCommand::ContextType>(context.get<int>()));
}
}

if (has_value(data, "handler")) {
handler = static_cast<DiscordCPP::ApplicationCommand::HandlerType>(data.at("handler").get<int>());
}

if (has_value(data, "options")) {
for (const json& option : data.at("options")) {
options.push_back(DiscordCPP::ApplicationCommandOptionHelper::application_command_option_from_json(option));
Expand All @@ -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));
Expand Down
51 changes: 43 additions & 8 deletions Discord.C++/ApplicationCommand.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -33,10 +50,16 @@ class ApplicationCommand : public DiscordObject {
/// Parameters for the command, max of 25.
std::vector<ApplicationCommandOptionVariant> 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<IntegrationType> integration_types;
/// Interaction context(s) where the command can be used, only for globally-scoped commands. Defaults to all.
std::vector<ContextType> 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<HandlerType> handler;

public:
DLL_EXPORT ApplicationCommand() = default;
Expand All @@ -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<ApplicationCommandOptionVariant> 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<IntegrationType> get_integration_types() { return integration_types; }
/// @return Interaction context(s) where the command can be used.
DLL_EXPORT std::vector<ContextType> 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<HandlerType> 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); }
Expand All @@ -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
} // namespace DiscordCPP
14 changes: 10 additions & 4 deletions test_bot/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <thread>
#include <vector>

#include "ApplicationCommand.h"

#ifndef _WIN32
#include <cstdlib>
#endif
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 6efbe5f

Please sign in to comment.