01 - 04
Some checks failed
Build / Build (push) Has been cancelled

This commit is contained in:
2026-06-06 20:31:28 -04:00
parent e750ead7b2
commit 4451bc3ff6

View File

@@ -1,17 +1,113 @@
#include <appcommand.h>
#include <cluster.h>
#include <dispatcher.h>
#include <dotenv/dotenv.h> #include <dotenv/dotenv.h>
#include <dpp.h>
#include <once.h>
#include <queues.h>
#include <utility.h>
#include <cstdlib> #include <cstdlib>
#include <iostream> #include <iostream>
int main() { int main() {
// Load the .env file into the environment
dotenv::init(); dotenv::init();
// Make sure the DISCORD_TOKEN was loaded in
if (!std::getenv("DISCORD_TOKEN")) { if (!std::getenv("DISCORD_TOKEN")) {
std::cout std::cout
<< "Failed to load the discord token from .env or the environment" << "Failed to load the discord token from .env or the environment"
<< std::endl; << std::endl;
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
// Load in the token
std::string botToken = std::getenv("DISCORD_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; return 0;
} }