diff options
| author | ilotterytea <iltsu@alright.party> | 2024-05-01 19:56:11 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-05-01 19:56:11 +0500 |
| commit | 463b93572f11887752dff6a836899a583c39682c (patch) | |
| tree | 8fcc3738e06aea892b236884f2e71c01d20d459d | |
| parent | 34fa75f64c8cdfa72fe9cd9cf04daca7c37446ec (diff) | |
feat: check if the command has been run recently
| -rw-r--r-- | src/commands/command.cpp | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/commands/command.cpp b/src/commands/command.cpp index a5ead40..8c097a9 100644 --- a/src/commands/command.cpp +++ b/src/commands/command.cpp @@ -1,11 +1,16 @@ #include "command.hpp" #include <algorithm> +#include <chrono> +#include <ctime> #include <memory> #include <optional> +#include <pqxx/pqxx> +#include <string> #include "../bundle.hpp" #include "../modules/ping.hpp" +#include "../utils/chrono.hpp" #include "request.hpp" namespace bot { @@ -34,6 +39,51 @@ namespace bot { return std::nullopt; } + pqxx::work work(request.conn); + + pqxx::result action_query = work.exec( + "SELECT sent_at FROM actions WHERE user_id = " + + std::to_string(request.user.get_id()) + + " AND channel_id = " + std::to_string(request.channel.get_id()) + + " AND command = '" + request.command_id + "' ORDER BY sent_at DESC"); + + if (!action_query.empty()) { + auto last_sent_at = utils::chrono::string_to_time_point( + action_query[0][0].as<std::string>()); + + auto now = std::chrono::system_clock::now(); + auto now_time_it = std::chrono::system_clock::to_time_t(now); + auto now_tm = std::gmtime(&now_time_it); + now = std::chrono::system_clock::from_time_t(std::mktime(now_tm)); + + auto difference = std::chrono::duration_cast<std::chrono::seconds>( + now - last_sent_at); + + if (difference.count() < command->get()->get_delay_seconds()) { + return std::nullopt; + } + } + + std::string arguments; + + if (request.subcommand_id.has_value()) { + arguments += request.subcommand_id.value() + " "; + } + + if (request.message.has_value()) { + arguments += request.message.value(); + } + + work.exec( + "INSERT INTO actions(user_id, channel_id, command, arguments, " + "full_message) VALUES (" + + std::to_string(request.user.get_id()) + ", " + + std::to_string(request.channel.get_id()) + ", '" + + request.command_id + "', '" + arguments + "', '" + + request.irc_message.message + "')"); + + work.commit(); + return (*command)->run(bundle, request); } } |
