2024-08-06 15:29:29 -04:00
|
|
|
using Serilog;
|
2024-08-12 01:44:31 -04:00
|
|
|
using TwitchChatTTS.Chat.Messaging;
|
2024-08-06 15:29:29 -04:00
|
|
|
using TwitchChatTTS.Twitch.Redemptions;
|
|
|
|
using TwitchChatTTS.Twitch.Socket.Messages;
|
|
|
|
|
|
|
|
namespace TwitchChatTTS.Twitch.Socket.Handlers
|
|
|
|
{
|
|
|
|
public class ChannelResubscriptionHandler : ITwitchSocketHandler
|
|
|
|
{
|
|
|
|
public string Name => "channel.subscription.message";
|
|
|
|
|
2024-08-12 01:44:31 -04:00
|
|
|
private readonly ChatMessageReader _reader;
|
2024-08-06 15:29:29 -04:00
|
|
|
private readonly IRedemptionManager _redemptionManager;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
2024-08-12 01:44:31 -04:00
|
|
|
public ChannelResubscriptionHandler(ChatMessageReader reader, IRedemptionManager redemptionManager, ILogger logger)
|
2024-08-06 15:29:29 -04:00
|
|
|
{
|
2024-08-12 01:44:31 -04:00
|
|
|
_reader = reader;
|
2024-08-06 15:29:29 -04:00
|
|
|
_redemptionManager = redemptionManager;
|
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task Execute(TwitchWebsocketClient sender, object data)
|
|
|
|
{
|
|
|
|
if (data is not ChannelResubscriptionMessage message)
|
|
|
|
return;
|
|
|
|
|
2024-08-07 16:30:03 -04:00
|
|
|
_logger.Debug($"Resubscription occured [chatter: {message.UserLogin}][chatter id: {message.UserId}][Tier: {message.Tier}][Streak: {message.StreakMonths}][Cumulative: {message.CumulativeMonths}][Duration: {message.DurationMonths}]");
|
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 [message: {message.Message.Text}]");
|
2024-08-06 15:29:29 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
_logger.Debug($"Found {actions.Count} actions for this Twitch subscription [message: {message.Message.Text}]");
|
|
|
|
|
|
|
|
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: resubscription][message: {message.Message.Text}]");
|
2024-08-06 15:29:29 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
|
|
|
_logger.Error(ex, $"Failed to fetch the redeemable actions for subscription [message: {message.Message.Text}]");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|