Repository navigation
Expand file tree
/
Copy pathDiscordWebhookSettings.cpp
More file actions
113 lines (97 loc) · 3.33 KB
/
Copy pathDiscordWebhookSettings.cpp
File metadata and controls
113 lines (97 loc) · 3.33 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
/* Discord Webhook Settings
*
* From: https://github.com/PokemonAutomation/
*
*/
#include "Common/Cpp/Json/JsonValue.h"
#include "Common/Cpp/Json/JsonArray.h"
#include "Common/Cpp/Json/JsonObject.h"
#include "CommonFramework/Notifications/EventNotificationsTable.h"
#include "DiscordWebhookSettings.h"
namespace PokemonAutomation{
namespace Integration{
DiscordWebhookUrl::DiscordWebhookUrl(EditableTableOption& parent_table)
: EditableTableRow(parent_table)
, enabled(LockMode::LOCK_WHILE_RUNNING, true)
, label(false, LockMode::LOCK_WHILE_RUNNING, "", "My test server")
, ping(LockMode::LOCK_WHILE_RUNNING, true)
, tags_text(false, LockMode::LOCK_WHILE_RUNNING, "Notifs, Showcase, LiveHost", "")
, delay(LockMode::LOCK_WHILE_RUNNING, 0, 0, 10)
, url(true, LockMode::LOCK_WHILE_RUNNING, "", "https://discord.com/api/webhooks/123456789012345678/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
{
// Keep the old JSON tags for backwards compatibility.
add_option(enabled, "Enabled");
add_option(label, "Label");
add_option(ping, "Ping");
add_option(tags_text, "Tags");
add_option(delay, "Delay");
add_option(url, "URL");
}
std::unique_ptr<EditableTableRow> DiscordWebhookUrl::clone() const{
std::unique_ptr<DiscordWebhookUrl> ret(new DiscordWebhookUrl(parent()));
ret->enabled = (bool)enabled;
ret->label.set(label);
ret->ping = (bool)ping;
ret->tags_text.set(tags_text);
ret->delay.set(delay);
ret->url.set(url);
return ret;
}
void DiscordWebhookUrl::load_json(const JsonValue& json){
EditableTableRow::load_json(json);
const JsonObject* obj = json.to_object();
if (obj == nullptr){
return;
}
// Load the old tags format.
const JsonArray* array = obj->get_array("Tags");
if (array){
std::string tags;
for (const auto& tag : *array){
const std::string* str = tag.to_string();
if (str == nullptr){
continue;
}
std::string token = EventNotificationOption::sanitize_tag(*str);
if (!token.empty()){
if (!tags.empty()){
tags += ", ";
}
tags += token;
}
}
tags_text.set(std::move(tags));
}
}
DiscordWebhookSettingsTable::DiscordWebhookSettingsTable()
: EditableTableOption_t<DiscordWebhookUrl>(
"<b>Discord Webhook URLs:</b> Notifications are sent to all enabled URLs that share a tag with the event.",
LockMode::LOCK_WHILE_RUNNING
)
{}
std::vector<std::string> DiscordWebhookSettingsTable::make_header() const{
return std::vector<std::string>{
"Enabled",
"Description",
"Allow Pings",
"Tags",
"Delay (seconds)",
"Webhook URL",
};
}
DiscordWebhookSettingsOption::DiscordWebhookSettingsOption()
: GroupOption(
"Discord Webhook Settings",
LockMode::LOCK_WHILE_RUNNING,
GroupOption::EnableMode::DEFAULT_ENABLED, false
)
, sends_per_second(
"<b>Rate Limit:</b><br>Maximum number of sends per second.",
LockMode::LOCK_WHILE_RUNNING, 2, 1
)
{
PA_ADD_OPTION(urls);
PA_ADD_OPTION(sends_per_second);
}
}
}