hermes-client/Twitch/Socket/Handlers/ChannelAdBreakBeginHandler.cs

92 lines
4.2 KiB
C#
Raw Normal View History

using Serilog;
using TwitchChatTTS.Twitch.Redemptions;
using TwitchChatTTS.Twitch.Socket.Messages;
namespace TwitchChatTTS.Twitch.Socket.Handlers
{
public class ChannelAdBreakBeginHandler : ITwitchSocketHandler
{
public string Name => "channel.ad_break.begin";
private readonly IRedemptionManager _redemptionManager;
private readonly ILogger _logger;
public ChannelAdBreakBeginHandler(IRedemptionManager redemptionManager, ILogger logger)
{
_redemptionManager = redemptionManager;
_logger = logger;
}
public async Task Execute(TwitchWebsocketClient sender, object data)
{
if (data is not ChannelAdBreakMessage message)
return;
if (message.IsAutomatic)
_logger.Information($"Ad break has begun [duration: {message.DurationSeconds} seconds][automatic: true]");
else
_logger.Information($"Ad break has begun [duration: {message.DurationSeconds} seconds][automatic: false][requester: {message.RequesterUserLogin}][requester id: {message.RequesterUserId}]");
try
{
var actions = _redemptionManager.Get("adbreak_begin");
if (actions.Any())
{
_logger.Debug($"Found {actions.Count()} actions for this Twitch ad break begin");
foreach (var action in actions)
try
{
await _redemptionManager.Execute(action, message.RequesterUserLogin, long.Parse(message.RequesterUserId));
}
catch (Exception ex)
{
_logger.Error(ex, $"Failed to execute redeemable action [action: {action.Name}][action type: {action.Type}][redeem: ad break begin]");
}
}
else
_logger.Debug($"No redeemable actions for ad break begin was found");
}
catch (Exception ex)
{
_logger.Error(ex, $"Failed to fetch the redeemable actions for ad break begin");
}
2024-12-28 21:19:28 +00:00
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Task.Run(async () =>
{
try
{
await Task.Delay(TimeSpan.FromSeconds(message.DurationSeconds));
if (message.IsAutomatic)
_logger.Information($"Ad break has ended [duration: {message.DurationSeconds} seconds][automatic: true]");
else
_logger.Information($"Ad break has ended [duration: {message.DurationSeconds} seconds][automatic: false][requester: {message.RequesterUserLogin}][requester id: {message.RequesterUserId}]");
var actions = _redemptionManager.Get("adbreak_end");
if (actions.Any())
{
_logger.Debug($"Found {actions.Count()} actions for this Twitch ad break end");
foreach (var action in actions)
try
{
await _redemptionManager.Execute(action, message.RequesterUserLogin, long.Parse(message.RequesterUserId));
}
catch (Exception ex)
{
_logger.Error(ex, $"Failed to execute redeemable action [action: {action.Name}][action type: {action.Type}][redeem: ad break end]");
}
}
else
_logger.Debug($"No redeemable actions for ad break end was found");
}
catch (Exception ex)
{
_logger.Error(ex, $"Failed to fetch the redeemable actions for ad break end");
}
});
2024-12-28 21:19:28 +00:00
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
}
}
}