summaryrefslogtreecommitdiff
path: root/bot/src/timer.cpp
diff options
context:
space:
mode:
authorilotterytea <iltsu@alright.party>2024-05-18 14:48:12 +0500
committerilotterytea <iltsu@alright.party>2024-05-18 14:48:12 +0500
commitd1793df1eda463b10107d41785ad1d7f055ed476 (patch)
treefd3e41c3b4a05924748ae4b762e1ae55a0bc815c /bot/src/timer.cpp
parentd7a2de17e9b7931f68b5b4079b1c36866a19d343 (diff)
upd: moved the bot part to a relative subfolder
Diffstat (limited to 'bot/src/timer.cpp')
-rw-r--r--bot/src/timer.cpp70
1 files changed, 70 insertions, 0 deletions
diff --git a/bot/src/timer.cpp b/bot/src/timer.cpp
new file mode 100644
index 0000000..055dde0
--- /dev/null
+++ b/bot/src/timer.cpp
@@ -0,0 +1,70 @@
+#include "timer.hpp"
+
+#include <chrono>
+#include <pqxx/pqxx>
+#include <string>
+#include <thread>
+
+#include "config.hpp"
+#include "irc/client.hpp"
+#include "utils/chrono.hpp"
+
+namespace bot {
+ void create_timer_thread(irc::Client *irc_client,
+ Configuration *configuration) {
+ while (true) {
+ pqxx::connection conn(GET_DATABASE_CONNECTION_URL_POINTER(configuration));
+ pqxx::work *work = new pqxx::work(conn);
+
+ pqxx::result timers = work->exec(
+ "SELECT id, interval_sec, message, channel_id, last_executed_at FROM "
+ "timers");
+
+ for (const auto &timer : timers) {
+ int id = timer[0].as<int>();
+ int interval_sec = timer[1].as<int>();
+ std::string message = timer[2].as<std::string>();
+ int channel_id = timer[3].as<int>();
+
+ // it could be done in sql query
+ std::chrono::system_clock::time_point last_executed_at =
+ utils::chrono::string_to_time_point(timer[4].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_executed_at);
+
+ if (difference.count() > interval_sec) {
+ pqxx::result channels = work->exec(
+ "SELECT alias_name, opted_out_at FROM channels WHERE id = " +
+ std::to_string(channel_id));
+
+ if (!channels.empty() && channels[0][1].is_null()) {
+ std::string alias_name = channels[0][0].as<std::string>();
+
+ irc_client->say(alias_name, message);
+ }
+
+ work->exec(
+ "UPDATE timers SET last_executed_at = timezone('utc', now()) "
+ "WHERE "
+ "id = " +
+ std::to_string(id));
+
+ work->commit();
+
+ delete work;
+ work = new pqxx::work(conn);
+ }
+ }
+
+ delete work;
+ conn.close();
+
+ std::this_thread::sleep_for(std::chrono::seconds(1));
+ }
+ }
+}