generated from DiscordPP/echo-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cc
303 lines (253 loc) · 10.3 KB
/
main.cc
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <fstream>
#include <iostream>
#include <random>
#include <regex>
#include <boost/asio.hpp>
#include <discordpp/bot.hh>
#include <discordpp/log.hh>
#include <discordpp/plugin-overload.hh>
#include <discordpp/plugin-ratelimit.hh>
#include <discordpp/plugin-responder.hh>
#include <discordpp/rest-beast.hh>
#include <discordpp/websocket-simpleweb.hh>
#define GUILD "164234463247597568"
#define QUIZCHANNEL "742540278484959322"
#define KEYROLE "742606708383154177"
#define LOCKROLE "761803600199680001"
#define GREETCHANNEL "164234463247597568"
#define LOGCHANNEL "475052413775249408"
namespace asio = boost::asio;
using json = nlohmann::json;
namespace dpp = discordpp;
using DppBot = dpp::PluginResponder<dpp::PluginOverload<
dpp::PluginRateLimit<dpp::WebsocketSimpleWeb<dpp::RestBeast<dpp::Bot>>>>>;
std::string getToken();
std::istream &safeGetline(std::istream &is, std::string &t);
void filter(std::string &target, const std::string &pattern);
void greet(std::shared_ptr<DppBot> bot, std::string &questionID, int &answer,
const json &user);
void printAndTell(std::shared_ptr<DppBot> bot, std::string message);
int main() {
dpp::log::filter = dpp::log::info;
dpp::log::out = &std::cerr;
std::cout
<< "Howdy, and thanks for trying out Discord++ Gatekeeper!\n"
<< "Feel free to drop into the official server at "
"https://discord.gg/0usP6xmT4sQ4kIDh if you have any questions.\n\n"
<< std::flush;
std::cout << "Starting bot...\n\n";
std::string token = getToken();
if (token.empty()) {
std::cerr
<< "CRITICAL: "
<< "There is no valid way for Gatekeeper to obtain a token! Use "
"one of the following ways:"
<< std::endl
<< "(1) Fill the BOT_TOKEN environment variable with the "
"token (e.g. 'Bot 123456abcdef')."
<< std::endl
<< "(2) Copy the example `token.eg.dat` as `token.dat` and "
"write your own token to it.\n";
exit(1);
}
// Create Bot object
auto bot = std::make_shared<DppBot>();
// Don't complain about unhandled events
bot->debugUnhandled = false;
// Don't show heartbeat status
bot->showHeartbeats = false;
bot->intents = discordpp::intents::GUILD_MEMBERS | discordpp::intents::GUILD_MESSAGES | discordpp::intents::GUILD_MESSAGE_REACTIONS;
json self;
bot->handlers.insert(
{"READY", [&self](json data) { self = data["user"]; }});
bot->prefix = "!";
bot->respond("help", "`help`: Print this message.\n"
"`join`: Re-send the join prompt");
std::string questionID;
int answer;
bot->handlers.insert(
{"GUILD_MEMBER_ADD", [&bot, &questionID, &answer](const json &msg) {
if (msg["guild_id"].get<std::string>() != GUILD)
return;
std::cout << msg["user"]["username"] << " joined.\n";
greet(bot, questionID, answer, msg["user"]);
/*bot->call(
"PUT",
"/guilds/" GUILD "/members/" +
msg["user"]["id"].get<std::string>() + "/roles/" ROLE,
[&bot, &questionID, &answer, msg](bool fail, const json &) {
greet(bot, questionID, answer, msg["user"]);
});*/
}});
bot->respond("join", [&bot, &questionID, &answer](const json &msg) {
if (msg["guild_id"].get<std::string>() != GUILD ||
msg["channel_id"].get<std::string>() != QUIZCHANNEL)
return;
printAndTell(bot, msg["author"]["username"].get<std::string>() +
" requested a quiz.");
greet(bot, questionID, answer, msg["author"]);
bot->call("DELETE", "/channels/" QUIZCHANNEL "/messages/" + msg["id"].get<std::string>());
});
bot->handlers.insert(
{"MESSAGE_REACTION_ADD",
[bot, &self, &questionID, &answer](const json &msg) {
if (msg["guild_id"].get<std::string>() != GUILD ||
msg["channel_id"].get<std::string>() != QUIZCHANNEL ||
msg["message_id"].get<std::string>() != questionID ||
msg["member"]["user"]["id"].get<std::string>() ==
self["id"].get<std::string>())
return;
bool isKeyed = false;
for (const auto &role : msg["member"]["roles"]) {
isKeyed = isKeyed || role.get<std::string>() == KEYROLE;
}
if (isKeyed)
return;
std::string strAnswer = "🇦";
strAnswer[3] += answer;
bool passed = msg["emoji"]["name"].get<std::string>() == strAnswer;
printAndTell(bot,
msg["member"]["user"]["username"].get<std::string>() +
" " + (passed ? "passed" : "failed") +
" the quiz.\n");
bot->call("DELETE",
"/channels/" QUIZCHANNEL "/messages/" + questionID);
bot->call("PUT",
"/guilds/" GUILD "/members/" +
msg["member"]["user"]["id"].get<std::string>() +
"/roles/" + (passed ? KEYROLE : LOCKROLE));
}});
// Create Asio context, this handles async stuff.
auto aioc = std::make_shared<asio::io_context>();
// Set the bot up
bot->initBot(6, token, aioc);
// Run the bot!
bot->run();
return 0;
}
void greet(std::shared_ptr<DppBot> bot, std::string &questionID, int &answer,
const json &user) {
if (!questionID.empty()) {
bot->call("DELETE", "/channels/" QUIZCHANNEL "/messages/" + questionID);
}
std::ostringstream content;
content << "Howdy, <@" << user["id"].get<std::string>()
<< ">! To join the server, please answer the following question "
"by reacting:\n**What is Discord++?**\n";
static const std::string correct = "A C++ library";
static const std::vector<std::string> incorrect{
"A hacked app", "A way to get free Nitro",
"A hacking/griefing toolset"};
static auto rng = std::mt19937(std::random_device()());
static std::uniform_int_distribution<int> distribution(0, incorrect.size());
std::vector<std::string> choices(incorrect);
std::shuffle(choices.begin(), choices.end(), rng);
answer = distribution(rng);
choices.insert(choices.begin() + answer, correct);
for (int i = 0; i < choices.size(); i++) {
content << ":regional_indicator_" << (char)('a' + i) << ": "
<< choices[i] << '\n';
}
bot->call(
"POST", "/channels/" QUIZCHANNEL "/messages",
{{"content", content.str()}},
[bot, &questionID](bool fail, const json &res) {
if (fail)
return;
questionID = res["body"]["id"].get<std::string>();
bot->call(
"PUT",
"/channels/" QUIZCHANNEL "/messages/" + questionID +
"/reactions/%F0%9F%87%A6/@me",
[bot, &questionID](bool fail, const json &) {
if (fail)
return;
bot->call(
"PUT",
"/channels/" QUIZCHANNEL "/messages/" + questionID +
"/reactions/%F0%9F%87%A7/@me",
[bot, &questionID](bool fail, const json &) {
if (fail)
return;
bot->call(
"PUT",
"/channels/" QUIZCHANNEL "/messages/" +
questionID + "/reactions/%F0%9F%87%A8/@me",
[bot, &questionID](bool fail, const json &) {
if (fail)
return;
bot->call(
"PUT",
"/channels/" QUIZCHANNEL "/"
"messages/" +
questionID +
"/reactions/%F0%9F%87%A9/@me");
});
});
});
});
}
std::string getToken() {
std::string token;
/*
First attempt to read the token from the BOT_TOKEN
environment variable.
*/
char const *env = std::getenv("BOT_TOKEN");
if (env != nullptr) {
token = std::string(env);
} else {
/*/
* Read token from token file.
* Tokens are required to communicate with Discord, and hardcoding
tokens is a bad idea.
* If your bot is open source, make sure it's ignore by git in your
.gitignore file.
/*/
std::ifstream tokenFile("token.dat");
if (!tokenFile) {
return "";
}
safeGetline(tokenFile, token);
tokenFile.close();
}
return token;
}
/*/
* Source: https://stackoverflow.com/a/6089413/1526048
/*/
std::istream &safeGetline(std::istream &is, std::string &t) {
t.clear();
// The characters in the stream are read one-by-one using a std::streambuf.
// That is faster than reading them one-by-one using the std::istream.
// Code that uses streambuf this way must be guarded by a sentry object.
// The sentry object performs various tasks,
// such as thread synchronization and updating the stream state.
std::istream::sentry se(is, true);
std::streambuf *sb = is.rdbuf();
for (;;) {
int c = sb->sbumpc();
switch (c) {
case '\n':
return is;
case '\r':
if (sb->sgetc() == '\n') {
sb->sbumpc();
}
return is;
case std::streambuf::traits_type::eof():
// Also handle the case when the last line has no line ending
if (t.empty()) {
is.setstate(std::ios::eofbit);
}
return is;
default:
t += (char)c;
}
}
}
void printAndTell(std::shared_ptr<DppBot> bot, std::string message) {
std::cout << message << '\n';
bot->call("POST", "/channels/" LOGCHANNEL "/messages",
{{"content", message}});
}