diff options
| author | ilotterytea <iltsu@alright.party> | 2024-05-08 22:33:16 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2024-05-08 22:33:16 +0500 |
| commit | 60830b2cd486d49e9955b1ad0e7f7a5205829949 (patch) | |
| tree | 7c656f6fe442f73fd581a2e6801e147a8d4e5e5d | |
| parent | 6034e01a6c0d62546f10247aa481304699ed4155 (diff) | |
upd: update channel data on startup + join channels with no opt out timestamp
| -rw-r--r-- | src/main.cpp | 46 |
1 files changed, 38 insertions, 8 deletions
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 <optional> #include <pqxx/pqxx> #include <string> +#include <vector> #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<int> ids; for (const auto &row : rows) { - auto name = row[0].as<std::string>(); - client.join(name); + ids.push_back(row[0].as<int>()); } - 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<std::string>(); + + if (name != helix_channel.login) { + work->exec("UPDATE channels SET alias_name = '" + helix_channel.login + + "' WHERE id = " + std::to_string(channel[0][0].as<int>())); + 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); |
