From 8c9f4843fc904f53197d6008c29df7ba5b1353e9 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Thu, 9 May 2024 00:04:43 +0500 Subject: feat: new command - !timer --- src/modules/timer.hpp | 112 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 112 insertions(+) create mode 100644 src/modules/timer.hpp (limited to 'src/modules') 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 +#include +#include + +#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 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 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( + request, bundle.localization, name); + } + + if (s.empty()) { + throw ResponseException( + request, bundle.localization, + command::CommandArgument::INTERVAL); + } + + int interval_s; + + try { + interval_s = std::stoi(s[0]); + } catch (std::exception e) { + throw ResponseException( + request, bundle.localization, s[0]); + } + + s.erase(s.begin()); + + 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 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( + request, bundle.localization, name); + } + + work.exec("DELETE FROM timers WHERE id = " + + std::to_string(timers[0][0].as())); + work.commit(); + + return bundle.localization + .get_formatted_line(request, loc::LineId::TimerDelete, {name}) + .value(); + } + + throw ResponseException( + request, bundle.localization); + } + }; + } +} -- cgit v1.2.3