From 79be89ad0491bfdd110b2c612e21a0f28c29fa87 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Wed, 2 Jul 2025 03:31:54 +0500 Subject: feat: MARIADB SUPPORT!!!! --- bot/src/stream.cpp | 60 ++++++++++++++++++++++++------------------------------ 1 file changed, 27 insertions(+), 33 deletions(-) (limited to 'bot/src/stream.cpp') diff --git a/bot/src/stream.cpp b/bot/src/stream.cpp index 107b883..ec10b71 100644 --- a/bot/src/stream.cpp +++ b/bot/src/stream.cpp @@ -2,14 +2,14 @@ #include #include -#include +#include #include #include #include #include "api/kick.hpp" #include "api/twitch/schemas/stream.hpp" -#include "config.hpp" +#include "database.hpp" #include "logger.hpp" #include "nlohmann/json.hpp" #include "schemas/stream.hpp" @@ -209,48 +209,44 @@ namespace bot::stream { void StreamListenerClient::handler(const schemas::EventType &type, const api::twitch::schemas::Stream &stream, const StreamerData &data) { - pqxx::connection conn(GET_DATABASE_CONNECTION_URL(this->configuration)); - pqxx::work work(conn); + std::unique_ptr conn = + db::create_connection(this->configuration); - pqxx::result events = work.exec_params( - "SELECT e.id, e.message, array_to_json(e.flags) AS " - "flags, c.alias_name AS channel_aname, c.alias_id AS channel_aid FROM " + db::DatabaseRows events = conn->exec( + "SELECT e.id, e.message, is_massping, c.alias_name AS channel_aname, " + "c.alias_id AS channel_aid FROM " "events e " "INNER JOIN channels c ON c.id = e.channel_id " "WHERE e.event_type = $1 AND e.name = $2", - pqxx::params{static_cast(type), stream.get_user_id()}); + {std::to_string(static_cast(type)), + std::to_string(stream.get_user_id())}); for (const auto &event : events) { std::vector names; - bool massping_enabled = false; - if (!event[2].is_null()) { - nlohmann::json j = nlohmann::json::parse(event[2].as()); - massping_enabled = std::any_of(j.begin(), j.end(), [](const auto &x) { - return static_cast(x) == static_cast(schemas::MASSPING); - }); - } + bool massping_enabled = std::stoi(event.at("is_massping")); if (massping_enabled) { auto chatters = this->helix_client.get_chatters( - event[4].as(), this->irc_client.get_bot_id()); + std::stoi(event.at("channel_aid")), this->irc_client.get_bot_id()); std::for_each(chatters.begin(), chatters.end(), [&names](const auto &x) { names.push_back(x.login); }); } else { - pqxx::result subs = work.exec_params( + db::DatabaseRows subs = conn->exec( "SELECT u.alias_name FROM users u " "INNER JOIN events e ON e.id = $1 " "INNER JOIN event_subscriptions es ON es.event_id = e.id " "WHERE u.id = es.user_id", - pqxx::params{event[0].as()}); + {event.at("id")}); - std::for_each(subs.begin(), subs.end(), [&names](const pqxx::row &x) { - names.push_back(x[0].as()); - }); + std::for_each(subs.begin(), subs.end(), + [&names](const db::DatabaseRow &x) { + names.push_back(x.at("alias_name")); + }); } - std::string base = "⚡ " + event[1].as(); + std::string base = "⚡ " + event.at("message"); if (!names.empty()) { base.append(" · "); } @@ -279,24 +275,23 @@ namespace bot::stream { utils::string::separate_by_length(base, names, "@", " ", 500); for (const auto &msg : msgs) { - this->irc_client.say(event[3].as(), base + msg); + this->irc_client.say(event.at("channel_aname"), base + msg); } } - work.commit(); - conn.close(); + conn->close(); } void StreamListenerClient::update_channel_ids() { - pqxx::connection conn(GET_DATABASE_CONNECTION_URL(this->configuration)); - pqxx::work work(conn); + std::unique_ptr conn = + db::create_connection(this->configuration); - pqxx::result ids = - work.exec("SELECT name, event_type FROM events WHERE event_type < 10"); + db::DatabaseRows ids = + conn->exec("SELECT name, event_type FROM events WHERE event_type < 10"); for (const auto &row : ids) { - int id = row[0].as(); - int event_type = row[1].as(); + int id = std::stoi(row.at("name")); + int event_type = std::stoi(row.at("event_type")); StreamerType type = (event_type >= schemas::EventType::KICK_LIVE && event_type <= schemas::EventType::KICK_GAME) @@ -313,7 +308,6 @@ namespace bot::stream { listen_channel(id, type); } - work.commit(); - conn.close(); + conn->close(); } } -- cgit v1.2.3