Fix EventSubSubscriptionConflict

This commit is contained in:
2024-11-17 23:04:23 +01:00
parent 55212752f4
commit 1dc04e7541

View File

@@ -5,6 +5,7 @@ from typing import NoReturn, Literal
from twitchAPI.eventsub.webhook import EventSubWebhook from twitchAPI.eventsub.webhook import EventSubWebhook
from twitchAPI.twitch import Twitch from twitchAPI.twitch import Twitch
from twitchAPI.object.eventsub import StreamOnlineEvent, ChannelUpdateEvent from twitchAPI.object.eventsub import StreamOnlineEvent, ChannelUpdateEvent
from twitchAPI.type import EventSubSubscriptionConflict
from core.config import config from core.config import config
from repositories.streamers import StreamerConfigRepository, StreamerConfig from repositories.streamers import StreamerConfigRepository, StreamerConfig
@@ -45,13 +46,29 @@ class TwitchService:
raise ValueError("Unknown method") raise ValueError("Unknown method")
except ValueError as e: except ValueError as e:
raise e raise e
except Exception as e: except EventSubSubscriptionConflict:
if retry > 0: if method == "listen_channel_update_v2":
await sleep(1) sub_type = "channel.update"
await self.subscribe_with_retry(method, eventsub, streamer, retry - 1) elif method == "listen_stream_online":
sub_type = "stream.online"
else: 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 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): async def subscribe_to_streamer(self, eventsub: EventSubWebhook, streamer: StreamerConfig):
logger.info(f"Subscribe to events for {streamer.twitch.name}") logger.info(f"Subscribe to events for {streamer.twitch.name}")
await gather( await gather(