01_06_callbacks
Build / Build (push) Failing after 17m2s

This commit is contained in:
2026-09-21 17:24:34 -04:00
parent b1dd8e8396
commit f46d7065f1
4 changed files with 135 additions and 2 deletions
+1
View File
@@ -23,3 +23,4 @@ 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/03_private_messages")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/04_adding_files") 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/05_webhooks")
add_subdirectory("${CMAKE_CURRENT_SOURCE_DIR}/src/01_basics/06_callbacks")
+15
View File
@@ -0,0 +1,15 @@
cmake_minimum_required(VERSION 3.28)
project(dppbots)
set(example_name "01_06_callbacks")
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})
+119
View File
@@ -0,0 +1,119 @@
#include <appcommand.h>
#include <channel.h>
#include <cluster.h>
#include <dispatcher.h>
#include <intents.h>
#include <message.h>
#include <misc-enum.h>
#include <once.h>
#include <restresults.h>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <string>
#include "dotenv.h"
int main() {
// Load the .env into the environment
dotenv::init();
if (!std::getenv("DISCORD_TOKEN")) {
std::cout
<< "Failed to get the discord token from the .env or enviroment"
<< std::endl;
exit(EXIT_FAILURE);
}
// Get the bot token
std::string botToken = std::getenv("DISCORD_TOKEN");
// Create a cluster with default intents as well as the message content
// dpp::i_message_content is needed in order to get messages
dpp::cluster bot(botToken, dpp::i_default_intents | dpp::i_message_content);
bot.on_log(dpp::utility::cout_logger());
bot.on_slashcommand([&bot](const dpp::slashcommand_t& event) {
if (event.command.get_command_name() == "msgs-get") {
int64_t limit = std::get<int64_t>(event.get_parameter("quantity"));
bot.messages_get(
event.command.channel_id, 0, 0, 0, limit,
[event](const dpp::confirmation_callback_t& callback) {
if (callback.is_error()) {
std::cout << callback.get_error().message << std::endl;
return;
}
auto messages = callback.get<dpp::message_map>();
std::string contents;
for (const auto& x : messages) {
contents += x.second.content + "\n";
}
event.reply(contents);
});
} else if (event.command.get_command_name() == "channel-create") {
dpp::channel channel = dpp::channel().set_name("test").set_guild_id(
event.command.guild_id);
bot.channel_create(
channel,
[&bot,
event](const dpp::confirmation_callback_t& callback) -> void {
if (callback.is_error()) {
bot.log(dpp::loglevel::ll_error,
callback.get_error().message);
return;
}
auto channel = callback.get<dpp::channel>();
dpp::message message =
dpp::message("The channel's name is '" + channel.name +
"', ID is '" + std::to_string(channel.id) +
"', and the type is '" +
std::to_string(channel.get_type()) + "'.");
event.reply(message);
});
} else if (event.command.get_command_name() == "msg-error") {
bot.message_get(
0, 0,
[event](const dpp::confirmation_callback_t& callback) -> void {
if (callback.is_error()) {
event.reply(callback.get_error().message);
return;
}
auto message = callback.get<dpp::message>();
event.reply(message);
});
}
});
bot.on_ready([&bot](const dpp::ready_t& event) {
if (dpp::run_once<struct register_global_commands>()) {
dpp::slashcommand msgs_get("msgs-get", "Get messages", bot.me.id);
constexpr int64_t min_val{1};
constexpr int64_t max_val{100};
msgs_get.add_option(
dpp::command_option(dpp::co_integer, "quantity",
"Quantity of messages to get. - Max 100")
.set_min_value(min_val)
.set_max_value(max_val));
dpp::slashcommand channel_create("channel-create",
"Create a channel", bot.me.id);
dpp::slashcommand msg_error(
"msg-error", "Get an error instead of a message", bot.me.id);
bot.global_bulk_command_create(
{msgs_get, channel_create, msg_error});
}
});
bot.start(dpp::st_wait);
return 0;
}
-2
View File
@@ -1,6 +1,4 @@
├── 01_basics ├── 01_basics
│   ├── 05_webhooks
│   ├── 06_callback_functions
│   ├── 07_cache │   ├── 07_cache
│   └── 08_listening_to_messages │   └── 08_listening_to_messages
├── 02_interactions_and_components ├── 02_interactions_and_components