From 1dc04e7541221ce8ac17cc7e908264c86c2d0037 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Sun, 17 Nov 2024 23:04:23 +0100 Subject: [PATCH] Fix EventSubSubscriptionConflict --- .../stream_notifications/twitch/webhook.py | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/modules/stream_notifications/twitch/webhook.py b/src/modules/stream_notifications/twitch/webhook.py index 81c289c..b6f459e 100644 --- a/src/modules/stream_notifications/twitch/webhook.py +++ b/src/modules/stream_notifications/twitch/webhook.py @@ -5,6 +5,7 @@ from typing import NoReturn, Literal from twitchAPI.eventsub.webhook import EventSubWebhook from twitchAPI.twitch import Twitch from twitchAPI.object.eventsub import StreamOnlineEvent, ChannelUpdateEvent +from twitchAPI.type import EventSubSubscriptionConflict from core.config import config from repositories.streamers import StreamerConfigRepository, StreamerConfig @@ -45,13 +46,29 @@ class TwitchService: raise ValueError("Unknown method") except ValueError as e: raise e - except Exception as e: - if retry > 0: - await sleep(1) - await self.subscribe_with_retry(method, eventsub, streamer, retry - 1) + except EventSubSubscriptionConflict: + if method == "listen_channel_update_v2": + sub_type = "channel.update" + elif method == "listen_stream_online": + sub_type = "stream.online" else: + raise ValueError("Unknown method") + + subs = await self.twitch.get_eventsub_subscriptions( + status="enabled", + sub_type=sub_type, + user_id=str(streamer.twitch.id) + ) + + for sub in subs.data: + await self.twitch.delete_eventsub_subscription(sub.id) + except Exception as e: + if retry <= 0: raise e + await sleep(1) + await self.subscribe_with_retry(method, eventsub, streamer, retry - 1) + async def subscribe_to_streamer(self, eventsub: EventSubWebhook, streamer: StreamerConfig): logger.info(f"Subscribe to events for {streamer.twitch.name}") await gather(