diff --git a/plugins/twitch/chat-connection.cpp b/plugins/twitch/chat-connection.cpp index 6c1c4e6f..0d34a96b 100644 --- a/plugins/twitch/chat-connection.cpp +++ b/plugins/twitch/chat-connection.cpp @@ -3,6 +3,7 @@ #include "twitch-helpers.hpp" #include +#include #undef DispatchMessage @@ -339,6 +340,24 @@ static constexpr std::string_view defaultURL = std::map> TwitchChatConnection::_chatMap = {}; +void TwitchChatConnection::DisconnectAll() +{ + for (auto &[key, weakConn] : _chatMap) { + auto conn = weakConn.lock(); + if (conn) { + conn->Disconnect(); + } + } +} + +static bool setupChatConnectionSupport() +{ + AddStopStep(TwitchChatConnection::DisconnectAll); + return true; +} + +static bool _chatSetup = setupChatConnectionSupport(); + TwitchChatConnection::TwitchChatConnection(const TwitchToken &token, const TwitchChannel &channel) : QObject(nullptr), diff --git a/plugins/twitch/chat-connection.hpp b/plugins/twitch/chat-connection.hpp index 3dc96338..08f44d5c 100644 --- a/plugins/twitch/chat-connection.hpp +++ b/plugins/twitch/chat-connection.hpp @@ -84,6 +84,7 @@ public: static std::shared_ptr GetChatConnection(const TwitchToken &token, const TwitchChannel &channel); + static void DisconnectAll(); [[nodiscard]] ChatMessageBuffer RegisterForMessages(); [[nodiscard]] ChatMessageBuffer RegisterForWhispers(); void SendChatMessage(const std::string &message); diff --git a/plugins/twitch/event-sub.cpp b/plugins/twitch/event-sub.cpp index e83f9f61..45612d45 100644 --- a/plugins/twitch/event-sub.cpp +++ b/plugins/twitch/event-sub.cpp @@ -3,6 +3,7 @@ #include "twitch-helpers.hpp" #include +#include #ifdef VERIFY_TIMESTAMPS #include "date/tz.h" @@ -71,6 +72,22 @@ void EventSub::UnregisterInstance() _instances.erase(it, _instances.end()); } +void EventSub::DisconnectAll() +{ + std::lock_guard lock(_instancesMtx); + for (auto *instance : _instances) { + instance->Disconnect(); + } +} + +static bool setupEventSubSupport() +{ + AddStopStep(EventSub::DisconnectAll); + return true; +} + +static bool _eventSubSetup = setupEventSubSupport(); + void EventSub::ConnectThread() { _client->reset(); diff --git a/plugins/twitch/event-sub.hpp b/plugins/twitch/event-sub.hpp index 65f12c1f..301327c2 100644 --- a/plugins/twitch/event-sub.hpp +++ b/plugins/twitch/event-sub.hpp @@ -53,6 +53,7 @@ public: void Connect(); void Disconnect(); + static void DisconnectAll(); [[nodiscard]] EventSubMessageBuffer RegisterForEvents(); bool SubscriptionIsActive(const std::string &id); static std::string AddEventSubscription(std::shared_ptr, diff --git a/plugins/twitch/macro-condition-twitch.cpp b/plugins/twitch/macro-condition-twitch.cpp index fed5d2ba..4fcf3c2c 100644 --- a/plugins/twitch/macro-condition-twitch.cpp +++ b/plugins/twitch/macro-condition-twitch.cpp @@ -547,16 +547,18 @@ bool MacroConditionTwitch::CheckChatMessageRemove(TwitchToken &token) bool MacroConditionTwitch::ChatConnectionIsSetup(TwitchToken &token) { + if (_chatConnection) { + _chatConnection->ConnectToChat(); + return true; + } + + _chatConnection = + TwitchChatConnection::GetChatConnection(token, _channel); if (!_chatConnection) { - _chatConnection = TwitchChatConnection::GetChatConnection( - token, _channel); - if (!_chatConnection) { - return false; - } - _chatBuffer = _chatConnection->RegisterForMessages(); return false; } - return true; + _chatBuffer = _chatConnection->RegisterForMessages(); + return false; } bool MacroConditionTwitch::HandleChatEvents(