Compare commits
7 Commits
2acba97173
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 5da6d610b1 | |||
| 4451bc3ff6 | |||
| e750ead7b2 | |||
| 97df4330f3 | |||
| ab41d81b2d | |||
| 01ee26b0f7 | |||
| b2bab16422 |
@@ -17,4 +17,8 @@ foreach(cmake_file ${CMAKE_FILES})
|
|||||||
endforeach()
|
endforeach()
|
||||||
|
|
||||||
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/00_project")
|
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/00_project")
|
||||||
|
|
||||||
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/01_first_bot")
|
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/01_first_bot")
|
||||||
|
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/02_embeds")
|
||||||
|
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/03_private_messages")
|
||||||
|
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/04_adding_files")
|
||||||
|
|||||||
@@ -6,7 +6,7 @@ This repo is a collection of examples for dpp, a cpp discord library.
|
|||||||
|
|
||||||
The majority of the examples have come from [these docs](https://dpp.dev/example-programs.html).
|
The majority of the examples have come from [these docs](https://dpp.dev/example-programs.html).
|
||||||
This repo more or less exists for me to learn how the library works through following the docs as well as to keep all the examples in one place for others to find and learn from.
|
This repo more or less exists for me to learn how the library works through following the docs as well as to keep all the examples in one place for others to find and learn from.
|
||||||
With that all in mind, I've gone through and add comments *after* going through the doc page into the example itself.
|
With that all in mind, I've gone through and added comments *after* going through the doc page into the example itself.
|
||||||
This is mostly for me to reiterate over the code to better understand it and put it into my own words, but should also serve as a fairly quick reference to how and what is going on for those wanting to learn without a bunch of reading.
|
This is mostly for me to reiterate over the code to better understand it and put it into my own words, but should also serve as a fairly quick reference to how and what is going on for those wanting to learn without a bunch of reading.
|
||||||
|
|
||||||
The docs themselves contain a lot lot more information then what I have put into comments, so if you're stuck or want to know more, make sure you check them out.
|
The docs themselves contain a lot lot more information then what I have put into comments, so if you're stuck or want to know more, make sure you check them out.
|
||||||
@@ -97,9 +97,7 @@ A "build" and a "bin" folder.
|
|||||||
*Note: the bin folder will only exist on unix based systems (Linux, MacOS, etc)*
|
*Note: the bin folder will only exist on unix based systems (Linux, MacOS, etc)*
|
||||||
The "build" folder will contain all of your build intermediaries.
|
The "build" folder will contain all of your build intermediaries.
|
||||||
"Bin" will contain your actual executables.
|
"Bin" will contain your actual executables.
|
||||||
|
|
||||||
```
|
|
||||||
```
|
|
||||||
```
|
```
|
||||||
root
|
root
|
||||||
├── build
|
├── build
|
||||||
@@ -108,9 +106,6 @@ root
|
|||||||
│ ├── Release
|
│ ├── Release
|
||||||
│ └── Relwithdeb
|
│ └── Relwithdeb
|
||||||
```
|
```
|
||||||
```
|
|
||||||
```
|
|
||||||
|
|
||||||
|
|
||||||
## Contributing
|
## Contributing
|
||||||
|
|
||||||
|
|||||||
@@ -7,8 +7,16 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
// Load in the .env file into the environment
|
// Load the .env file into the environment
|
||||||
dotenv::init();
|
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;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
// Get the bot token
|
||||||
std::string botToken = std::getenv("DISCORD_TOKEN");
|
std::string botToken = std::getenv("DISCORD_TOKEN");
|
||||||
std::cout << "Using discord bot token: " << botToken << std::endl;
|
std::cout << "Using discord bot token: " << botToken << std::endl;
|
||||||
|
|
||||||
|
|||||||
15
src/01_basics/02_embeds/CMakeLists.txt
Normal file
15
src/01_basics/02_embeds/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.28)
|
||||||
|
project(dppbots)
|
||||||
|
set(example_name "01_02_embeds")
|
||||||
|
message("Setting up ${example_name}")
|
||||||
|
add_executable(${example_name} "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")
|
||||||
|
target_include_directories(${example_name} PUBLIC
|
||||||
|
"${dpp_SOURCE_DIR}/include"
|
||||||
|
"${dotenv-cpp_SOURCE_DIR}/include/laserpants"
|
||||||
|
|
||||||
|
)
|
||||||
|
target_link_libraries(${example_name}
|
||||||
|
dpp
|
||||||
|
dotenv
|
||||||
|
)
|
||||||
|
SET_OUTPUT_NAMES(${example_name})
|
||||||
89
src/01_basics/02_embeds/src/main.cpp
Normal file
89
src/01_basics/02_embeds/src/main.cpp
Normal file
@@ -0,0 +1,89 @@
|
|||||||
|
#include <cluster.h>
|
||||||
|
#include <colors.h>
|
||||||
|
#include <dispatcher.h>
|
||||||
|
#include <dotenv/dotenv.h>
|
||||||
|
#include <dpp/dpp.h>
|
||||||
|
#include <message.h>
|
||||||
|
#include <once.h>
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Load the .env file into the environment
|
||||||
|
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;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
// Load in the token
|
||||||
|
std::string botToken = std::getenv("DISCORD_TOKEN");
|
||||||
|
|
||||||
|
// Create a bot cluster
|
||||||
|
dpp::cluster bot(botToken);
|
||||||
|
|
||||||
|
// Set the bot logging callback to a default cout logger
|
||||||
|
bot.on_log(dpp::utility::cout_logger());
|
||||||
|
|
||||||
|
// Set the slashcommand callback
|
||||||
|
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
|
||||||
|
// Filter the slashcommand by name
|
||||||
|
if (event.command.get_command_name() == "embed") {
|
||||||
|
// Create the embed object
|
||||||
|
dpp::embed embed =
|
||||||
|
dpp::embed()
|
||||||
|
// Set the color of the embed
|
||||||
|
// This sets the color of the line
|
||||||
|
// on the left side
|
||||||
|
.set_color(dpp::colors::sti_blue)
|
||||||
|
// Set the title
|
||||||
|
.set_title("Title")
|
||||||
|
// 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_description("Description")
|
||||||
|
// Set the thumbnail, this is a small image off the right
|
||||||
|
.set_thumbnail("https://dpp.dev/DPP-Logo.png")
|
||||||
|
// Add some regular and inline fields
|
||||||
|
.add_field("Regular field title", "Value")
|
||||||
|
.add_field("Inline field title", "Value", true)
|
||||||
|
.add_field("Inline field title", "Value", true)
|
||||||
|
// 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"))
|
||||||
|
// Add a timestamp,
|
||||||
|
// time(<arg>) returns a timer of <arg> seconds
|
||||||
|
// using a 0 for the arg just returns the current time
|
||||||
|
.set_timestamp(time(0));
|
||||||
|
|
||||||
|
// Create a message using the embed
|
||||||
|
dpp::message msg(event.command.channel_id, embed);
|
||||||
|
// Send the message as a reply
|
||||||
|
event.reply(msg);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Set the bot on_ready to create a the global commands
|
||||||
|
bot.on_ready([&bot](const dpp::ready_t& event) {
|
||||||
|
// Only run the register_bot_commands once
|
||||||
|
// (If the bot is disconnected from the discord gateway,
|
||||||
|
// on_ready() is called once reconnected.
|
||||||
|
// We dont want to re-register when this happens)
|
||||||
|
if (dpp::run_once<struct register_bot_commands>()) {
|
||||||
|
bot.global_command_create(
|
||||||
|
dpp::slashcommand("embed", "Send an embed", bot.me.id));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Start the bot
|
||||||
|
bot.start(dpp::st_wait);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
15
src/01_basics/03_private_messages/CMakeLists.txt
Normal file
15
src/01_basics/03_private_messages/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.28)
|
||||||
|
project(dppbots)
|
||||||
|
set(example_name "01_03_private_messages")
|
||||||
|
message("Setting up ${example_name}")
|
||||||
|
add_executable(${example_name} "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")
|
||||||
|
target_include_directories(${example_name} PUBLIC
|
||||||
|
"${dpp_SOURCE_DIR}/include"
|
||||||
|
"${dotenv-cpp_SOURCE_DIR}/include/laserpants"
|
||||||
|
|
||||||
|
)
|
||||||
|
target_link_libraries(${example_name}
|
||||||
|
dpp
|
||||||
|
dotenv
|
||||||
|
)
|
||||||
|
SET_OUTPUT_NAMES(${example_name})
|
||||||
111
src/01_basics/03_private_messages/src/main.cpp
Normal file
111
src/01_basics/03_private_messages/src/main.cpp
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
#include <appcommand.h>
|
||||||
|
#include <cluster.h>
|
||||||
|
#include <dispatcher.h>
|
||||||
|
#include <dotenv/dotenv.h>
|
||||||
|
#include <dpp/dpp.h>
|
||||||
|
#include <message.h>
|
||||||
|
#include <once.h>
|
||||||
|
#include <restresults.h>
|
||||||
|
#include <snowflake.h>
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Load in the .env file
|
||||||
|
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;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
// Load in the token into a string
|
||||||
|
std::string botToken = std::getenv("DISCORD_TOKEN");
|
||||||
|
|
||||||
|
// Create the bot
|
||||||
|
dpp::cluster bot(botToken);
|
||||||
|
|
||||||
|
// Setup a default cout_logger
|
||||||
|
bot.on_log(dpp::utility::cout_logger());
|
||||||
|
|
||||||
|
// Setup a lamda for the on_slashcommand
|
||||||
|
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
|
||||||
|
// Filter for the /pm slashcommand
|
||||||
|
if (event.command.get_command_name() == "pm") {
|
||||||
|
// Discord stores all objects through a reference called a
|
||||||
|
// "snowflake"
|
||||||
|
dpp::snowflake user;
|
||||||
|
|
||||||
|
if (event.get_parameter("user").index() == 0) {
|
||||||
|
// If the user parameter wasn't passed, just get the user that
|
||||||
|
// issued the command
|
||||||
|
user = event.command.get_issuing_user().id;
|
||||||
|
} else {
|
||||||
|
// Get the value of the user parameter
|
||||||
|
user = std::get<dpp::snowflake>(event.get_parameter("user"));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create a direct message
|
||||||
|
bot.direct_message_create(
|
||||||
|
// Define the user you're sending the message to, aswell as the
|
||||||
|
// message
|
||||||
|
user, dpp::message("Here's a private message!"),
|
||||||
|
// Once the request has been sent to discord, a callback can be
|
||||||
|
// specified to handle any errors or anything related to the
|
||||||
|
// message
|
||||||
|
[event, user](const dpp::confirmation_callback_t& callback) {
|
||||||
|
// Check to make sure there wasnt an error sending the
|
||||||
|
// message
|
||||||
|
if (callback.is_error()) {
|
||||||
|
// 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));
|
||||||
|
} 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));
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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));
|
||||||
|
} else {
|
||||||
|
event.reply(
|
||||||
|
dpp::message(
|
||||||
|
"I've sent a private message to that user.")
|
||||||
|
.set_flags(dpp::m_ephemeral));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Define a callback for the on_ready
|
||||||
|
bot.on_ready([&bot](const dpp::ready_t& event) {
|
||||||
|
if (dpp::run_once<struct register_bot_commands>()) {
|
||||||
|
// Create the /pm slashcommand
|
||||||
|
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));
|
||||||
|
// Register the command with discord
|
||||||
|
bot.global_command_create(command);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Start the bot
|
||||||
|
bot.start(dpp::st_wait);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
15
src/01_basics/04_adding_files/CMakeLists.txt
Normal file
15
src/01_basics/04_adding_files/CMakeLists.txt
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
cmake_minimum_required(VERSION 3.28)
|
||||||
|
project(dppbots)
|
||||||
|
set(example_name "01_04_adding_files")
|
||||||
|
message("Setting up ${example_name}")
|
||||||
|
add_executable(${example_name} "${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp")
|
||||||
|
target_include_directories(${example_name} PUBLIC
|
||||||
|
"${dpp_SOURCE_DIR}/include"
|
||||||
|
"${dotenv-cpp_SOURCE_DIR}/include/laserpants"
|
||||||
|
|
||||||
|
)
|
||||||
|
target_link_libraries(${example_name}
|
||||||
|
dpp
|
||||||
|
dotenv
|
||||||
|
)
|
||||||
|
SET_OUTPUT_NAMES(${example_name})
|
||||||
113
src/01_basics/04_adding_files/src/main.cpp
Normal file
113
src/01_basics/04_adding_files/src/main.cpp
Normal file
@@ -0,0 +1,113 @@
|
|||||||
|
#include <appcommand.h>
|
||||||
|
#include <cluster.h>
|
||||||
|
#include <dispatcher.h>
|
||||||
|
#include <dotenv/dotenv.h>
|
||||||
|
#include <dpp.h>
|
||||||
|
#include <once.h>
|
||||||
|
#include <queues.h>
|
||||||
|
#include <utility.h>
|
||||||
|
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main() {
|
||||||
|
// Load the .env file into the environment
|
||||||
|
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;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
// Load in the token
|
||||||
|
std::string botToken = std::getenv("DISCORD_TOKEN");
|
||||||
|
|
||||||
|
// Create the bot
|
||||||
|
dpp::cluster bot(botToken);
|
||||||
|
|
||||||
|
// Setup on_log to use a default cout_logger
|
||||||
|
bot.on_log(dpp::utility::cout_logger());
|
||||||
|
|
||||||
|
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!");
|
||||||
|
// Add the "README.md" file to the message
|
||||||
|
msg.add_file("README.md", dpp::utility::read_file("README.md"));
|
||||||
|
// Send the message
|
||||||
|
event.reply(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
// Create the message
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
// Send the message
|
||||||
|
event.reply(msg);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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) {
|
||||||
|
// 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);
|
||||||
|
// Create the embed
|
||||||
|
dpp::embed embed;
|
||||||
|
// Set the image for the embed as the image
|
||||||
|
// added above
|
||||||
|
embed.set_image("attachment://logo.png");
|
||||||
|
// Add the embed to the message
|
||||||
|
msg.add_embed(embed);
|
||||||
|
}
|
||||||
|
// Send the message
|
||||||
|
event.reply(msg);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
bot.on_ready([&bot](const dpp::ready_t& event) {
|
||||||
|
if (dpp::run_once<struct register_bot_commands>()) {
|
||||||
|
// 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);
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
bot.global_command_create(embedCommand);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Start the bot
|
||||||
|
bot.start(dpp::st_wait);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user