clean subs before start

This commit is contained in:
2025-03-18 18:10:05 +01:00
parent dac0ddb884
commit b5b3397bff

View File

@@ -56,6 +56,30 @@ class TwitchService:
MessageEvent.from_twitch_event(event) 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( async def subscribe_with_retry(
self, self,
method: Literal["listen_channel_update_v2"] method: Literal["listen_channel_update_v2"]
@@ -66,6 +90,7 @@ class TwitchService:
streamer: StreamerConfig, streamer: StreamerConfig,
retry: int = 10 retry: int = 10
): ):
await self._clean_subs(method, streamer)
try: try:
match method: match method:
@@ -92,29 +117,6 @@ class TwitchService:
if retry <= 0: if retry <= 0:
raise e 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 sleep(1)
await self.subscribe_with_retry(method, eventsub, streamer, retry - 1) await self.subscribe_with_retry(method, eventsub, streamer, retry - 1)