Repository navigation
Expand file tree
/
Copy pathDiscordIntegrationSettings.cpp
More file actions
323 lines (282 loc) · 9.46 KB
/
Copy pathDiscordIntegrationSettings.cpp
File metadata and controls
323 lines (282 loc) · 9.46 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
/* Discord Integration Settings
*
* From: https://github.com/PokemonAutomation/Arduino-Source
*
*/
#include <QHBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <dpp/DPP_SilenceWarnings.h>
#include "Common/Qt/StringToolsQt.h"
//#include "CommonFramework/Globals.h"
//#include "CommonFramework/GlobalSettingsPanel.h"
#include "DppIntegration/DppClient.h"
#include "SleepyDiscordRunner.h"
#include "DiscordIntegrationSettings.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
namespace Integration{
DiscordIntegrationSettingsOption::~DiscordIntegrationSettingsOption(){
library0.remove_listener(*this);
this->remove_listener(*this);
}
DiscordIntegrationSettingsOption::DiscordIntegrationSettingsOption()
: GroupOption("Discord Integration Settings", LockWhileRunning::LOCKED, true, false)
// , m_integration_enabled(integration_enabled)
, run_on_start(
"<b>Run Discord Integration on Launch:</b><br>Automatically connect to Discord as soon as the program is launched.",
LockWhileRunning::LOCKED,
false
)
, library0(
"<b>Discord Integration Library:</b><br>Restart the program for this to take effect.",
{
{Library::SleepyDiscord, "sleepy", "Sleepy Discord (normal commands, deprecated)"},
{Library::DPP, "dpp", "D++ (slash commands and normal commands)"},
},
LockWhileRunning::LOCKED,
Library::DPP
)
, command_type(
"<b>Discord Integration Command Type:</b><br>Restart the program for this to take effect.",
{
{CommandType::SlashCommands, "slash", "Slash commands"},
{CommandType::MessageCommands, "message", "Message (prefix) commands"},
},
LockWhileRunning::LOCKED,
CommandType::SlashCommands
)
, token(
true,
"<b>Discord Token:</b><br>Enter your Discord bot's token. Keep it safe and don't share it with anyone.",
LockWhileRunning::LOCKED,
"", "0123456789abcdefghijklmnopqrstuvwxyzABCDEGFHIJKLMNOPQRSTUVWXYZ"
)
, command_prefix(
false,
"<b>Discord Command Prefix:</b><br>Enter a command prefix for your bot.",
LockWhileRunning::LOCKED,
"^", "^"
)
, use_suffix(
"<b>Use Suffix (Sleepy):</b><br>Use a suffix instead of a prefix for commands.",
LockWhileRunning::LOCKED,
false
)
, game_status(
false,
"<b>Discord Game Status:</b><br>Enter a status message your bot would display.",
LockWhileRunning::LOCKED,
"", "Controlling your Switch. :)"
)
, hello_message(
false,
"<b>Discord Hello Message:</b><br>Enter a message you'd like the bot to respond with to the \"$hi\" command.",
LockWhileRunning::LOCKED,
"", "Automation at your service!"
)
, sudo(
false,
"<b>Discord Sudo (Sleepy):</b><br>Enter user ID(s) to grant sudo access to.",
LockWhileRunning::LOCKED,
"", "123456789012345678"
)
, owner(
false,
"<b>Discord Owner (Sleepy):</b><br>Enter the bot owner's ID (your own ID).",
LockWhileRunning::LOCKED,
"", "123456789012345678"
)
{
PA_ADD_OPTION(run_on_start);
PA_ADD_OPTION(library0);
PA_ADD_OPTION(command_type);
PA_ADD_OPTION(token);
PA_ADD_OPTION(command_prefix);
PA_ADD_OPTION(use_suffix);
PA_ADD_OPTION(game_status);
PA_ADD_OPTION(hello_message);
PA_ADD_OPTION(sudo);
PA_ADD_OPTION(owner);
PA_ADD_OPTION(channels);
DiscordIntegrationSettingsOption::value_changed();
this->add_listener(*this);
library0.add_listener(*this);
}
void DiscordIntegrationSettingsOption::value_changed(){
// cout << this->enabled() << endl;
#if (defined PA_SLEEPY || defined PA_DPP)
bool options_enabled = this->enabled();
switch (library0){
#ifdef PA_SLEEPY
case Library::SleepyDiscord:{
options_enabled &= !SleepyDiscordRunner::is_running();
ConfigOptionState state = options_enabled ? ConfigOptionState::ENABLED : ConfigOptionState::DISABLED;
library0.set_visibility(state);
command_type.set_visibility(ConfigOptionState::HIDDEN);
token.set_visibility(state);
game_status.set_visibility(state);
hello_message.set_visibility(state);
command_prefix.set_visibility(state);
use_suffix.set_visibility(state);
sudo.set_visibility(state);
owner.set_visibility(state);
break;
}
#endif
#ifdef PA_DPP
case Library::DPP:{
options_enabled &= !DppClient::Client::instance().is_initialized();
ConfigOptionState state = options_enabled ? ConfigOptionState::ENABLED : ConfigOptionState::DISABLED;
library0.set_visibility(state);
command_type.set_visibility(state);
token.set_visibility(state);
game_status.set_visibility(state);
hello_message.set_visibility(state);
command_prefix.set_visibility(state);
use_suffix.set_visibility(ConfigOptionState::HIDDEN);
sudo.set_visibility(ConfigOptionState::HIDDEN);
owner.set_visibility(ConfigOptionState::HIDDEN);
break;
}
#endif
}
#endif
}
class DiscordIntegrationSettingsWidget : public GroupWidget{
public:
DiscordIntegrationSettingsWidget(QWidget& parent, DiscordIntegrationSettingsOption& value);
};
ConfigWidget* DiscordIntegrationSettingsOption::make_QtWidget(QWidget& parent){
return new DiscordIntegrationSettingsWidget(parent, *this);
}
DiscordIntegrationSettingsWidget::DiscordIntegrationSettingsWidget(QWidget& parent, DiscordIntegrationSettingsOption& value)
: GroupWidget(parent, value)
{
#if (defined PA_SLEEPY || defined PA_DPP)
QWidget* control_buttons = new QWidget(this);
m_options_layout->insertWidget(0, control_buttons);
QHBoxLayout* layout = new QHBoxLayout(control_buttons);
layout->setContentsMargins(5, 5, 5, 5);
QLabel* text = new QLabel("<b>Bot Control:</b>", control_buttons);
layout->addWidget(text, 2);
text->setWordWrap(true);
QPushButton* button_start = new QPushButton("Start Bot", this);
layout->addWidget(button_start, 1);
QPushButton* button_stop = new QPushButton("Stop Bot", this);
layout->addWidget(button_stop, 1);
QFont font = button_start->font();
font.setBold(true);
button_start->setFont(font);
button_stop->setFont(font);
connect(
button_start, &QPushButton::clicked,
this, [&value](bool) {
switch (value.library0){
#ifdef PA_SLEEPY
case DiscordIntegrationSettingsOption::Library::SleepyDiscord:
SleepyDiscordRunner::sleepy_connect();
break;
#endif
#ifdef PA_DPP
case DiscordIntegrationSettingsOption::Library::DPP:
DppClient::Client::instance().connect();
break;
#endif
}
value.value_changed();
}
);
connect(
button_stop, &QPushButton::clicked,
this, [&value](bool) {
switch (value.library0){
#ifdef PA_SLEEPY
case DiscordIntegrationSettingsOption::Library::SleepyDiscord:
SleepyDiscordRunner::sleepy_terminate();
break;
#endif
#ifdef PA_DPP
case DiscordIntegrationSettingsOption::Library::DPP:
DppClient::Client::instance().disconnect();
break;
#endif
}
value.value_changed();
}
);
#endif
}
std::string convert(const std::string& message){
std::u32string ret;
std::u32string block;
for (char32_t ch : to_utf32(message)){
switch (ch){
case 38:
if (block.empty() || block.back() != 64){
break;
}
continue;
case 64:
block += ch;
continue;
}
ret += ch;
block.clear();
}
return to_utf8(ret);
}
MessageBuilder::MessageBuilder(const std::vector<std::string>& message_tags){
for (const std::string& tag : message_tags){
m_message_tags.insert(to_lower(tag));
}
}
bool MessageBuilder::should_send(const std::vector<std::string>& channel_tags) const{
for (const std::string& tag : channel_tags){
auto iter = m_message_tags.find(to_lower(tag));
if (iter != m_message_tags.end()){
return true;
}
}
return false;
}
std::string MessageBuilder::build_message(
std::chrono::seconds delay,
bool ping, const std::string& user_id,
const std::string& message
) const{
if (std::atoll(user_id.c_str()) == 0){
ping = false;
}
std::string str;
if (ping){
str += "<?" + user_id + ">";
str[1]++;
}
if (!message.empty()){
if (!str.empty()){
str += " ";
}
str += convert(message);
}
auto delay_seconds = delay.count();
if (delay_seconds != 0){
if (!str.empty()){
str += "\n";
}
str += "*(";
str += "Message ";
str += "delay";
str += "ed ";
str += "by " + std::to_string(delay_seconds);
str += delay_seconds == 1
? " second"
: " seconds";
str += ".)*";
}
return str;
}
}
}