From 854e052b3a75bd4f77dd404341ea5c1070a2009c Mon Sep 17 00:00:00 2001 From: ilotterytea Date: Sun, 21 Apr 2024 16:33:23 +0500 Subject: feat: join() method --- src/irc/client.cpp | 18 ++++++++++++++++++ src/irc/client.hpp | 3 +++ 2 files changed, 21 insertions(+) (limited to 'src/irc') diff --git a/src/irc/client.cpp b/src/irc/client.cpp index 2604186..08a3ff3 100644 --- a/src/irc/client.cpp +++ b/src/irc/client.cpp @@ -74,6 +74,11 @@ void Client::run() { case ix::WebSocketMessageType::Close: { std::cout << "Twitch IRC Connection closed!\n"; this->is_connected = false; + + for (const auto &x : this->joined_channels) { + this->raw("JOIN #" + x); + } + break; } default: { @@ -85,6 +90,19 @@ void Client::run() { this->websocket.run(); } +bool Client::join(const std::string &channel_login) { + auto already_joined = + std::any_of(this->joined_channels.begin(), this->joined_channels.end(), + [&](const auto &x) { return x == channel_login; }); + + if (!already_joined) { + this->raw("JOIN #" + channel_login); + this->joined_channels.push_back(channel_login); + } + + return !already_joined; +} + void Client::raw(const std::string &raw_message) { std::string msg = raw_message + "\r\n"; if (this->is_connected) { diff --git a/src/irc/client.hpp b/src/irc/client.hpp index 0194750..2598550 100644 --- a/src/irc/client.hpp +++ b/src/irc/client.hpp @@ -16,6 +16,7 @@ namespace bot { void run(); + bool join(const std::string &channel_login); void raw(const std::string &raw_message); template @@ -43,6 +44,8 @@ namespace bot { bool is_connected = false; std::vector pool; + std::vector joined_channels; + // Message handlers typename MessageHandler::fn onPrivmsg; }; -- cgit v1.2.3