This repository was archived by the owner on Sep 13, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cc
More file actions
325 lines (275 loc) · 11.3 KB
/
main.cc
File metadata and controls
325 lines (275 loc) · 11.3 KB
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#include "include.hh"
namespace asio = boost::asio;
using json = nlohmann::json;
namespace dpp = discordpp;
using s_clock = std::chrono::system_clock;
using namespace std::literals::chrono_literals;
const auto tz = -6h;
std::string getToken();
std::istream &safeGetline(std::istream &is, std::string &t);
void filter(std::string &target, const std::string &pattern);
int main() {
dpp::log::filter = dpp::log::info;
dpp::log::out = &std::cerr;
std::cout << "Howdy, and thanks for trying out Discord++!\n"
<< "Feel free to drop into the official server at "
"https://discord.gg/VHAyrvspCx 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 Echo 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 = newBot();
// Create Asio context, this handles async stuff.
auto aioc = std::make_shared<asio::io_context>();
// A timer for recurring reminders
std::unique_ptr<boost::asio::system_timer> reminder;
// Don't complain about unhandled events
bot->debugUnhandled = false;
// Declare the intent to receive guild messages
// You don't need `NONE` it's just to show you how to declare multiple
bot->intents =
dpp::intents::GUILD_MESSAGES | dpp::intents::GUILD_MESSAGE_REACTIONS;
/*/
* Create handler for the READY payload, this may be handled by the bot in
the future.
* The `self` object contains all information about the 'bot' user.
/*/
json self;
bot->handlers.insert(
{"READY", [&self](json data) { self = data["user"]; }});
bot->prefix = "!";
const std::string helpSchedule =
"`" + bot->prefix +
"schedule TIME INTERVAL [USERS...]` Set up recurring puppy reminders\n"
"\t`TIME` is formatted `HH:MM`\n"
"\t`INTERVAL` is formatted `H:MM`\n"
"\t`USERS...` is any number of users to mention including zero\n";
std::ostringstream help;
help << "Puppybot is a bot to help you take care of your new puppy!\n"
<< "`" << bot->prefix << "help` Print this message\n"
<< helpSchedule;
bot->respond("help", help.str());
std::function<void(
const boost::system::error_code ec, const s_clock::time_point triggered,
const std::chrono::minutes period, const std::string &channel_id,
const std::string &targets)>
reminder_f;
reminder_f = [aioc, bot, &reminder,
&reminder_f](const boost::system::error_code ec,
const s_clock::time_point triggered,
const std::chrono::minutes period,
const std::string &channel_id,
const std::string &targets) {
if (ec.failed()) {
return;
}
bot->call("POST", "/channels/" + channel_id + "/messages",
json({{"content", ":bone: **IT'S PUPPY TIME!** " + targets}}));
reminder = std::make_unique<boost::asio::system_timer>(
*aioc, s_clock::now() + period); //+ std::chrono::minutes(1)
reminder->async_wait([&reminder_f, triggered, period, channel_id,
targets](const boost::system::error_code ec) {
reminder_f(ec, triggered + period, period, channel_id, targets);
});
};
bot->respond("schedule", [bot, &helpSchedule, aioc, &reminder,
&reminder_f](const json &msg) {
auto argc = msg["content"].get<std::string>();
std::vector<std::string> argv;
for (size_t pos; (pos = argc.find(' ')) != std::string::npos;) {
std::string arg = argc.substr(0, pos);
argv.push_back(arg);
argc.erase(0, pos + 1);
}
argv.push_back(argc);
for (const auto &arg : argv) {
std::cout << arg << std::endl;
}
try {
if (argv.size() < 3) {
throw std::logic_error("Wrong arg count");
}
if (argv[1].at(1) == ':') {
argv[1] = '0' + argv[1];
}
std::ostringstream out;
s_clock::time_point now = s_clock::now();
s_clock::time_point today =
std::chrono::floor<std::chrono::days>(now + tz) - tz;
// auto todaymin =
// std::chrono::time_point_cast<std::chrono::minutes>(std::chrono::floor<std::chrono::days>(now));
std::time_t now_tt = s_clock::to_time_t(now);
std::time_t today_tt = s_clock::to_time_t(today);
std::tm now_local = *std::localtime(&now_tt);
std::tm now_gm = *std::gmtime(&now_tt);
std::tm today_local = *std::localtime(&today_tt);
std::tm today_gm = *std::gmtime(&today_tt);
/*out << "`Now, Local: ` " << std::put_time(&now_local, "%F %T")
<< '\n';
out << "`Now, GMTime: ` " << std::put_time(&now_gm, "%F %T")
<< '\n';
out << "`Today, Local:` " << std::put_time(&today_local, "%F %T")
<< '\n';
out << "`Today, GMTime:` " << std::put_time(&today_gm, "%F %T")
<< '\n';*/
// s_clock::time_point start =
// s_clock::from_time_t(std::mktime(&start_tm));
s_clock::time_point start;
{
std::stringstream ss;
ss.exceptions(std::ios::failbit | std::ios::badbit);
ss << std::put_time(&today_local, "%F ") << argv[1];
ss >> date::parse("%Y-%m-%d %H:%M", start);
}
start -= tz;
std::cout << date::format("%c", start + tz) << std::endl;
std::cout << date::format("%c", now + tz) << std::endl;
if (start < now) {
start += 24h;
std::cout << date::format("%c", start + tz) << std::endl;
std::cout << date::format("%c", now + tz) << std::endl;
if (start < now) {
throw std::logic_error("Invalid start time");
}
}
out << ":dog: Puppy alarms will start at "
<< date::format("%I:%M %p on %A", start + tz) << '\n';
std::chrono::minutes period;
{
size_t sep = argv[2].find(':');
period = std::chrono::hours(std::stoi(argv[2].substr(0, sep))) +
std::chrono::minutes(std::stoi(
argv[2].substr(sep + 1, argv[2].size())));
}
if (period <= std::chrono::minutes::zero()) {
throw std::logic_error("Negative or zero period");
}
{
out << "They'll recur every ";
auto hours =
std::chrono::floor<std::chrono::hours>(period).count();
auto minutes = std::chrono::minutes(period).count() % 60;
if (hours > 0) {
out << hours << " hour" << (hours == 1 ? "" : "s");
}
if (hours > 0 && minutes > 0) {
out << " and ";
}
if (minutes > 0) {
out << minutes << " minute" << (minutes == 1 ? "" : "s");
}
out << "\n";
}
bot->call("POST",
"/channels/" + msg["channel_id"].get<std::string>() +
"/messages",
json({{"content", out.str()}}));
{
std::string channel_id = msg["channel_id"],
guild_id = msg["guild_id"], targets;
if (argv.size() > 3) {
bool first = true;
std::for_each(argv.begin() + 3, argv.end(),
[&targets, &first](std::string target) {
if (first) {
first = false;
} else {
targets += " ";
}
targets += target;
});
} else {
targets = "@everyone";
}
reminder =
std::make_unique<boost::asio::system_timer>(*aioc, start);
reminder->async_wait(
[&reminder_f, start, period, channel_id, guild_id,
targets](const boost::system::error_code ec) {
reminder_f(ec, start, period, channel_id, targets);
});
}
} catch (...) {
bot->call("POST",
"/channels/" + msg["channel_id"].get<std::string>() +
"/messages",
json({{"content", helpSchedule}}));
}
});
// Set the bot up
bot->initBot(6, token, aioc);
// Run the bot!
bot->run();
return 0;
}
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;
}
}
}