aboutsummaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 6ce58587942cec869fe7e42a33ee5871c03c561c (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <dpp/dpp.h>
#include <dpp/json.h>
#include <142bot/bot.hpp>
#include <142bot/db.hpp>

#include <spdlog/spdlog.h>
#include <spdlog/async.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/sinks/rotating_file_sink.h>
#include <string>

#include <sentry.h>

#include "142bot_config.h"

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");
	sentry_options_t *options = sentry_options_new();
    sentry_options_set_dsn(options, cfg.value("sentry_dsn", "").c_str());
  	// This is also the default-path. For further information and recommendations:
  	// https://docs.sentry.io/platforms/native/configuration/options/#database-path
  	sentry_options_set_database_path(options, ".sentry-native");
  	sentry_options_set_release(options, "142bot@" + onefortytwobot_VERSION_MAJOR + '.' + onefortytwobot_VERSION_MINOR);
  	sentry_options_set_debug(options, 0);
	sentry_options_set_environment(options, onefortytwobot_env);
	sentry_options_set_symbolize_stacktraces(options, 1);
  	sentry_init(options);
    dpp::cluster bot(token, dpp::intents::i_all_intents);

		std::shared_ptr<spdlog::logger> log;
		spdlog::init_thread_pool(8192, 2);
		std::vector<spdlog::sink_ptr> sinks;
		auto stdout_sink = std::make_shared<spdlog::sinks::stdout_color_sink_mt >();
		auto rotating = std::make_shared<spdlog::sinks::rotating_file_sink_mt>("142bot.log", 1024 * 1024 * 5, 10);
		sinks.push_back(stdout_sink);
		sinks.push_back(rotating);
		log = std::make_shared<spdlog::async_logger>("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::trace);	

		/* 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 client(0, &bot, cfg.value("prefix", ".").at(0), cfg);

    client.set_owner_id(dpp::snowflake(cfg.value("owner", "00000000000")));

    		/* 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.on_message_reaction_add(std::bind(&Bot::onMessageReactionAdd, &client, std::placeholders::_1));
		bot.on_slashcommand(std::bind(&Bot::onSlashCommand, &client, std::placeholders::_1));
		bot.on_form_submit(std::bind(&Bot::onFormSubmit, &client, std::placeholders::_1));

    bot.start(dpp::st_wait);
	sentry_close();
    return 0;
}