diff --git a/.clang-format b/.clang-format index 661aa6f..6f3d7ea 100644 --- a/.clang-format +++ b/.clang-format @@ -143,7 +143,7 @@ BreakFunctionDefinitionParameters: false BreakInheritanceList: BeforeColon BreakStringLiterals: true BreakTemplateDeclarations: Yes -ColumnLimit: 80 +ColumnLimit: 120 CommentPragmas: "^ IWYU pragma:" CompactNamespaces: false ConstructorInitializerIndentWidth: 4 @@ -355,4 +355,3 @@ WhitespaceSensitiveMacros: - STRINGIZE WrapNamespaceBodyWithEmptyLines: Leave ... - diff --git a/src/01_basics/01_first_bot/src/main.cpp b/src/01_basics/01_first_bot/src/main.cpp index 0c8b891..324ef8c 100644 --- a/src/01_basics/01_first_bot/src/main.cpp +++ b/src/01_basics/01_first_bot/src/main.cpp @@ -11,9 +11,7 @@ int main() { dotenv::init(); // Make sure the DISCORD_TOKEN was loaded if (!std::getenv("DISCORD_TOKEN")) { - std::cout - << "Failed to load the discord token from .env or the environment" - << std::endl; + std::cout << "Failed to load the discord token from .env or the environment" << std::endl; exit(EXIT_FAILURE); } // Get the bot token diff --git a/src/01_basics/02_embeds/src/main.cpp b/src/01_basics/02_embeds/src/main.cpp index 22f1f12..8b9657a 100644 --- a/src/01_basics/02_embeds/src/main.cpp +++ b/src/01_basics/02_embeds/src/main.cpp @@ -13,9 +13,7 @@ int main() { dotenv::init(); // Check to make sure the token was loaded if (!std::getenv("DISCORD_TOKEN")) { - std::cout - << "Failed to load the discord token from .env or the environment" - << std::endl; + std::cout << "Failed to load the discord token from .env or the environment" << std::endl; exit(EXIT_FAILURE); } // Load in the token @@ -43,8 +41,7 @@ int main() { // Set the title url .set_url("https://dpp.dev") // Set the author info - .set_author("Firewire", "https://dpp.dev", - "https://dpp.dev/DPP-Logo.png") + .set_author("Firewire", "https://dpp.dev", "https://dpp.dev/DPP-Logo.png") .set_description("Description") // Set the thumbnail, this is a small image off the right .set_thumbnail("https://dpp.dev/DPP-Logo.png") @@ -55,9 +52,7 @@ int main() { // Set the main image, this will be the center large image .set_image("https://dpp.dev/DPP-Logo.png") // Set the footer - .set_footer(dpp::embed_footer() - .set_text("Footer text") - .set_icon("https://dpp.dev/DPP-Logo.png")) + .set_footer(dpp::embed_footer().set_text("Footer text").set_icon("https://dpp.dev/DPP-Logo.png")) // Add a timestamp, // time() returns a timer of seconds // using a 0 for the arg just returns the current time @@ -77,8 +72,7 @@ int main() { // on_ready() is called once reconnected. // We dont want to re-register when this happens) if (dpp::run_once()) { - bot.global_command_create( - dpp::slashcommand("embed", "Send an embed", bot.me.id)); + bot.global_command_create(dpp::slashcommand("embed", "Send an embed", bot.me.id)); } }); diff --git a/src/01_basics/03_private_messages/src/main.cpp b/src/01_basics/03_private_messages/src/main.cpp index 0b469ec..852c8e7 100644 --- a/src/01_basics/03_private_messages/src/main.cpp +++ b/src/01_basics/03_private_messages/src/main.cpp @@ -15,9 +15,7 @@ int main() { dotenv::init(); // Check to make sure the token was loaded if (!std::getenv("DISCORD_TOKEN")) { - std::cout - << "Failed to load the discord token from .env or the environment" - << std::endl; + std::cout << "Failed to load the discord token from .env or the environment" << std::endl; exit(EXIT_FAILURE); } // Load in the token into a string @@ -61,15 +59,11 @@ int main() { // Reply back with a message saying the direct message // failed to send if (user == event.command.get_issuing_user().id) { - event.reply( - dpp::message("I couldnt send you a message.") - .set_flags(dpp::m_ephemeral)); + event.reply(dpp::message("I couldnt send you a message.").set_flags(dpp::m_ephemeral)); } else { - event.reply( - dpp::message( - "I couldnt send a message to that user. " - "Please check that is a valid user.") - .set_flags(dpp::m_ephemeral)); + event.reply(dpp::message("I couldnt send a message to that user. " + "Please check that is a valid user.") + .set_flags(dpp::m_ephemeral)); } return; } @@ -77,14 +71,10 @@ int main() { // In the case the direct message was able to be sent, send // a confirmation message if (user == event.command.get_issuing_user().id) { - event.reply( - dpp::message("I've sent you a private message") - .set_flags(dpp::m_ephemeral)); + event.reply(dpp::message("I've sent you a private message").set_flags(dpp::m_ephemeral)); } else { event.reply( - dpp::message( - "I've sent a private message to that user.") - .set_flags(dpp::m_ephemeral)); + dpp::message("I've sent a private message to that user.").set_flags(dpp::m_ephemeral)); } }); } @@ -94,11 +84,9 @@ int main() { bot.on_ready([&bot](const dpp::ready_t& event) { if (dpp::run_once()) { // Create the /pm slashcommand - dpp::slashcommand command("pm", "Send a private message.", - bot.me.id); + dpp::slashcommand command("pm", "Send a private message.", bot.me.id); // Add the "user" option to the command - command.add_option(dpp::command_option( - dpp::co_mentionable, "user", "The user to message", false)); + command.add_option(dpp::command_option(dpp::co_mentionable, "user", "The user to message", false)); // Register the command with discord bot.global_command_create(command); } diff --git a/src/01_basics/04_adding_files/src/main.cpp b/src/01_basics/04_adding_files/src/main.cpp index 7e7fd78..65d62de 100644 --- a/src/01_basics/04_adding_files/src/main.cpp +++ b/src/01_basics/04_adding_files/src/main.cpp @@ -15,9 +15,7 @@ int main() { dotenv::init(); // Make sure the DISCORD_TOKEN was loaded in if (!std::getenv("DISCORD_TOKEN")) { - std::cout - << "Failed to load the discord token from .env or the environment" - << std::endl; + std::cout << "Failed to load the discord token from .env or the environment" << std::endl; exit(EXIT_FAILURE); } // Load in the token @@ -32,8 +30,7 @@ int main() { bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) { if (event.command.get_command_name() == "file") { // Create the message - dpp::message msg(event.command.channel_id, - "Hey there, I've got a file for you!"); + dpp::message msg(event.command.channel_id, "Hey there, I've got a file for you!"); // Add the "README.md" file to the message msg.add_file("README.md", dpp::utility::read_file("README.md")); // Send the message @@ -43,16 +40,13 @@ int main() { // Handle the external command if (event.command.get_command_name() == "external") { bot.request("https://dpp.dev/DPP-Logo.png", dpp::m_get, - [event](const dpp::http_request_completion_t& - httpRequestCompletion) { + [event](const dpp::http_request_completion_t& httpRequestCompletion) { // Create the message - dpp::message msg(event.command.channel_id, - "Heres an external image!"); + dpp::message msg(event.command.channel_id, "Heres an external image!"); // Make sure the http request was completed if (httpRequestCompletion.status == 200) { // Add the body of the request to the msg - msg.add_file("logo.png", - httpRequestCompletion.body); + msg.add_file("logo.png", httpRequestCompletion.body); } // Send the message event.reply(msg); @@ -62,15 +56,13 @@ int main() { // Handle the embed command if (event.command.get_command_name() == "embed") { bot.request("https://dpp.dev/DPP-Logo.png", dpp::m_get, - [event](const dpp::http_request_completion_t& - httpRequestCompletion) { + [event](const dpp::http_request_completion_t& httpRequestCompletion) { // Create the message dpp::message msg(event.command.channel_id, ""); // Make sure the http request completed if (httpRequestCompletion.status == 200) { // Add the file to the message - msg.add_file("logo.png", - httpRequestCompletion.body); + msg.add_file("logo.png", httpRequestCompletion.body); // Create the embed dpp::embed embed; // Set the image for the embed as the image @@ -90,18 +82,15 @@ int main() { // Create and register the file command // This command will send a copy of the README.md file in the root // of the project - dpp::slashcommand fileCommand( - "file", "Send a message with a file attached!", bot.me.id); + dpp::slashcommand fileCommand("file", "Send a message with a file attached!", bot.me.id); bot.global_command_create(fileCommand); // Create and register the external command - dpp::slashcommand externalCommand( - "external", "Send an image from an external source", bot.me.id); + dpp::slashcommand externalCommand("external", "Send an image from an external source", bot.me.id); bot.global_command_create(externalCommand); // Create and register the embed command - dpp::slashcommand embedCommand( - "embed", "Send a message with an image embed", bot.me.id); + dpp::slashcommand embedCommand("embed", "Send a message with an image embed", bot.me.id); bot.global_command_create(embedCommand); } }); diff --git a/src/01_basics/05_webhooks/src/main.cpp b/src/01_basics/05_webhooks/src/main.cpp index 43d2e99..28b1f2b 100644 --- a/src/01_basics/05_webhooks/src/main.cpp +++ b/src/01_basics/05_webhooks/src/main.cpp @@ -12,8 +12,7 @@ int main() { // Load in the .env file into the environment dotenv::init(); if (!std::getenv("DISCORD_WEBHOOK")) { - std::cout << "Failed to get webhook from .env or environment" - << std::endl; + std::cout << "Failed to get webhook from .env or environment" << std::endl; exit(EXIT_FAILURE); } diff --git a/src/01_basics/06_callbacks/src/main.cpp b/src/01_basics/06_callbacks/src/main.cpp index 86efc47..ab9792f 100644 --- a/src/01_basics/06_callbacks/src/main.cpp +++ b/src/01_basics/06_callbacks/src/main.cpp @@ -19,9 +19,7 @@ int main() { // Load the .env into the environment dotenv::init(); if (!std::getenv("DISCORD_TOKEN")) { - std::cout - << "Failed to get the discord token from the .env or enviroment" - << std::endl; + std::cout << "Failed to get the discord token from the .env or enviroment" << std::endl; exit(EXIT_FAILURE); } // Get the bot token @@ -37,57 +35,47 @@ int main() { if (event.command.get_command_name() == "msgs-get") { int64_t limit = std::get(event.get_parameter("quantity")); - bot.messages_get( - event.command.channel_id, 0, 0, 0, limit, - [event](const dpp::confirmation_callback_t& callback) { - if (callback.is_error()) { - std::cout << callback.get_error().message << std::endl; - return; - } + bot.messages_get(event.command.channel_id, 0, 0, 0, limit, + [event](const dpp::confirmation_callback_t& callback) { + if (callback.is_error()) { + std::cout << callback.get_error().message << std::endl; + return; + } - auto messages = callback.get(); + auto messages = callback.get(); - std::string contents; - for (const auto& x : messages) { - contents += x.second.content + "\n"; - } + std::string contents; + for (const auto& x : messages) { + contents += x.second.content + "\n"; + } - event.reply(contents); - }); + event.reply(contents); + }); } else if (event.command.get_command_name() == "channel-create") { - dpp::channel channel = dpp::channel().set_name("test").set_guild_id( - event.command.guild_id); + dpp::channel channel = dpp::channel().set_name("test").set_guild_id(event.command.guild_id); - bot.channel_create( - channel, - [&bot, - event](const dpp::confirmation_callback_t& callback) -> void { - if (callback.is_error()) { - bot.log(dpp::loglevel::ll_error, - callback.get_error().message); - return; - } + bot.channel_create(channel, [&bot, event](const dpp::confirmation_callback_t& callback) -> void { + if (callback.is_error()) { + bot.log(dpp::loglevel::ll_error, callback.get_error().message); + return; + } - auto channel = callback.get(); - dpp::message message = - dpp::message("The channel's name is '" + channel.name + - "', ID is '" + std::to_string(channel.id) + - "', and the type is '" + - std::to_string(channel.get_type()) + "'."); - event.reply(message); - }); + auto channel = callback.get(); + dpp::message message = + dpp::message("The channel's name is '" + channel.name + "', ID is '" + std::to_string(channel.id) + + "', and the type is '" + std::to_string(channel.get_type()) + "'."); + event.reply(message); + }); } else if (event.command.get_command_name() == "msg-error") { - bot.message_get( - 0, 0, - [event](const dpp::confirmation_callback_t& callback) -> void { - if (callback.is_error()) { - event.reply(callback.get_error().message); - return; - } + bot.message_get(0, 0, [event](const dpp::confirmation_callback_t& callback) -> void { + if (callback.is_error()) { + event.reply(callback.get_error().message); + return; + } - auto message = callback.get(); - event.reply(message); - }); + auto message = callback.get(); + event.reply(message); + }); } }); @@ -98,18 +86,14 @@ int main() { constexpr int64_t max_val{100}; msgs_get.add_option( - dpp::command_option(dpp::co_integer, "quantity", - "Quantity of messages to get. - Max 100") + dpp::command_option(dpp::co_integer, "quantity", "Quantity of messages to get. - Max 100") .set_min_value(min_val) .set_max_value(max_val)); - dpp::slashcommand channel_create("channel-create", - "Create a channel", bot.me.id); - dpp::slashcommand msg_error( - "msg-error", "Get an error instead of a message", bot.me.id); + dpp::slashcommand channel_create("channel-create", "Create a channel", bot.me.id); + dpp::slashcommand msg_error("msg-error", "Get an error instead of a message", bot.me.id); - bot.global_bulk_command_create( - {msgs_get, channel_create, msg_error}); + bot.global_bulk_command_create({msgs_get, channel_create, msg_error}); } }); diff --git a/src/01_basics/07_cache/src/main.cpp b/src/01_basics/07_cache/src/main.cpp index 8284188..3136091 100644 --- a/src/01_basics/07_cache/src/main.cpp +++ b/src/01_basics/07_cache/src/main.cpp @@ -14,8 +14,7 @@ int main() { dotenv::init(); // Make sure the DISCORD_TOKEN environment var exists if (!std::getenv("DISCORD_TOKEN")) { - std::cout << "Failed to get DISCORD_TOKEN from .env or the environment." - << std::endl; + std::cout << "Failed to get DISCORD_TOKEN from .env or the environment." << std::endl; exit(EXIT_FAILURE); } // Get the bot token from DISCORD_TOKEN @@ -25,31 +24,26 @@ int main() { dpp::cluster bot(botToken, dpp::i_default_intents | dpp::i_guild_members); // Create a lamba as a callback for when a reaction is removed - bot.on_message_reaction_remove( - [&bot](const dpp::message_reaction_remove_t& event) { - // Get the user from the cache. - // More then just users are cached, you can use the following - // functions to interact with it - // dpp::find_role() - // dpp::find_channel() - // dpp::find_guild() - // dpp::find_guild_member() - // dpp::find_user() - dpp::user* reacting_user = dpp::find_user(event.reacting_user_id); + bot.on_message_reaction_remove([&bot](const dpp::message_reaction_remove_t& event) { + // Get the user from the cache. + // More then just users are cached, you can use the following + // functions to interact with it + // dpp::find_role() + // dpp::find_channel() + // dpp::find_guild() + // dpp::find_guild_member() + // dpp::find_user() + dpp::user* reacting_user = dpp::find_user(event.reacting_user_id); - // Make sure the user was actually found in the cache - if (!reacting_user) { - bot.log(dpp::ll_info, - "User with id " + - std::to_string(event.reacting_user_id) + - " was not found."); - return; - } - // Log the action, using their username that we grabbed from the - // cache - bot.log(dpp::ll_info, reacting_user->format_username() + - " removed their reaction."); - }); + // Make sure the user was actually found in the cache + if (!reacting_user) { + bot.log(dpp::ll_info, "User with id " + std::to_string(event.reacting_user_id) + " was not found."); + return; + } + // Log the action, using their username that we grabbed from the + // cache + bot.log(dpp::ll_info, reacting_user->format_username() + " removed their reaction."); + }); // Start the bot bot.start(dpp::st_wait); diff --git a/src/01_basics/08_listening_to_messages/src/main.cpp b/src/01_basics/08_listening_to_messages/src/main.cpp index 30900e9..0ff520a 100644 --- a/src/01_basics/08_listening_to_messages/src/main.cpp +++ b/src/01_basics/08_listening_to_messages/src/main.cpp @@ -18,8 +18,7 @@ int main() { dotenv::init(); // Make sure the DISCORD_TOKEN env var exists if (!std::getenv("DISCORD_TOKEN")) { - std::cout << "Failed to get DISCORD_TOKEN from .env or environment" - << std::endl; + std::cout << "Failed to get DISCORD_TOKEN from .env or environment" << std::endl; exit(EXIT_FAILURE); } // Load in the bot token from the env @@ -32,8 +31,7 @@ int main() { bot.on_message_create([](const dpp::message_create_t& event) { // Filter through the message content if (event.msg.content.find("bad word") != std::string::npos) { - event.reply("That is not allowed here. Please, mind you language.", - true); + event.reply("That is not allowed here. Please, mind you language.", true); } }); diff --git a/src/02_interactions_and_components/01_slash_cmds/01_using_slash_cmds/src/main.cpp b/src/02_interactions_and_components/01_slash_cmds/01_using_slash_cmds/src/main.cpp index 7b1ccfb..c3f1dad 100644 --- a/src/02_interactions_and_components/01_slash_cmds/01_using_slash_cmds/src/main.cpp +++ b/src/02_interactions_and_components/01_slash_cmds/01_using_slash_cmds/src/main.cpp @@ -1,6 +1,91 @@ +#include +#include +#include +#include + +#include #include +#include "dotenv.h" + int main() { - std::cout << "WIP" << std::endl; + // Load the .env file into the environment + dotenv::init(); + // Make sure the DISCORD_TOKEN env var exists + if (!std::getenv("DISCORD_TOKEN") || !std::getenv("DISCORD_GUILD")) { + std::cout << "Failed to get DISCORD_TOKEN or DISCORD_GUILD from .env or environment" << std::endl; + return 0; + } + // Load in the bot token + std::string botToken = std::getenv("DISCORD_TOKEN"); + // Load in the guild id + std::string guildId = std::getenv("DISCORD_GUILD"); + + // Create the bot + dpp::cluster bot(botToken); + // Register a cout logger + bot.on_log(dpp::utility::cout_logger()); + + // Create a lambda callback for when a slashcommand is run + bot.on_slashcommand([](const dpp::slashcommand_t& event) { + // Filter through the command name and run its matching code + if (event.command.get_command_name() == "blep") { + // Get the "animal" option + std::string animal = std::get(event.get_parameter("animal")); + // Reply back + event.reply(std::string("Blep! You chose") + animal); + } else if (event.command.get_command_name() == "blep-guild") { + std::string animal = std::get(event.get_parameter("animal")); + event.reply(std::string("Blep! You chose") + animal); + } else if (event.command.get_command_name() == "ping") { + event.reply("Pong!"); + } else if (event.command.get_command_name() == "pong") { + event.reply("Ping!"); + } else if (event.command.get_command_name() == "ding") { + event.reply("Dong!"); + } else if (event.command.get_command_name() == "dong") { + event.reply("Ding!"); + } + }); + + // Create a lambda for when the bot is ready + bot.on_ready([&bot, &guildId](const dpp::ready_t& event) { + // Only run once + if (dpp::run_once()) { + // Create the "blep" global command + dpp::slashcommand globalCommand("blep", "Send a random adorable animal photo", bot.me.id); + // Add some options to the global command + globalCommand.add_option( + dpp::command_option(dpp::co_string, "animal", "The type of animal", true) + .add_choice(dpp::command_option_choice("Dog", std::string("animal_dog"))) + .add_choice(dpp::command_option_choice("Cat", std::string("animal_cat"))) + .add_choice(dpp::command_option_choice("Penguin", std::string("animal_penguin")))); + + // Register the global command + bot.global_command_create(globalCommand); + + // Create the guild only command + dpp::slashcommand guildCommand("blep-guild", "Send a random adorable animal photo", bot.me.id); + guildCommand.add_option( + dpp::command_option(dpp::co_string, "animal", "The type of animal", true) + .add_choice(dpp::command_option_choice("Dog", std::string("animal_dog"))) + .add_choice(dpp::command_option_choice("Cat", std::string("animal_cat"))) + .add_choice(dpp::command_option_choice("Penguin", std::string("animal_penguin")))); + // Register the guild command + bot.guild_command_create(guildCommand, guildId); + + // Create some commands + dpp::slashcommand pingCommand("ping", "Pong", bot.me.id); + dpp::slashcommand pongCommand("pong", "ping", bot.me.id); + dpp::slashcommand dingCommand("ding", "dong", bot.me.id); + dpp::slashcommand dongCommand("dong", "ding", bot.me.id); + // Bulk register the commands using a list + bot.global_bulk_command_create({pingCommand, pongCommand, dingCommand, dongCommand}); + } + }); + + // Start the bot + bot.start(dpp::st_wait); + return 0; }