@@ -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")
|
||||||
|
|||||||
@@ -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})
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user