diff options
| -rw-r--r-- | localization/english.json | 11 | ||||
| -rw-r--r-- | localization/russian.json | 11 | ||||
| -rw-r--r-- | src/commands/command.cpp | 2 | ||||
| -rw-r--r-- | src/localization/line_id.cpp | 18 | ||||
| -rw-r--r-- | src/localization/line_id.hpp | 11 | ||||
| -rw-r--r-- | src/modules/join.hpp | 93 |
6 files changed, 143 insertions, 3 deletions
diff --git a/localization/english.json b/localization/english.json index 29b9858..c3bf209 100644 --- a/localization/english.json +++ b/localization/english.json @@ -1,4 +1,6 @@ { + "msg.owner": "Contact @%s", + "argument.subcommand": "Subcommand", "argument.message": "Message", "argument.interval": "Interval", @@ -23,5 +25,12 @@ "event.off": "{sender.alias_name}: The event \"%s\" has been deleted!", "notify.sub": "{sender.alias_name}: You have been successfully subscribed to the \"%s\" event!", - "notify.unsub": "{sender.alias_name}: You have been unsubscribed from the \"%s\" event!" + "notify.unsub": "{sender.alias_name}: You have been unsubscribed from the \"%s\" event!", + + "join.response": "{sender.alias_name}: Successfully joined your chat room!", + "join.response_in_chat": "Hi, I'm %s and I'll be serving this chat. Send {default.prefix}help to learn about the commands!", + "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" } diff --git a/localization/russian.json b/localization/russian.json index a29068c..70ff1fb 100644 --- a/localization/russian.json +++ b/localization/russian.json @@ -1,4 +1,6 @@ { + "msg.owner": "Свяжитесь с @%s", + "argument.subcommand": "Подкоманда", "argument.message": "Сообщение", "argument.interval": "Интервал", @@ -23,5 +25,12 @@ "event.off": "{sender.alias_name}: Событие \"%s\" удалено!", "notify.sub": "{sender.alias_name}: Вы успешно подписались на событие \"%s\"!", - "notify.unsub": "{sender.alias_name}: Вы отписались от события \"%s\"!" + "notify.unsub": "{sender.alias_name}: Вы отписались от события \"%s\"!", + + "join.response": "{sender.alias_name}: Успешно зашёл в ваш чат!", + "join.response_in_chat": "Привет! Я - %s и я буду обслуживать этот чат. Напишите {default.prefix}help, чтобы узнать о командах!", + "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" } diff --git a/src/commands/command.cpp b/src/commands/command.cpp index dfb1fbf..44a405c 100644 --- a/src/commands/command.cpp +++ b/src/commands/command.cpp @@ -10,6 +10,7 @@ #include "../bundle.hpp" #include "../modules/event.hpp" +#include "../modules/join.hpp" #include "../modules/massping.hpp" #include "../modules/notify.hpp" #include "../modules/ping.hpp" @@ -23,6 +24,7 @@ namespace bot { this->add_command(std::make_unique<mod::Massping>()); this->add_command(std::make_unique<mod::Event>()); this->add_command(std::make_unique<mod::Notify>()); + this->add_command(std::make_unique<mod::Join>()); } void CommandLoader::add_command(std::unique_ptr<Command> command) { diff --git a/src/localization/line_id.cpp b/src/localization/line_id.cpp index 2eca697..560ac90 100644 --- a/src/localization/line_id.cpp +++ b/src/localization/line_id.cpp @@ -10,6 +10,10 @@ namespace bot { return LineId::PingResponse; } + else if (str == "msg.owner") { + return LineId::MsgOwner; + } + else if (str == "argument.subcommand") { return LineId::ArgumentSubcommand; } else if (str == "argument.message") { @@ -56,6 +60,20 @@ namespace bot { return LineId::NotifyUnsub; } + else if (str == "join.response") { + return LineId::JoinResponse; + } else if (str == "join.response_in_chat") { + return LineId::JoinResponseInChat; + } else if (str == "join.already_in") { + return LineId::JoinAlreadyIn; + } else if (str == "join.rejoined") { + return LineId::JoinRejoined; + } else if (str == "join.from_other_chat") { + return LineId::JoinFromOtherChat; + } else if (str == "join.not_allowed") { + return LineId::JoinNotAllowed; + } + else { return std::nullopt; } diff --git a/src/localization/line_id.hpp b/src/localization/line_id.hpp index 264be15..da9a4a2 100644 --- a/src/localization/line_id.hpp +++ b/src/localization/line_id.hpp @@ -6,6 +6,8 @@ namespace bot { namespace loc { enum LineId { + MsgOwner, + ArgumentSubcommand, ArgumentMessage, ArgumentInterval, @@ -30,7 +32,14 @@ namespace bot { EventOff, NotifySub, - NotifyUnsub + NotifyUnsub, + + JoinResponse, + JoinResponseInChat, + JoinAlreadyIn, + JoinRejoined, + JoinFromOtherChat, + JoinNotAllowed }; std::optional<LineId> string_to_line_id(const std::string &str); diff --git a/src/modules/join.hpp b/src/modules/join.hpp new file mode 100644 index 0000000..5718324 --- /dev/null +++ b/src/modules/join.hpp @@ -0,0 +1,93 @@ +#pragma once + +#include <pqxx/pqxx> +#include <string> +#include <variant> +#include <vector> + +#include "../bundle.hpp" +#include "../commands/command.hpp" +#include "../schemas/channel.hpp" + +namespace bot { + namespace mod { + class Join : public command::Command { + std::string get_name() const override { return "join"; } + + std::variant<std::vector<std::string>, std::string> run( + const InstanceBundle &bundle, + const command::Request &request) const override { + if (!bundle.configuration.commands.join_allowed) { + std::string owner = ""; + + if (bundle.configuration.owner.name.has_value()) { + owner = " " + bundle.localization + .get_formatted_line( + request, loc::LineId::MsgOwner, + {*bundle.configuration.owner.name}) + .value(); + } + + return bundle.localization + .get_formatted_line(request, loc::LineId::JoinNotAllowed, + {owner}) + .value(); + } + + if (!bundle.configuration.commands.join_allow_from_other_chats && + request.channel.get_alias_name() != + bundle.irc_client.get_bot_username()) { + return bundle.localization + .get_formatted_line(request, loc::LineId::JoinFromOtherChat, + {bundle.irc_client.get_bot_username()}) + .value(); + } + + pqxx::work work(request.conn); + + pqxx::result channels = + work.exec("SELECT * FROM channels WHERE alias_id = " + + std::to_string(request.user.get_alias_id())); + + if (!channels.empty()) { + schemas::Channel channel(channels[0]); + + if (channel.get_opted_out_at().has_value()) { + work.exec("UPDATE channels SET opted_out_at = null WHERE id = " + + std::to_string(channel.get_id())); + work.commit(); + + bundle.irc_client.join(channel.get_alias_name()); + + return bundle.localization + .get_formatted_line(request, loc::LineId::JoinRejoined, {}) + .value(); + } + + return bundle.localization + .get_formatted_line(request, loc::LineId::JoinAlreadyIn, {}) + .value(); + } + + return "asd"; + + work.exec("INSERT INTO channels(alias_id, alias_name) VALUES (" + + std::to_string(request.user.get_alias_id()) + ", '" + + request.user.get_alias_name() + "')"); + work.commit(); + + bundle.irc_client.join(request.user.get_alias_name()); + bundle.irc_client.say( + request.user.get_alias_name(), + bundle.localization + .get_formatted_line(request, loc::LineId::JoinResponseInChat, + {}) + .value()); + + return bundle.localization + .get_formatted_line(request, loc::LineId::JoinResponse, {}) + .value(); + } + }; + } +} |
