diff options
| author | ilotterytea <iltsu@alright.party> | 2025-04-15 04:39:06 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-04-15 04:39:06 +0500 |
| commit | 25477e58bd54585066f518a0e87317a7cd9a6632 (patch) | |
| tree | ddd9f1c36b6bf3629bdd0109fa9eb79e92ee864b | |
| parent | c9d499a3bbd57df13c073798f4cae03272509625 (diff) | |
feat: !settings in lua
| -rw-r--r-- | bot/src/commands/command.cpp | 2 | ||||
| -rw-r--r-- | bot/src/modules/settings.hpp | 119 | ||||
| -rw-r--r-- | luamods/settings.lua | 147 |
3 files changed, 147 insertions, 121 deletions
diff --git a/bot/src/commands/command.cpp b/bot/src/commands/command.cpp index fe29b28..7364125 100644 --- a/bot/src/commands/command.cpp +++ b/bot/src/commands/command.cpp @@ -17,7 +17,6 @@ #include "../modules/event.hpp" #include "../modules/help.hpp" #include "../modules/notify.hpp" -#include "../modules/settings.hpp" #include "../modules/timer.hpp" #include "../utils/chrono.hpp" #include "commands/lua.hpp" @@ -33,7 +32,6 @@ namespace bot { this->add_command(std::make_unique<mod::CustomCommand>()); this->add_command(std::make_unique<mod::Timer>()); this->add_command(std::make_unique<mod::Help>()); - this->add_command(std::make_unique<mod::Settings>()); this->add_command(std::make_unique<mod::LuaExecution>()); this->add_command(std::make_unique<mod::LuaRemoteExecution>()); diff --git a/bot/src/modules/settings.hpp b/bot/src/modules/settings.hpp deleted file mode 100644 index 75060f3..0000000 --- a/bot/src/modules/settings.hpp +++ /dev/null @@ -1,119 +0,0 @@ -#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/luamods/settings.lua b/luamods/settings.lua new file mode 100644 index 0000000..1ac037a --- /dev/null +++ b/luamods/settings.lua @@ -0,0 +1,147 @@ +local lines = { + english = { + ["no_value"] = "{sender.alias_name}: Value must be provided.", + ["no_subcommand"] = + "{sender.alias_name}: Subcommand must be provided. Use {channel.prefix}help to get more information.", + ["locale_not_exists"] = "{sender.alias_name}: Language %s not found", + ["set_locale"] = "{sender.alias_name}: This bot will speak English in this chat!", + ["set_prefix"] = "{sender.alias_name}: Prefix \"%s\" has been set for this chat!", + ["no_feature"] = "{sender.alias_name}: Feature %s not found", + ["feature_disabled"] = "{sender.alias_name}: Feature %s has been disabled", + ["feature_enabled"] = "{sender.alias_name}: Feature %s has been enabled" + }, + russian = { + ["no_value"] = "{sender.alias_name}: Значение требуется для этой команды.", + ["no_subcommand"] = + "{sender.alias_name}: Подкоманда требуется для этой команды. Используйте {channel.prefix}help для большей информации.", + ["locale_not_exists"] = "{sender.alias_name}: Язык %s не найден", + ["set_locale"] = "{sender.alias_name}: Этот бот будет говорить по-русски!", + ["set_prefix"] = "{sender.alias_name}: Префикс \"%s\" установлен для этого чата!", + ["no_feature"] = "{sender.alias_name}: Функция %s не найдена", + ["feature_disabled"] = "{sender.alias_name}: Функция %s теперь выключена", + ["feature_enabled"] = "{sender.alias_name}: Функция %s теперь включена" + }, +} + +return { + name = "set", + description = [[ +> This command is for broadcaster and moderators only. + + +The `!set` command gives broadcasters ability to customize the bot as they need it to be more fitted for chat. + + +## Available features + ++ `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 + +### Set the bot localization for the chat +`!set locale [lang]` + ++ `[lang]` - Language name in English and lowercase. +Available languages at the moment: **english**, **russian**. + +### Set the bot prefix +`!set prefix [characters]` + ++ `[characters]` - Characters to be set as a prefix. + +### Enable/disable the bot feature for the chat +`!set feature [feature]` + ++ `[feature]` - [Available features](#available-features) + +## Usage + +### Setting the bot localization + ++ `!set locale russian` ++ `!set locale english` + +### Setting the bot prefix + ++ `!set prefix ~` ++ `!set prefix ?!` + +### Enabling/disabling the bot feature + ++ `!set feature notify_7tv_updates` + +## Responses + +### Setting the bot localization + ++ `Успешно установил язык чата на русский!` ++ `Successfully set the chat language to English!` + +### Setting the bot prefix + ++ `Successfully set the chat prefix to "~"` ++ `Successfully set the chat prefix to "?!"` + +### Enabling/disabling the bot feature + ++ `Successfully enabled the "markov_responses" feature for this chat!` ++ `Successfully disabled the "random_markov_responses" feature for this chat!` +]], + delay_sec = 1, + options = {}, + subcommands = { "locale", "prefix", "feature" }, + aliases = {}, + minimal_rights = "moderator", + handle = function(request) + if request.subcommand_id == nil then + return l10n_custom_formatted_line_request(request, lines, "no_subcommand", {}) + end + + if request.message == nil then + return l10n_custom_formatted_line_request(request, lines, "no_value", {}) + end + + local parts = str_split(request.message, ' ') + + local value = parts[1] + + if request.subcommand_id == "locale" then + local locals = l10n_get_localization_names() + if not array_contains(locals, value) then + return l10n_custom_formatted_line_request(request, lines, "locale_not_exists", { value }) + end + + db_execute('UPDATE channel_preferences SET locale = $1 WHERE channel_id = $2', { value, request.channel.id }) + request['channel_preference']['language'] = value + + return l10n_custom_formatted_line_request(request, lines, "set_locale", {}) + elseif request.subcommand_id == "prefix" then + value = value:gsub(" ", " ") + db_execute('UPDATE channel_preferences SET prefix = $1 WHERE channel_id = $2', { value, request.channel.id }) + return l10n_custom_formatted_line_request(request, lines, "set_prefix", { value }) + elseif request.subcommand_id == "feature" then + local feature = str_to_feature(value) + if feature == nil then + return l10n_custom_formatted_line_request(request, lines, "no_feature", { value }) + end + + local channel_features = request.channel_preference.features + + local line_id = "" + local query = "" + + if array_contains(channel_features, value) then + line_id = "feature_disabled" + query = 'UPDATE channel_preferences SET features = array_remove(features, $1) WHERE channel_id = $2' + else + line_id = "feature_enabled" + query = 'UPDATE channel_preferences SET features = array_append(features, $1) WHERE channel_id = $2' + end + + db_execute(query, { feature, request.channel.id }) + + return l10n_custom_formatted_line_request(request, lines, line_id, { value }) + end + end, +} |
