summaryrefslogtreecommitdiff
path: root/bot
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-12-14 23:23:12 +0500
committerilotterytea <iltsu@alright.party>2024-12-14 23:23:12 +0500
commit1134c8316e5ee445567f9bf225e9fb9c2631b4dc (patch)
tree05380fdd9ab9b40af9443bb5e0cd52aeec36a53a /bot
parent79c39906131bb72780da96ccc6eb43524dec3350 (diff)
feat: settings command
Diffstat (limited to 'bot')
-rw-r--r--bot/src/commands/command.cpp2
-rw-r--r--bot/src/localization/line_id.cpp10
-rw-r--r--bot/src/localization/line_id.hpp7
-rw-r--r--bot/src/modules/settings.hpp119
4 files changed, 137 insertions, 1 deletions
diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp
index 3f99d9e..7dd4e47 100644
--- a/bot/src/commands/command.cpp
+++ b/bot/src/commands/command.cpp
@@ -17,6 +17,7 @@
#include "../modules/massping.hpp"
#include "../modules/notify.hpp"
#include "../modules/ping.hpp"
+#include "../modules/settings.hpp"
#include "../modules/spam.hpp"
#include "../modules/timer.hpp"
#include "../utils/chrono.hpp"
@@ -36,6 +37,7 @@ namespace bot {
this->add_command(std::make_unique<mod::Help>());
this->add_command(std::make_unique<mod::Chatters>());
this->add_command(std::make_unique<mod::Spam>());
+ this->add_command(std::make_unique<mod::Settings>());
}
void CommandLoader::add_command(std::unique_ptr<Command> command) {
diff --git a/bot/src/localization/line_id.cpp b/bot/src/localization/line_id.cpp
index dabd85a..7f17378 100644
--- a/bot/src/localization/line_id.cpp
+++ b/bot/src/localization/line_id.cpp
@@ -96,6 +96,16 @@ namespace bot {
return LineId::ChattersResponse;
}
+ else if (str == "set.locale") {
+ return LineId::SetLocale;
+ } else if (str == "set.prefix") {
+ return LineId::SetPrefix;
+ } else if (str == "set.feature.disabled") {
+ return LineId::SetFeatureDisabled;
+ } else if (str == "set.feature.enabled") {
+ return LineId::SetFeatureEnabled;
+ }
+
else {
return std::nullopt;
}
diff --git a/bot/src/localization/line_id.hpp b/bot/src/localization/line_id.hpp
index 6d7729e..e9b90c0 100644
--- a/bot/src/localization/line_id.hpp
+++ b/bot/src/localization/line_id.hpp
@@ -50,7 +50,12 @@ namespace bot {
HelpResponse,
- ChattersResponse
+ ChattersResponse,
+
+ SetLocale,
+ SetPrefix,
+ SetFeatureDisabled,
+ SetFeatureEnabled
};
std::optional<LineId> string_to_line_id(const std::string &str);
diff --git a/bot/src/modules/settings.hpp b/bot/src/modules/settings.hpp
new file mode 100644
index 0000000..75060f3
--- /dev/null
+++ b/bot/src/modules/settings.hpp
@@ -0,0 +1,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);
+ }
+ };
+ }
+}