From 8b86e9a8f071a1af5ce1531a9ebd2b2140e73ff7 Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Mon, 21 Jul 2025 16:34:23 +0500 Subject: feat: a function for getting events --- bot/src/schemas/event.hpp | 13 +++++++++++ bot/src/utils/events.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++ bot/src/utils/events.hpp | 15 +++++++++++++ 3 files changed, 84 insertions(+) create mode 100644 bot/src/schemas/event.hpp create mode 100644 bot/src/utils/events.cpp create mode 100644 bot/src/utils/events.hpp (limited to 'bot') diff --git a/bot/src/schemas/event.hpp b/bot/src/schemas/event.hpp new file mode 100644 index 0000000..a91aef3 --- /dev/null +++ b/bot/src/schemas/event.hpp @@ -0,0 +1,13 @@ +#pragma once + +#include +#include + +namespace bot::schemas { + struct Event { + int id, alias_id; + std::string message, channel_alias_name; + bool is_massping; + std::vector subs; + }; +} \ No newline at end of file diff --git a/bot/src/utils/events.cpp b/bot/src/utils/events.cpp new file mode 100644 index 0000000..a6e0834 --- /dev/null +++ b/bot/src/utils/events.cpp @@ -0,0 +1,56 @@ +#include "utils/events.hpp" + +#include +#include +#include + +namespace bot::utils { + std::vector get_events(std::unique_ptr conn, + api::twitch::HelixClient &api_client, + int moderator_id, int type, + const std::string &name) { + std::vector events; + + db::DatabaseRows rows = 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", + {std::to_string(type), name}); + + for (const db::DatabaseRow &row : rows) { + schemas::Event event; + + event.id = std::stoi(row.at("id")); + event.alias_id = std::stoi(row.at("channel_aid")); + event.is_massping = std::stoi(row.at("is_massping")); + event.message = row.at("message"); + event.channel_alias_name = row.at("channel_aname"); + + if (event.is_massping) { + auto chatters = api_client.get_chatters(event.alias_id, moderator_id); + + std::for_each( + chatters.begin(), chatters.end(), + [&event](const auto &x) { event.subs.push_back(x.login); }); + } else { + 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", + {std::to_string(event.id)}); + + std::for_each(subs.begin(), subs.end(), + [&event](const db::DatabaseRow &x) { + event.subs.push_back(x.at("alias_name")); + }); + } + + events.push_back(event); + } + + return events; + } +} \ No newline at end of file diff --git a/bot/src/utils/events.hpp b/bot/src/utils/events.hpp new file mode 100644 index 0000000..e806c94 --- /dev/null +++ b/bot/src/utils/events.hpp @@ -0,0 +1,15 @@ +#pragma once + +#include +#include + +#include "api/twitch/helix_client.hpp" +#include "database.hpp" +#include "schemas/event.hpp" + +namespace bot::utils { + std::vector get_events(std::unique_ptr conn, + api::twitch::HelixClient &api_client, + int moderator_id, int type, + const std::string &name); +}; \ No newline at end of file -- cgit v1.2.3