@@ -0,0 +1,15 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
project(dppbots)
|
||||
set(example_name "01_05_webhooks")
|
||||
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})
|
||||
@@ -0,0 +1,41 @@
|
||||
#include <cluster.h>
|
||||
#include <message.h>
|
||||
|
||||
#include <chrono>
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
#include "dotenv.h"
|
||||
|
||||
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;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
// Get the webhook url from the environment
|
||||
std::string webhookUrl = std::getenv("DISCORD_WEBHOOK");
|
||||
|
||||
// When using webhooks, a bot token is not needed,
|
||||
// we can just create the cluster without it
|
||||
dpp::cluster bot;
|
||||
// Start the bot, returning once its initializaed
|
||||
bot.start(dpp::st_return);
|
||||
// Create a webhook object with our webhook url
|
||||
dpp::webhook wh(webhookUrl);
|
||||
|
||||
// Send a simple message over the webhook
|
||||
bot.execute_webhook(wh, dpp::message("This is a webhook message!"));
|
||||
|
||||
// Wait for all active requests to be completed
|
||||
while (bot.active_requests() > 0) {
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
// Exit, the bot will automatically be cleaned up
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user