2024-08-04 19:46:10 -04:00
|
|
|
using Serilog;
|
2024-08-06 15:29:29 -04:00
|
|
|
using TwitchChatTTS.Twitch.Redemptions;
|
2024-08-04 19:46:10 -04:00
|
|
|
using TwitchChatTTS.Twitch.Socket.Messages;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.Twitch.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class ChannelSubscriptionHandler : ITwitchSocketHandler
|
|
|
|
{
|
2024-08-06 17:15:05 -04:00
|
|
|
public string Name => "channel.subscribe";
|
2024-08-04 19:46:10 -04:00
|
|
|
|
2024-08-06 15:29:29 -04:00
|
|
|
private readonly IRedemptionManager _redemptionManager;
|
2024-08-04 19:46:10 -04:00
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
2024-08-06 15:29:29 -04:00
|
|
|
public ChannelSubscriptionHandler(IRedemptionManager redemptionManager, ILogger logger)
|
|
|
|
{
|
|
|
|
_redemptionManager = redemptionManager;
|
2024-08-04 19:46:10 -04:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
2024-08-06 15:29:29 -04:00
|
|
|
public async Task Execute(TwitchWebsocketClient sender, object data)
|
2024-08-04 19:46:10 -04:00
|
|
|
{
|
|
|
|
if (data is not ChannelSubscriptionMessage message)
|
|
|
|
return;
|
2024-08-06 15:29:29 -04:00
|
|
|
if (message.IsGifted)
|
|
|
|
return;
|
2024-08-04 19:46:10 -04:00
|
|
|
|
2024-08-07 16:30:03 -04:00
|
|
|
_logger.Debug($"Subscription occured [chatter: {message.UserLogin}][chatter id: {message.UserId}][Tier: {message.Tier}]");
|
2024-08-06 15:29:29 -04:00
|
|
|
try
|
|
|
|
{
|
|
|
|
var actions = _redemptionManager.Get("subscription");
|
|
|
|
if (!actions.Any())
|
|
|
|
{
|
2024-08-06 17:15:05 -04:00
|
|
|
_logger.Debug($"No redeemable actions for this subscription was found [subscriber: {message.UserLogin}][subscriber id: {message.UserId}]");
|
2024-08-06 15:29:29 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_logger.Debug($"Found {actions.Count} actions for this Twitch subscription [subscriber: {message.UserLogin}][subscriber id: {message.UserId}]");
|
|
|
|
|
|
|
|
foreach (var action in actions)
|
|
|
|
try
|
|
|
|
{
|
2024-08-07 16:30:03 -04:00
|
|
|
await _redemptionManager.Execute(action, message.UserName!, long.Parse(message.UserId!));
|
2024-08-06 15:29:29 -04:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2024-08-06 17:15:05 -04:00
|
|
|
_logger.Error(ex, $"Failed to execute redeemable action [action: {action.Name}][action type: {action.Type}][redeem: subscription][subscriber: {message.UserLogin}][subscriber id: {message.UserId}]");
|
2024-08-06 15:29:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.Error(ex, $"Failed to fetch the redeemable actions for subscription [subscriber: {message.UserLogin}][subscriber id: {message.UserId}]");
|
|
|
|
}
|
2024-08-04 19:46:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|