From 860e6216e9c0c5e554d07aa0db8ab74dfbb873f7 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Wed, 8 May 2024 23:29:37 +0500 Subject: feat: new command - !scmd --- localization/english.json | 5 ++- localization/russian.json | 5 ++- src/commands/command.cpp | 2 + src/localization/line_id.cpp | 6 +++ src/localization/line_id.hpp | 5 ++- src/modules/custom_command.hpp | 96 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 116 insertions(+), 3 deletions(-) create mode 100644 src/modules/custom_command.hpp diff --git a/localization/english.json b/localization/english.json index c3bf209..8cec9cb 100644 --- a/localization/english.json +++ b/localization/english.json @@ -32,5 +32,8 @@ "join.already_in": "{sender.alias_name}: I'm already in your chat room!", "join.rejoined": "{sender.alias_name}: I have rejoined your chat room!", "join.from_other_chat": "{sender.alias_name}: In order for the bot to join your chat, you need to send {default.prefix}join directly in %s chat.", - "join.not_allowed": "{sender.alias_name}: The bot cannot join chat rooms!%s" + "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!" } diff --git a/localization/russian.json b/localization/russian.json index 70ff1fb..3fca425 100644 --- a/localization/russian.json +++ b/localization/russian.json @@ -32,5 +32,8 @@ "join.already_in": "{sender.alias_name}: Уже в вашем чат!", "join.rejoined": "{sender.alias_name}: Перезашёл в ваш чат!", "join.from_other_chat": "{sender.alias_name}: Чтобы бот мог зайти в ваш чат, нужно написать {default.prefix}join непосредственно в чате %s", - "join.not_allowed": "{sender.alias_name}: Бот не может заходить в чужие чаты!%s" + "join.not_allowed": "{sender.alias_name}: Бот не может заходить в чужие чаты!%s", + + "custom_command.new": "{sender.alias_name}: Новая команда \"%s\" была успешно создана!", + "custom_command.delete": "{sender.alias_name}: Команда \"%s\" была удалена!" } diff --git a/src/commands/command.cpp b/src/commands/command.cpp index 44a405c..4cfe959 100644 --- a/src/commands/command.cpp +++ b/src/commands/command.cpp @@ -9,6 +9,7 @@ #include #include "../bundle.hpp" +#include "../modules/custom_command.hpp" #include "../modules/event.hpp" #include "../modules/join.hpp" #include "../modules/massping.hpp" @@ -25,6 +26,7 @@ namespace bot { this->add_command(std::make_unique()); this->add_command(std::make_unique()); this->add_command(std::make_unique()); + this->add_command(std::make_unique()); } void CommandLoader::add_command(std::unique_ptr command) { diff --git a/src/localization/line_id.cpp b/src/localization/line_id.cpp index 560ac90..f4ea6f1 100644 --- a/src/localization/line_id.cpp +++ b/src/localization/line_id.cpp @@ -74,6 +74,12 @@ namespace bot { return LineId::JoinNotAllowed; } + else if (str == "custom_command.new") { + return LineId::CustomcommandNew; + } else if (str == "custom_command.delete") { + return LineId::CustomcommandDelete; + } + else { return std::nullopt; } diff --git a/src/localization/line_id.hpp b/src/localization/line_id.hpp index da9a4a2..e9796bb 100644 --- a/src/localization/line_id.hpp +++ b/src/localization/line_id.hpp @@ -39,7 +39,10 @@ namespace bot { JoinAlreadyIn, JoinRejoined, JoinFromOtherChat, - JoinNotAllowed + JoinNotAllowed, + + CustomcommandNew, + CustomcommandDelete }; std::optional string_to_line_id(const std::string &str); diff --git a/src/modules/custom_command.hpp b/src/modules/custom_command.hpp new file mode 100644 index 0000000..0757a20 --- /dev/null +++ b/src/modules/custom_command.hpp @@ -0,0 +1,96 @@ +#pragma once + +#include +#include +#include + +#include "../bundle.hpp" +#include "../commands/command.hpp" +#include "../commands/response_error.hpp" + +namespace bot { + namespace mod { + class CustomCommand : public command::Command { + std::string get_name() const override { return "scmd"; } + + schemas::PermissionLevel get_permission_level() const override { + return schemas::PermissionLevel::MODERATOR; + } + + std::vector get_subcommand_ids() const override { + return {"new", "delete"}; + } + + std::variant, std::string> run( + const InstanceBundle &bundle, + const command::Request &request) const override { + if (!request.subcommand_id.has_value()) { + throw ResponseException( + request, bundle.localization, command::SUBCOMMAND); + } + + const std::string &subcommand_id = request.subcommand_id.value(); + + if (!request.message.has_value()) { + throw ResponseException( + request, bundle.localization, command::CommandArgument::NAME); + } + + const std::string &message = request.message.value(); + std::vector s = utils::string::split_text(message, ' '); + + std::string name = s[0]; + s.erase(s.begin()); + + pqxx::work work(request.conn); + pqxx::result cmds = work.exec( + "SELECT id FROM custom_commands WHERE name = '" + name + + "' AND channel_id = " + std::to_string(request.channel.get_id())); + + if (subcommand_id == "new") { + if (!cmds.empty()) { + throw ResponseException( + request, bundle.localization, name); + } + + if (s.empty()) { + throw ResponseException( + request, bundle.localization, + command::CommandArgument::MESSAGE); + } + + std::string m = utils::string::str(s.begin(), s.end(), ' '); + + work.exec( + "INSERT INTO custom_commands(channel_id, name, message) VALUES " + "(" + + std::to_string(request.channel.get_id()) + ", '" + name + + "', '" + m + "')"); + work.commit(); + + return bundle.localization + .get_formatted_line(request, loc::LineId::CustomcommandNew, + {name}) + .value(); + } else if (subcommand_id == "delete") { + if (cmds.empty()) { + throw ResponseException( + request, bundle.localization, name); + } + + work.exec("DELETE FROM custom_commands WHERE id = " + + std::to_string(cmds[0][0].as())); + work.commit(); + + return bundle.localization + .get_formatted_line(request, loc::LineId::CustomcommandDelete, + {name}) + .value(); + } + + throw ResponseException( + request, bundle.localization); + } + }; + } +} -- cgit v1.2.3