Repository navigation
Expand file tree
/
Copy pathDiscordWebhook.cpp
More file actions
306 lines (255 loc) · 9.04 KB
/
Copy pathDiscordWebhook.cpp
File metadata and controls
306 lines (255 loc) · 9.04 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
/* Discord Webhook
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
*/
#include <deque>
#include <QString>
#include <QFile>
#include <QHttpMultiPart>
#include <QEventLoop>
#include <QNetworkAccessManager>
#include "Common/Cpp/PrettyPrint.h"
//#include "Common/Cpp/PanicDump.h"
//#include "Common/Cpp/Json/JsonValue.h"
#include "Common/Cpp/Json/JsonArray.h"
#include "Common/Cpp/Json/JsonObject.h"
#include "Common/Qt/StringToolsQt.h"
#include "CommonFramework/GlobalSettingsPanel.h"
#include "CommonFramework/Logging/Logger.h"
#include "CommonFramework/Notifications/EventNotificationOption.h"
#include "DiscordSettingsOption.h"
#include "DiscordWebhook.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
namespace Integration{
namespace DiscordWebhook{
DiscordWebhookSender::DiscordWebhookSender()
: m_logger(global_logger_raw(), "DiscordWebhookSender")
, m_stopping(false)
, m_dispatcher(nullptr, 1)
, m_queue(m_dispatcher)
{}
DiscordWebhookSender::~DiscordWebhookSender(){
{
std::lock_guard<std::mutex> lg(m_lock);
m_stopping = true;
m_cv.notify_all();
}
}
DiscordWebhookSender& DiscordWebhookSender::instance(){
static DiscordWebhookSender sender;
return sender;
}
void DiscordWebhookSender::send_json(
Logger& logger,
const QUrl& url, std::chrono::milliseconds delay,
const JsonObject& obj,
std::shared_ptr<PendingFileSend> file
){
cleanup_stuck_requests();
m_queue.add_event(
delay,
[this, url, data = QByteArray::fromStdString(obj.dump()), file = std::move(file)]{
throttle();
if (!file && !data.isEmpty()){
internal_send_json(url, data);
}else if (file && !data.isEmpty()){
internal_send_image_embed(url, data, file->filepath(), file->filename());
}else{
internal_send_file(url, file->filepath());
}
}
);
logger.log("Scheduling Webhook Message... (queue = " + tostr_u_commas(m_queue.size()) + ")", COLOR_PURPLE);
}
void DiscordWebhookSender::send_file(
Logger& logger,
const QUrl& url, std::chrono::milliseconds delay,
std::shared_ptr<PendingFileSend> file
){
cleanup_stuck_requests();
// m_queue.emplace_back(url, file);
m_queue.add_event(
delay,
[this, url, file = std::move(file)]{
throttle();
internal_send_file(url, file->filepath());
}
);
logger.log("Scheduling Webhook Message... (queue = " + tostr_u_commas(m_queue.size()) + ")", COLOR_PURPLE);
}
void DiscordWebhookSender::cleanup_stuck_requests(){
std::lock_guard<std::mutex> lg(m_lock);
WallClock next = m_queue.next_event();
if (next == WallClock::max()){
return;
}
WallClock now = current_time();
WallClock threshold = now - std::chrono::seconds(60);
if (next < threshold){
m_logger.log("Purging request that appears to be stuck.", COLOR_RED);
emit stop_event_loop();
}
}
void DiscordWebhookSender::throttle(){
// Throttle the messages.
auto duration = THROTTLE_DURATION;
auto now = current_time();
while (!m_sent.empty() && m_sent[0] + duration < now){
m_sent.pop_front();
}
if (m_sent.size() >= GlobalSettings::instance().DISCORD.webhooks.sends_per_second){
m_logger.log("Throttling webhook messages due to rate limit...", COLOR_RED);
std::unique_lock<std::mutex> lg(m_lock);
m_cv.wait_for(
lg, duration,
[&]{ return m_sent[0] + duration < now || m_stopping; }
);
if (m_stopping){
return;
}
m_sent.clear();
}
m_sent.push_back(now);
}
void DiscordWebhookSender::process_reply(QNetworkReply* reply){
if (!reply){
m_logger.log("QNetworkReply is null.", COLOR_RED);
}else if (reply->error() == QNetworkReply::NoError){
// QString contents = QString::fromUtf8(reply->readAll());
// qDebug() << contents;
}else{
QString error_string = reply->errorString();
QString url = reply->url().toString();
int index = error_string.indexOf(url);
if (index >= 0){
error_string.replace(index, url.size(), "****************");
}
m_logger.log("Discord Request Response: " + error_string.toStdString(), COLOR_RED);
// QString err = reply->errorString();
// qDebug() << err;
}
}
void DiscordWebhookSender::internal_send_json(const QUrl& url, const QByteArray& data){
QEventLoop event_loop;
connect(
this, &DiscordWebhookSender::stop_event_loop,
&event_loop, &QEventLoop::quit
);
QNetworkRequest request(url);
request.setHeader(QNetworkRequest::ContentTypeHeader, "application/json");
QNetworkAccessManager manager;
event_loop.connect(&manager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
m_logger.log("Sending Webhook Message...", COLOR_BLUE);
std::unique_ptr<QNetworkReply> reply(manager.post(request, data));
event_loop.exec();
process_reply(reply.get());
}
void DiscordWebhookSender::internal_send_file(const QUrl& url, const std::string& filename){
QEventLoop event_loop;
connect(
this, &DiscordWebhookSender::stop_event_loop,
&event_loop, &QEventLoop::quit
);
QFile file(QString::fromStdString(filename));
if (!file.open(QIODevice::ReadOnly)){
m_logger.log("File doesn't exist: " + filename, COLOR_RED);
return;
}
QNetworkRequest request(url);
QHttpPart imagePart;
imagePart.setHeader(
QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"file1\"; filename=\"" + file.fileName() + "\"")
);
imagePart.setBodyDevice(&file);
QHttpMultiPart multiPart(QHttpMultiPart::FormDataType);
multiPart.append(imagePart);
QNetworkAccessManager manager;
event_loop.connect(&manager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
m_logger.log("Sending Webhook Message...", COLOR_BLUE);
std::unique_ptr<QNetworkReply> reply(manager.post(request, &multiPart));
event_loop.exec();
process_reply(reply.get());
}
void DiscordWebhookSender::internal_send_image_embed(const QUrl& url, const QByteArray& data, const std::string& filepath, const std::string& filename){
QEventLoop event_loop;
connect(
this, &DiscordWebhookSender::stop_event_loop,
&event_loop, &QEventLoop::quit
);
QFile file(QString::fromStdString(filepath));
if (!file.open(QIODevice::ReadOnly)){
m_logger.log("File doesn't exist: " + filepath, COLOR_RED);
return;
}
QNetworkRequest request(url);
QHttpPart imagePart;
imagePart.setHeader(
QNetworkRequest::ContentDispositionHeader,
QVariant("application/octet-stream; name=file0; filename=" + QString::fromStdString(filename))
);
imagePart.setBodyDevice(&file);
QHttpPart jsonPart;
jsonPart.setHeader(
QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=payload_json")
);
jsonPart.setBody(data);
QHttpMultiPart multiPart(QHttpMultiPart::FormDataType);
multiPart.append(imagePart);
multiPart.append(jsonPart);
QNetworkAccessManager manager;
event_loop.connect(&manager, SIGNAL(finished(QNetworkReply*)), SLOT(quit()));
m_logger.log("Sending Webhook Message...", COLOR_BLUE);
std::unique_ptr<QNetworkReply> reply(manager.post(request, &multiPart));
event_loop.exec();
process_reply(reply.get());
}
void send_embed(
Logger& logger,
bool should_ping,
const std::vector<std::string>& tags,
const JsonArray& embeds,
std::shared_ptr<PendingFileSend> file
){
DiscordSettingsOption& settings = GlobalSettings::instance().DISCORD;
if (!settings.webhooks.enabled()){
return;
}
MessageBuilder builder(tags);
std::vector<std::unique_ptr<DiscordWebhookUrl>> list = settings.webhooks.urls.copy_snapshot();
for (size_t c = 0; c < list.size(); c++){
const DiscordWebhookUrl& url = *list[c];
if (!url.enabled || ((std::string)url.url).empty()){
continue;
}
if (!builder.should_send(EventNotificationOption::parse_tags(url.tags_text))){
continue;
}
std::chrono::seconds delay(url.delay);
JsonObject json;
json["content"] = builder.build_message(
delay,
should_ping && url.ping,
settings.message.user_id,
settings.message.message
);
if (!embeds.empty()){
json["embeds"] = embeds.clone();
}
DiscordWebhookSender::instance().send_json(
logger,
QString::fromStdString(url.url),
delay,
std::move(json),
file
);
}
}
}
}
}