From 527b7ab2df126bb2f480999049ed05b057a6ef83 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Thu, 30 Mar 2023 12:52:19 -0400 Subject: modules! --- src/main.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 49 insertions(+), 13 deletions(-) (limited to 'src/main.cpp') diff --git a/src/main.cpp b/src/main.cpp index 6cdbd31..d58f474 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,31 +1,67 @@ #include -#include "include/json.hpp" +#include +#include <142bot/bot.hpp> + +#include +#include +#include +#include using namespace std; using json = nlohmann::json; int main(int argc, char const *argv[]) { + std::ifstream f("config.json"); json cfg = json::parse(f); string token = cfg.value("token", "bad-token"); dpp::cluster bot(token, dpp::intents::i_all_intents); - bot.on_log(dpp::utility::cout_logger()); + std::shared_ptr log; + spdlog::init_thread_pool(8192, 2); + std::vector sinks; + auto stdout_sink = std::make_shared(); + auto rotating = std::make_shared("142bot.log", 1024 * 1024 * 5, 10); + sinks.push_back(stdout_sink); + sinks.push_back(rotating); + log = std::make_shared("logs", sinks.begin(), sinks.end(), spdlog::thread_pool(), spdlog::async_overflow_policy::block); + spdlog::register_logger(log); + log->set_pattern("%^%Y-%m-%d %H:%M:%S.%e [%L] [th#%t]%$ : %v"); + log->set_level(spdlog::level::level_enum::debug); + + /* Integrate spdlog logger to D++ log events */ + bot.on_log([&bot, &log](const dpp::log_t & event) { + switch (event.severity) { + case dpp::ll_trace: + log->trace("{}", event.message); + break; + case dpp::ll_debug: + log->debug("{}", event.message); + break; + case dpp::ll_info: + log->info("{}", event.message); + break; + case dpp::ll_warning: + log->warn("{}", event.message); + break; + case dpp::ll_error: + log->error("{}", event.message); + break; + case dpp::ll_critical: + default: + log->critical("{}", event.message); + break; + } + }); /* code */ - bot.on_slashcommand([](const dpp::slashcommand_t &event) { - if (event.command.get_command_name() == "ping") { - event.reply("Pong!"); - } - }); - - bot.on_ready([&bot](const dpp::ready_t &event) { - if (dpp::run_once()) { - bot.global_command_create(dpp::slashcommand("ping", "Ping!", bot.me.id)); - } - }); + Bot client(0, &bot); + + /* Attach events to the Bot class methods */ + bot.on_message_create(std::bind(&Bot::onMessage, &client, std::placeholders::_1)); + bot.on_ready(std::bind(&Bot::onReady, &client, std::placeholders::_1)); bot.start(dpp::st_wait); return 0; -- cgit v1.2.3