Repository navigation
Expand file tree
/
Copy pathDiscordIntegrationSettings.cpp
More file actions
230 lines (194 loc) · 6.37 KB
/
Copy pathDiscordIntegrationSettings.cpp
File metadata and controls
230 lines (194 loc) · 6.37 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
/* Discord Integration Settings
*
* From: https://github.com/PokemonAutomation/
*
*/
#include "Common/Cpp/Strings/Unicode.h"
#include "Common/Qt/StringToolsQt.h"
#include "Integrations/DppIntegration/DppClient.h"
#include "DiscordIntegrationSettings.h"
//#include <iostream>
//using std::cout;
//using std::endl;
namespace PokemonAutomation{
namespace Integration{
DiscordIntegrationSettingsOption::~DiscordIntegrationSettingsOption(){
m_start_button.remove_listener(static_cast<ButtonListener&>(*this));
m_stop_button.remove_listener(static_cast<ButtonListener&>(*this));
this->remove_listener(*this);
}
DiscordIntegrationSettingsOption::DiscordIntegrationSettingsOption()
: GroupOption(
"Discord Integration Settings",
LockMode::LOCK_WHILE_RUNNING,
GroupOption::EnableMode::DEFAULT_DISABLED,
false,
false
)
, m_start_button("<b>Bot Control:</b>", "Start Bot")
, m_stop_button("", "Stop Bot")
, run_on_start(
"<b>Run Discord Integration on Launch:</b><br>Automatically connect to Discord as soon as the program is launched.",
LockMode::LOCK_WHILE_RUNNING,
false
)
, register_slash_button(
"<b>Register Slash Commands on Start:</b><br>Only needs to be run once, or after additional commands are added.",
"Register Slash Commands"
)
, 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"},
},
LockMode::LOCK_WHILE_RUNNING,
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.",
LockMode::LOCK_WHILE_RUNNING,
"", "0123456789abcdefghijklmnopqrstuvwxyzABCDEGFHIJKLMNOPQRSTUVWXYZ"
)
, command_prefix(
false,
"<b>Discord Command Prefix:</b><br>Enter a command prefix for your bot.",
LockMode::LOCK_WHILE_RUNNING,
"^", "^"
)
, game_status(
false,
"<b>Discord Game Status:</b><br>Enter a status message your bot would display.",
LockMode::LOCK_WHILE_RUNNING,
"", "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.",
LockMode::LOCK_WHILE_RUNNING,
"", "Automation at your service!"
)
, allow_buttons_from_users(
"<b>Allow Buttons from Users:</b><br>Allow other users to issue button presses.",
LockMode::LOCK_WHILE_RUNNING,
false
)
{
PA_ADD_OPTION(m_start_button);
PA_ADD_OPTION(m_stop_button);
PA_ADD_OPTION(run_on_start);
PA_ADD_OPTION(register_slash_button);
PA_ADD_OPTION(command_type);
PA_ADD_OPTION(token);
PA_ADD_OPTION(command_prefix);
PA_ADD_OPTION(game_status);
PA_ADD_OPTION(hello_message);
PA_ADD_OPTION(allow_buttons_from_users);
PA_ADD_OPTION(channels);
DiscordIntegrationSettingsOption::on_config_value_changed(this);
this->add_listener(*this);
m_start_button.add_listener(static_cast<ButtonListener&>(*this));
m_stop_button.add_listener(static_cast<ButtonListener&>(*this));
}
void DiscordIntegrationSettingsOption::on_config_value_changed([[maybe_unused]] void* object){
// cout << this->enabled() << endl;
#ifdef PA_DPP
bool options_enabled = this->enabled();
options_enabled &= !DppClient::Client::instance().is_initialized();
ConfigOptionState state = options_enabled ? ConfigOptionState::ENABLED : ConfigOptionState::DISABLED;
command_type.set_visibility(state);
token.set_visibility(state);
game_status.set_visibility(state);
hello_message.set_visibility(state);
allow_buttons_from_users.set_visibility(state);
command_prefix.set_visibility(state);
#endif
}
void DiscordIntegrationSettingsOption::on_press(ButtonCell& button){
#ifdef PA_DPP
if (&button == &m_start_button){
DppClient::Client::instance().connect();
on_config_value_changed(this);
return;
}
if (&button == &m_stop_button){
DppClient::Client::instance().disconnect();
on_config_value_changed(this);
return;
}
#else
(void)button;
#endif
}
std::string convert(const std::string& message){
std::u32string ret;
std::u32string block;
for (char32_t ch : utf8_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 utf32_to_str(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;
}
}
}