diff options
| -rw-r--r-- | localization/english.json | 5 | ||||
| -rw-r--r-- | localization/russian.json | 5 | ||||
| -rw-r--r-- | src/commands/command.cpp | 2 | ||||
| -rw-r--r-- | src/localization/line_id.cpp | 6 | ||||
| -rw-r--r-- | src/localization/line_id.hpp | 5 | ||||
| -rw-r--r-- | src/modules/timer.hpp | 112 |
6 files changed, 132 insertions, 3 deletions
diff --git a/localization/english.json b/localization/english.json index 8cec9cb..97b74d7 100644 --- a/localization/english.json +++ b/localization/english.json @@ -35,5 +35,8 @@ "join.not_allowed": "{sender.alias_name}: The bot cannot join chat rooms!%s", "custom_command.new": "{sender.alias_name}: New command \"%s\" has been successfully created!", - "custom_command.delete": "{sender.alias_name}: The command \"%s\" has been deleted!" + "custom_command.delete": "{sender.alias_name}: The command \"%s\" has been deleted!", + + "timer.new": "{sender.alias_name}: New timer \"%s\" has been successfully created!", + "timer.delete": "{sender.alias_name}: The timer \"%s\" has been deleted!" } diff --git a/localization/russian.json b/localization/russian.json index 3fca425..ab0ebff 100644 --- a/localization/russian.json +++ b/localization/russian.json @@ -35,5 +35,8 @@ "join.not_allowed": "{sender.alias_name}: Бот не может заходить в чужие чаты!%s", "custom_command.new": "{sender.alias_name}: Новая команда \"%s\" была успешно создана!", - "custom_command.delete": "{sender.alias_name}: Команда \"%s\" была удалена!" + "custom_command.delete": "{sender.alias_name}: Команда \"%s\" была удалена!", + + "timer.new": "{sender.alias_name}: Новый таймер \"%s\" был успешно создан!", + "timer.delete": "{sender.alias_name}: Таймер \"%s\" был удален!" } diff --git a/src/commands/command.cpp b/src/commands/command.cpp index 4cfe959..0bc0f15 100644 --- a/src/commands/command.cpp +++ b/src/commands/command.cpp @@ -15,6 +15,7 @@ #include "../modules/massping.hpp" #include "../modules/notify.hpp" #include "../modules/ping.hpp" +#include "../modules/timer.hpp" #include "../utils/chrono.hpp" #include "request.hpp" @@ -27,6 +28,7 @@ namespace bot { this->add_command(std::make_unique<mod::Notify>()); this->add_command(std::make_unique<mod::Join>()); this->add_command(std::make_unique<mod::CustomCommand>()); + this->add_command(std::make_unique<mod::Timer>()); } void CommandLoader::add_command(std::unique_ptr<Command> command) { diff --git a/src/localization/line_id.cpp b/src/localization/line_id.cpp index f4ea6f1..82441fc 100644 --- a/src/localization/line_id.cpp +++ b/src/localization/line_id.cpp @@ -80,6 +80,12 @@ namespace bot { return LineId::CustomcommandDelete; } + else if (str == "timer.new") { + return LineId::TimerNew; + } else if (str == "timer.delete") { + return LineId::TimerDelete; + } + else { return std::nullopt; } diff --git a/src/localization/line_id.hpp b/src/localization/line_id.hpp index e9796bb..e820559 100644 --- a/src/localization/line_id.hpp +++ b/src/localization/line_id.hpp @@ -42,7 +42,10 @@ namespace bot { JoinNotAllowed, CustomcommandNew, - CustomcommandDelete + CustomcommandDelete, + + TimerNew, + TimerDelete }; std::optional<LineId> string_to_line_id(const std::string &str); diff --git a/src/modules/timer.hpp b/src/modules/timer.hpp new file mode 100644 index 0000000..03e319e --- /dev/null +++ b/src/modules/timer.hpp @@ -0,0 +1,112 @@ +#pragma once + +#include <string> +#include <variant> +#include <vector> + +#include "../bundle.hpp" +#include "../commands/command.hpp" +#include "../commands/response_error.hpp" + +namespace bot { + namespace mod { + class Timer : public command::Command { + std::string get_name() const override { return "timer"; } + + schemas::PermissionLevel get_permission_level() const override { + return schemas::PermissionLevel::MODERATOR; + } + + std::vector<std::string> get_subcommand_ids() const override { + return {"new", "delete"}; + } + + std::variant<std::vector<std::string>, std::string> 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::NAME); + } + + const std::string &message = request.message.value(); + std::vector<std::string> s = utils::string::split_text(message, ' '); + + std::string name = s[0]; + s.erase(s.begin()); + + pqxx::work work(request.conn); + pqxx::result timers = work.exec( + "SELECT id FROM timers WHERE name = '" + name + + "' AND channel_id = " + std::to_string(request.channel.get_id())); + + if (subcommand_id == "new") { + if (!timers.empty()) { + throw ResponseException<ResponseError::NAMESAKE_CREATION>( + request, bundle.localization, name); + } + + if (s.empty()) { + throw ResponseException<ResponseError::NOT_ENOUGH_ARGUMENTS>( + request, bundle.localization, + command::CommandArgument::INTERVAL); + } + + int interval_s; + + try { + interval_s = std::stoi(s[0]); + } catch (std::exception e) { + throw ResponseException<ResponseError::INCORRECT_ARGUMENT>( + request, bundle.localization, s[0]); + } + + s.erase(s.begin()); + + 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(), ' '); + + work.exec( + "INSERT INTO timers(channel_id, name, message, interval_sec) " + "VALUES " + "(" + + std::to_string(request.channel.get_id()) + ", '" + name + + "', '" + m + "', " + std::to_string(interval_s) + ")"); + work.commit(); + + return bundle.localization + .get_formatted_line(request, loc::LineId::TimerNew, {name}) + .value(); + } else if (subcommand_id == "delete") { + if (timers.empty()) { + throw ResponseException<ResponseError::NOT_FOUND>( + request, bundle.localization, name); + } + + work.exec("DELETE FROM timers WHERE id = " + + std::to_string(timers[0][0].as<int>())); + work.commit(); + + return bundle.localization + .get_formatted_line(request, loc::LineId::TimerDelete, {name}) + .value(); + } + + throw ResponseException<ResponseError::SOMETHING_WENT_WRONG>( + request, bundle.localization); + } + }; + } +} |
