Skip to content

Commit

Permalink
improve co-stream error handling, backwards-compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
Felk committed Feb 29, 2024
1 parent b2d9522 commit 4607c91
Showing 1 changed file with 26 additions and 7 deletions.
33 changes: 26 additions & 7 deletions TPP.Core/Chat/TwitchEventSubChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using TPP.Twitch.EventSub;
using TPP.Twitch.EventSub.Notifications;
using TwitchLib.Api;
using TwitchLib.Api.Core.Exceptions;
using TwitchLib.Api.Helix.Models.Streams.GetStreams;
using TwitchLib.Api.Helix.Models.Users.GetUsers;
using static TPP.Core.EventUtils;
Expand Down Expand Up @@ -98,10 +99,20 @@ await session.SubscribeWithTwitchLibApi<ChannelChatSettingsUpdate>(
{
foreach (string channelId in await _coStreamChannelsRepo.GetJoinedChannels())
{
var response = await session.SubscribeWithTwitchLibApi<ChannelChatMessage>(
(await _twitchApiProvider.Get()).Helix.EventSub,
new ChannelChatMessage.Condition(BroadcasterUserId: channelId, UserId: _userId).AsDict());
_coStreamEventSubSubscriptions[channelId] = response.Subscriptions[0].Id;
try
{
var response = await session.SubscribeWithTwitchLibApi<ChannelChatMessage>(
(await _twitchApiProvider.Get()).Helix.EventSub,
new ChannelChatMessage.Condition(BroadcasterUserId: channelId, UserId: _userId).AsDict());
_coStreamEventSubSubscriptions[channelId] = response.Subscriptions[0].Id;
}
catch (HttpResponseException ex)
{
_logger.LogError(ex,
"Failed to join co-stream channel ID {ChannelId}, removing channel. Maybe the channel was " +
"deleted, or the database contained old data (channel names instead of IDs)", channelId);
await _coStreamChannelsRepo.Remove(channelId);
}
}
}
}
Expand Down Expand Up @@ -145,9 +156,17 @@ public async Task<LeaveResult> Leave(string userId)
{
if (!await _coStreamChannelsRepo.IsJoined(userId))
return LeaveResult.NotJoined;
await (await _twitchApiProvider.Get()).Helix.Chat.SendChatMessage(userId, _userId, "Leaving channel, goodbye!");
await (await _twitchApiProvider.Get()).Helix.EventSub.DeleteEventSubSubscriptionAsync(
_coStreamEventSubSubscriptions[userId]);
TwitchAPI api = await _twitchApiProvider.Get();
try
{
await api.Helix.Chat.SendChatMessage(userId, _userId, "Leaving channel, goodbye!");
}
catch (HttpResponseException ex)
{
_logger.LogError(ex, "Failed sending goodbye message to {ChannelID} after leaving, ignoring", userId);
}
if (_coStreamEventSubSubscriptions.TryGetValue(userId, out string? subscriptionId))
await api.Helix.EventSub.DeleteEventSubSubscriptionAsync(subscriptionId);
_coStreamEventSubSubscriptions.Remove(userId);
await _coStreamChannelsRepo.Remove(userId);
return LeaveResult.Ok;
Expand Down

0 comments on commit 4607c91

Please sign in to comment.