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
|
#pragma once
#include <sys/resource.h>
#include <sys/types.h>
#include <unistd.h>
#include <algorithm>
#include <optional>
#include <string>
#include <vector>
#include "../bundle.hpp"
#include "../commands/command.hpp"
#include "../commands/response_error.hpp"
namespace bot {
namespace mod {
class Settings : public command::Command {
std::string get_name() const override { return "set"; }
schemas::PermissionLevel get_permission_level() const override {
return schemas::PermissionLevel::MODERATOR;
}
std::vector<std::string> get_subcommand_ids() const override {
return {"locale", "prefix", "feature"};
}
command::Response run(const InstanceBundle &bundle,
const command::Request &request) const override {
if (!request.message.has_value()) {
throw ResponseException<ResponseError::NOT_ENOUGH_ARGUMENTS>(
request, bundle.localization, command::CommandArgument::VALUE);
}
pqxx::work work(request.conn);
std::vector<std::string> parts =
utils::string::split_text(request.message.value(), ' ');
std::string &value = parts[0];
if (request.subcommand_id == "locale") {
auto locals = bundle.localization.get_loaded_localizations();
if (!std::any_of(locals.begin(), locals.end(),
[&value](const auto &x) { return x == value; })) {
throw ResponseException<ResponseError::NOT_FOUND>(
request, bundle.localization, value);
}
work.exec_params(
"UPDATE channel_preferences SET locale = $1 WHERE channel_id = "
"$2",
value, request.channel.get_id());
work.commit();
return command::Response(
bundle.localization
.get_formatted_line(request, loc::LineId::SetLocale,
{value})
.value());
} else if (request.subcommand_id == "prefix") {
work.exec_params(
"UPDATE channel_preferences SET prefix = $1 WHERE channel_id = "
"$2",
value, request.channel.get_id());
work.commit();
return command::Response(
bundle.localization
.get_formatted_line(request, loc::LineId::SetPrefix,
{value})
.value());
} else if (request.subcommand_id == "feature") {
std::optional<schemas::ChannelFeature> feature =
schemas::string_to_channel_feature(value);
if (!feature.has_value()) {
throw ResponseException<ResponseError::NOT_FOUND>(
request, bundle.localization, value);
}
loc::LineId line_id;
std::string query;
if (std::any_of(request.channel_preferences.get_features().begin(),
request.channel_preferences.get_features().end(),
[&feature](const auto &x) {
return x == feature.value();
})) {
line_id = loc::LineId::SetFeatureDisabled;
query =
"UPDATE channel_preferences SET features = "
"array_remove(features, $1) WHERE channel_id = $2";
} else {
line_id = loc::LineId::SetFeatureEnabled;
query =
"UPDATE channel_preferences SET features = "
"array_append(features, $1) WHERE channel_id = $2";
}
work.exec_params(query, (int)feature.value(),
request.channel.get_id());
work.commit();
return command::Response(
bundle.localization
.get_formatted_line(request, line_id, {value})
.value());
}
work.commit();
throw ResponseException<ResponseError::SOMETHING_WENT_WRONG>(
request, bundle.localization);
}
};
}
}
|