From 60830b2cd486d49e9955b1ad0e7f7a5205829949 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Wed, 8 May 2024 22:33:16 +0500 Subject: upd: update channel data on startup + join channels with no opt out timestamp --- src/main.cpp | 46 ++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 38 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/main.cpp b/src/main.cpp index 4443497..700c01d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -2,6 +2,7 @@ #include #include #include +#include #include "api/twitch/helix_client.hpp" #include "bundle.hpp" @@ -44,24 +45,53 @@ int main(int argc, char *argv[]) { cfg.twitch_credentials.token); bot::command::CommandLoader command_loader; bot::loc::Localization localization("localization"); + bot::api::twitch::HelixClient helix_client(cfg.twitch_credentials.token, + cfg.twitch_credentials.client_id); client.join(client.get_bot_username()); pqxx::connection conn(GET_DATABASE_CONNECTION_URL(cfg)); - pqxx::work work(conn); + pqxx::work *work = new pqxx::work(conn); - auto rows = work.exec("SELECT alias_name FROM channels"); + pqxx::result rows = work->exec( + "SELECT alias_id FROM channels WHERE opted_out_at is null AND alias_id " + "!= " + + std::to_string(client.get_bot_id())); + + std::vector ids; for (const auto &row : rows) { - auto name = row[0].as(); - client.join(name); + ids.push_back(row[0].as()); } - work.commit(); - conn.close(); + auto helix_channels = helix_client.get_users(ids); - bot::api::twitch::HelixClient helix_client(cfg.twitch_credentials.token, - cfg.twitch_credentials.client_id); + // it could be optimized + for (const auto &helix_channel : helix_channels) { + auto channel = + work->exec("SELECT id, alias_name FROM channels WHERE alias_id = " + + std::to_string(helix_channel.id)); + + if (!channel.empty()) { + std::string name = channel[0][1].as(); + + if (name != helix_channel.login) { + work->exec("UPDATE channels SET alias_name = '" + helix_channel.login + + "' WHERE id = " + std::to_string(channel[0][0].as())); + work->commit(); + + delete work; + work = new pqxx::work(conn); + } + + client.join(helix_channel.login); + } + } + + work->commit(); + delete work; + + conn.close(); bot::stream::StreamListenerClient stream_listener_client(helix_client, client, cfg); -- cgit v1.2.3