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
|
#pragma once
#include <string>
#include <vector>
#include "../bundle.hpp"
#include "../commands/command.hpp"
#include "../commands/response_error.hpp"
#include "../schemas/stream.hpp"
namespace bot {
namespace mod {
class Event : public command::Command {
std::string get_name() const override { return "event"; }
schemas::PermissionLevel get_permission_level() const override {
return schemas::PermissionLevel::MODERATOR;
}
std::vector<std::string> get_subcommand_ids() const override {
return {"on", "off"};
}
command::Response run(const InstanceBundle &bundle,
const command::Request &request) const override {
if (!request.subcommand_id.has_value()) {
throw ResponseException<NOT_ENOUGH_ARGUMENTS>(
request, bundle.localization, command::SUBCOMMAND);
}
const std::string &subcommand_id = request.subcommand_id.value();
if (!request.message.has_value()) {
throw ResponseException<ResponseError::NOT_ENOUGH_ARGUMENTS>(
request, bundle.localization, command::CommandArgument::TARGET);
}
const std::string &message = request.message.value();
std::vector<std::string> s = utils::string::split_text(message, ' ');
std::string target;
schemas::EventType type;
std::vector<std::string> target_and_type =
utils::string::split_text(s[0], ':');
if (target_and_type.size() != 2) {
throw ResponseException<ResponseError::INCORRECT_ARGUMENT>(
request, bundle.localization, s[0]);
}
s.erase(s.begin());
target = target_and_type[0];
type = schemas::string_to_event_type(target_and_type[1]);
std::string t = target_and_type[0] + ":" + target_and_type[1];
auto channels = bundle.helix_client.get_users({target});
api::twitch::schemas::User channel;
if (channels.empty() && type < schemas::EventType::GITHUB) {
throw ResponseException<ResponseError::NOT_FOUND>(
request, bundle.localization, t);
}
pqxx::work work(request.conn);
std::string query;
if (type < schemas::GITHUB) {
channel = channels[0];
query = "SELECT id FROM events WHERE channel_id = " +
std::to_string(request.channel.get_id()) +
" AND target_alias_id = " + std::to_string(channel.id) +
" AND event_type = " + std::to_string(type);
} else {
query = "SELECT id FROM events WHERE channel_id = " +
std::to_string(request.channel.get_id()) +
" AND custom_alias_id = '" + target +
"' AND event_type = " + std::to_string(type);
}
pqxx::result event = work.exec(query);
if (subcommand_id == "on") {
if (!event.empty()) {
throw ResponseException<ResponseError::NAMESAKE_CREATION>(
request, bundle.localization, t);
}
if (s.empty()) {
throw ResponseException<ResponseError::NOT_ENOUGH_ARGUMENTS>(
request, bundle.localization,
command::CommandArgument::MESSAGE);
}
std::string m = utils::string::str(s.begin(), s.end(), ' ');
if (type < schemas::GITHUB) {
query =
"INSERT INTO events (channel_id, target_alias_id, "
"event_type, "
"message) VALUES (" +
std::to_string(request.channel.get_id()) + ", " +
std::to_string(channel.id) + ", " + std::to_string(type) +
", '" + m + "')";
} else {
query =
"INSERT INTO events (channel_id, custom_alias_id, "
"event_type, "
"message) VALUES (" +
std::to_string(request.channel.get_id()) + ", '" + target +
"', " + std::to_string(type) + ", '" + m + "')";
}
work.exec(query);
work.commit();
return command::Response(
bundle.localization
.get_formatted_line(request, loc::LineId::EventOn, {t})
.value());
} else if (subcommand_id == "off") {
if (event.empty()) {
throw ResponseException<ResponseError::NOT_FOUND>(
request, bundle.localization, t);
}
work.exec("DELETE FROM events WHERE id = " +
std::to_string(event[0][0].as<int>()));
work.commit();
return command::Response(
bundle.localization
.get_formatted_line(request, loc::LineId::EventOff, {t})
.value());
}
throw ResponseException<ResponseError::SOMETHING_WENT_WRONG>(
request, bundle.localization);
}
};
}
}
|