@@ -34,3 +34,4 @@ add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/04_adding_files")
|
||||
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/05_webhooks")
|
||||
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/06_callbacks")
|
||||
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/07_cache")
|
||||
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/08_listening_to_messages")
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
project(dppbots)
|
||||
set(example_name "01_08_listening_to_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})
|
||||
@@ -0,0 +1,44 @@
|
||||
#include <cluster.h>
|
||||
#include <dispatcher.h>
|
||||
#include <intents.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
|
||||
#include "dotenv.h"
|
||||
|
||||
// Please note:
|
||||
// Discord made message content a privileged intent in August 2022.
|
||||
// Directly pulling the message content and using prefixes for commands is
|
||||
// generally an older technique for running bot commands. Nowadays it is
|
||||
// recommanded to use slash commands, which is covered in 02_01.
|
||||
|
||||
int main() {
|
||||
// Load the .env into the environment
|
||||
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;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Load in the bot token from the env
|
||||
std::string botToken = std::getenv("DISCORD_TOKEN");
|
||||
|
||||
// Create the bot
|
||||
dpp::cluster bot(botToken, dpp::i_default_intents | dpp::i_message_content);
|
||||
|
||||
// Create a lambda for the on_message_create event.
|
||||
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);
|
||||
}
|
||||
});
|
||||
|
||||
// Start the bot
|
||||
bot.start(dpp::st_wait);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user