2024-08-04 19:46:10 -04:00
using Serilog ;
using TwitchChatTTS.Twitch.Redemptions ;
using TwitchChatTTS.Twitch.Socket.Messages ;
namespace TwitchChatTTS.Twitch.Socket.Handlers
{
public class ChannelCustomRedemptionHandler : ITwitchSocketHandler
{
public string Name = > "channel.channel_points_custom_reward_redemption.add" ;
2024-08-06 15:29:29 -04:00
private readonly IRedemptionManager _redemptionManager ;
2024-08-04 19:46:10 -04:00
private readonly ILogger _logger ;
public ChannelCustomRedemptionHandler (
2024-08-06 15:29:29 -04:00
IRedemptionManager redemptionManager ,
2024-08-04 19:46:10 -04:00
ILogger logger
)
{
_redemptionManager = redemptionManager ;
_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 ChannelCustomRedemptionMessage message )
return ;
_logger . Information ( $"Channel Point Reward Redeemed [redeem: {message.Reward.Title}][redeem id: {message.Reward.Id}][transaction: {message.Id}]" ) ;
try
{
var actions = _redemptionManager . Get ( message . Reward . Id ) ;
if ( ! actions . Any ( ) )
{
2024-08-06 17:15:05 -04:00
_logger . Debug ( $"No redeemable actions for this redeem was found [redeem: {message.Reward.Title}][redeem id: {message.Reward.Id}][transaction: {message.Id}]" ) ;
2024-08-04 19:46:10 -04:00
return ;
}
_logger . Debug ( $"Found {actions.Count} actions for this Twitch channel point redemption [redeem: {message.Reward.Title}][redeem id: {message.Reward.Id}][transaction: {message.Id}]" ) ;
foreach ( var action in actions )
try
{
await _redemptionManager . Execute ( action , message . UserName , long . Parse ( message . UserId ) ) ;
}
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: {message.Reward.Title}][redeem id: {message.Reward.Id}][transaction: {message.Id}]" ) ;
2024-08-04 19:46:10 -04:00
}
}
catch ( Exception ex )
{
_logger . Error ( ex , $"Failed to fetch the redeemable actions for a redemption [redeem: {message.Reward.Title}][redeem id: {message.Reward.Id}][transaction: {message.Id}]" ) ;
}
}
}
}