01-07
This commit is contained in:
@@ -0,0 +1,15 @@
|
||||
cmake_minimum_required(VERSION 3.28)
|
||||
project(dppbots)
|
||||
set(example_name "01_07_cache")
|
||||
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,58 @@
|
||||
#include <cache.h>
|
||||
#include <cluster.h>
|
||||
#include <intents.h>
|
||||
#include <misc-enum.h>
|
||||
|
||||
#include <cstdlib>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
#include "dotenv.h"
|
||||
|
||||
int main() {
|
||||
// Load the .env into the environment
|
||||
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;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
// Get the bot token from DISCORD_TOKEN
|
||||
std::string botToken = std::getenv("DISCORD_TOKEN");
|
||||
|
||||
// Create the bot, passing in our token as well as the intents
|
||||
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);
|
||||
|
||||
// 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);
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user