-
Notifications
You must be signed in to change notification settings - Fork 1
/
DiscordInstance.cpp
158 lines (126 loc) · 5.12 KB
/
DiscordInstance.cpp
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* LevelAPI - Geometry Dash level cacher with search functionality and more.
Copyright (C) 2023 Sergei Baigerov
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "DiscordInstance.h"
#include "DCommand.h"
#include "appcommand.h"
#include "cluster.h"
#include "dispatcher.h"
#include "lapi_database.h"
#include "Translation.h"
#include "presence.h"
#include "iAndy.h"
#include "DCommandStats.h"
#include "DCommandSearch.h"
#include "restresults.h"
using namespace std::chrono_literals;
using namespace LevelAPI::Frontend;
DiscordInstance::DiscordInstance(void *db) {
this->m_pDB = db;
auto dbA = reinterpret_cast<LevelAPI::DatabaseController::Database *>(db);
m_pBot = new dpp::cluster(dbA->m_sBotToken, dpp::i_default_intents | dpp::i_message_content);
m_pBot->on_log(dpp::utility::cout_logger());
}
std::thread *DiscordInstance::start() {
std::thread *dst = new std::thread(DiscordInstance::dthread, this);
dst->detach();
return dst;
}
void DiscordInstance::setStatus(std::string status) {
dpp::presence_status st = dpp::ps_dnd;
auto pr = dpp::presence(st, dpp::activity_type::at_listening, status);
auto bot = m_pBot;
bot->set_presence(pr);
}
void DiscordInstance::dthread(DiscordInstance *instance) {
auto dbA = reinterpret_cast<LevelAPI::DatabaseController::Database *>(instance->m_pDB);
auto bot = instance->m_pBot;
std::vector<DCommand *> commandList = {};
bot->on_ready([&](const dpp::ready_t& event) {
std::cout << Translation::getByKey("lapi.bot.command.create") << std::endl;
commandList.push_back(new DCommandStats(instance->m_pBot->me.id));
commandList.push_back(new DCommandSearch(instance->m_pBot->me.id));
int i = 0;
while (i < commandList.size()) {
std::cout << Translation::getByKey("lapi.bot.command.register", commandList[i]->getCommandName()) << std::endl;
auto cmd = commandList[i]->getCommand();
bot->global_command_create(cmd);
i++;
}
dbA->m_bBotReady = true;
std::cout << Translation::getByKey("lapi.bot.ready") << std::endl;
});
instance->m_pBot->on_message_create([&](const dpp::message_create_t& event) {
if(!event.msg.author.is_bot() && !event.msg.is_dm()) {
if(event.msg.content == "lapi:registerchannel") {
if(dbA->m_sRegisteredCID.empty()) {
event.reply(Translation::getByKey("lapi.bot.command.registerchannel.success"));
uint64_t val = event.msg.channel_id;
dbA->m_sRegisteredCID = std::to_string(val);
dbA->save();
}
}
}
});
bot->on_slashcommand([&](const dpp::slashcommand_t & event) {
int i = 0;
while (i < commandList.size()) {
if (event.command.get_command_name() == commandList[i]->getCommandName()) {
std::cout << Translation::getByKey("lapi.bot.command.run", commandList[i]->getCommandName()) << std::endl;
commandList[i]->_cluster = bot;
commandList[i]->run(event);
}
i++;
}
});
bot->on_button_click([&](const dpp::button_click_t & event) {
int i = 0;
while (i < commandList.size()) {
auto event_list = commandList[i]->getRegistedEvents();
int j = 0;
while (j < event_list.size()) {
if (event.custom_id == event_list[j].first) {
commandList[i]->_cluster = bot;
commandList[i]->run(event);
}
j++;
}
i++;
}
});
bot->on_select_click([&](const dpp::select_click_t & event) {
int i = 0;
while (i < commandList.size()) {
auto event_list = commandList[i]->getRegistedEvents();
int j = 0;
while (j < event_list.size()) {
if (event.custom_id == event_list[j].first) {
commandList[i]->_cluster = bot;
commandList[i]->run(event);
}
j++;
}
i++;
}
});
instance->m_pBot->start(dpp::st_return);
while(true) {
int ll = dbA->getTotalLevels();
float scaleMult = 1.f;
std::string levels = iAndy::intToFormatString(ll, scaleMult);
std::string levelssofar = Translation::getByKey("lapi.bot.status.levels", levels);
instance->setStatus(levelssofar);
std::this_thread::sleep_for(std::chrono::seconds(30s));
}
}