summaryrefslogtreecommitdiff
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
parent79c39906131bb72780da96ccc6eb43524dec3350 (diff)
feat: settings command
-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
-rw-r--r--docs/channel/settings.md7
-rw-r--r--docs/summary.md5
-rw-r--r--localization/english.json7
-rw-r--r--localization/russian.json7
8 files changed, 154 insertions, 10 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);
+ }
+ };
+ }
+}
diff --git a/docs/channel/settings.md b/docs/channel/settings.md
index 1f25d32..330f3b6 100644
--- a/docs/channel/settings.md
+++ b/docs/channel/settings.md
@@ -8,7 +8,8 @@ The `!set` command gives broadcasters ability to customize the bot as they need
## Available features
-+ `notify_7tv_updates` - Enable notifications for changes to the channel's 7TV emote set.
++ `markov_responses` - Enable Markov-generated responses *(triggered by "@teabot, " prefix)*
++ `random_markov_responses` - Enable Markov-generated responses on random messages. It is required that the feature `markov_responses` is enabled.
## Syntax
@@ -58,8 +59,8 @@ Available languages at the moment: **english**, **russian**.
### Enabling/disabling the bot feature
-+ `Successfully enabled the "notify_7tv_updates" feature for this chat!`
-+ `Successfully disabled the "notify_7tv_updates" feature for this chat!`
++ `Successfully enabled the "markov_responses" feature for this chat!`
++ `Successfully disabled the "random_markov_responses" feature for this chat!`
## Error handling
diff --git a/docs/summary.md b/docs/summary.md
index ea05c68..f5b7a1e 100644
--- a/docs/summary.md
+++ b/docs/summary.md
@@ -8,6 +8,7 @@
+ [Custom commands](/wiki/channel/custom-commands)
+ [Timers](/wiki/channel/timer)
++ [Settings](/wiki/channel/settings)
## Stream features
@@ -27,10 +28,6 @@
# NOT IMPLEMENTED
-## Channel management
-
-+ [Settings](/wiki/channel/settings)
-
## Emote management
+ [Check the usage of emote](/wiki/emotes/count)
diff --git a/localization/english.json b/localization/english.json
index a198783..c7cc035 100644
--- a/localization/english.json
+++ b/localization/english.json
@@ -43,5 +43,10 @@
"help.response": "{sender.alias_name}: Bot command reference: %s",
- "chatters.response": "{sender.alias_name}: Chatter list: %s"
+ "chatters.response": "{sender.alias_name}: Chatter list: %s",
+
+ "set.locale": "{sender.alias_name}: The bot now speaks English in this chat!",
+ "set.prefix": "{sender.alias_name}: The bot prefix is now \"%s\" in this chat!",
+ "set.feature.disabled": "{sender.alias_name}: The feature \"%s\" has been disabled for this chat!",
+ "set.feature.enabled": "{sender.alias_name}: The feature \"%s\" has been enabled for this chat!"
}
diff --git a/localization/russian.json b/localization/russian.json
index f170bb3..3790aea 100644
--- a/localization/russian.json
+++ b/localization/russian.json
@@ -43,5 +43,10 @@
"help.response": "{sender.alias_name}: Справочник по командам бота: %s",
- "chatters.response": "{sender.alias_name}: Список чаттеров: %s"
+ "chatters.response": "{sender.alias_name}: Список чаттеров: %s",
+
+ "set.locale": "{sender.alias_name}: Теперь бот говорит по-русски в этом чате!",
+ "set.prefix": "{sender.alias_name}: Префикс бота был установлен как \"%s\"",
+ "set.feature.disabled": "{sender.alias_name}: Функция \"%s\" была выключена в этом чате!",
+ "set.feature.enabled": "{sender.alias_name}: Функция \"%s\" была включена в этом чате!"
}