From b5b3397bff1e12853cc590766fde258487373fd7 Mon Sep 17 00:00:00 2001 From: Bulat Kurbanov Date: Tue, 18 Mar 2025 18:10:05 +0100 Subject: [PATCH] clean subs before start --- .../stream_notifications/twitch/webhook.py | 48 ++++++++++--------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/src/modules/stream_notifications/twitch/webhook.py b/src/modules/stream_notifications/twitch/webhook.py index b407860..4ae2bf0 100644 --- a/src/modules/stream_notifications/twitch/webhook.py +++ b/src/modules/stream_notifications/twitch/webhook.py @@ -56,6 +56,30 @@ class TwitchService: MessageEvent.from_twitch_event(event) ) + async def _clean_subs(self, method: str, streamer: StreamerConfig): + match method: + case "listen_channel_update_v2": + sub_type = "channel.update" + case "listen_stream_online": + sub_type = "stream.online" + case "listen_channel_chat_message": + sub_type = "channel.chat.message" + case "listen_channel_points_custom_reward_redemption_add": + sub_type = "channel.channel_points_custom_reward_redemption.add" + case _: + raise ValueError("Unknown method") + + subs = await self.twitch.get_eventsub_subscriptions( + user_id=str(streamer.twitch.id) + ) + + for sub in subs.data: + if sub.type == sub_type: + try: + await self.twitch.delete_eventsub_subscription(sub.id) + except Exception as e: + logger.error(f"Failed to delete subscription {sub.id}", exc_info=e) + async def subscribe_with_retry( self, method: Literal["listen_channel_update_v2"] @@ -66,6 +90,7 @@ class TwitchService: streamer: StreamerConfig, retry: int = 10 ): + await self._clean_subs(method, streamer) try: match method: @@ -92,29 +117,6 @@ class TwitchService: if retry <= 0: raise e - match method: - case "listen_channel_update_v2": - sub_type = "channel.update" - case "listen_stream_online": - sub_type = "stream.online" - case "listen_channel_chat_message": - sub_type = "channel.chat.message" - case "listen_channel_points_custom_reward_redemption_add": - sub_type = "channel.channel_points_custom_reward_redemption.add" - case _: - raise ValueError("Unknown method") - - subs = await self.twitch.get_eventsub_subscriptions( - user_id=str(streamer.twitch.id) - ) - - for sub in subs.data: - if sub.type == sub_type: - try: - await self.twitch.delete_eventsub_subscription(sub.id) - except Exception as e: - logger.error(f"Failed to delete subscription {sub.id}", exc_info=e) - await sleep(1) await self.subscribe_with_retry(method, eventsub, streamer, retry - 1)