diff --git a/Magic8HeadService/ISayingResponse.cs b/Magic8HeadService/ISayingResponse.cs index 8d6329f..50f262a 100644 --- a/Magic8HeadService/ISayingResponse.cs +++ b/Magic8HeadService/ISayingResponse.cs @@ -12,5 +12,6 @@ Task SaySomethingNiceAsync(string message, ITwitchClient client, string channel, string username, CommandTrackerEntry commandTrackerEntity = null); Task SetupSayingsAsync(); Task SetupVoiceListAsync(); + bool ResetVoiceForUser(string username); } } diff --git a/Magic8HeadService/MqttHandlers/Redeems/ReRollVoiceHandler .cs b/Magic8HeadService/MqttHandlers/Redeems/ReRollVoiceHandler .cs new file mode 100644 index 0000000..b3f9d5b --- /dev/null +++ b/Magic8HeadService/MqttHandlers/Redeems/ReRollVoiceHandler .cs @@ -0,0 +1,51 @@ +using Microsoft.Extensions.Logging; +using System.Linq; +using TwitchLib.Client.Interfaces; +using System.Text.Json; +using System.Text; + +namespace Magic8HeadService.MqttHandlers.Redeems +{ + public class ReRollVoiceHandler : IMqttHandler + { + private ITwitchClient client; + private readonly ISayingResponse sayingResponse; + private ILogger logger; + + public ReRollVoiceHandler(ITwitchClient client, ISayingResponse sayingResponse, ILogger logger) + { + this.client = client; + this.sayingResponse = sayingResponse; + this.logger = logger; + } + + public bool CanHandle(MqttHandlerMessage message) + { + if (message == null) return false; + + var payloadString = Encoding.ASCII.GetString(message.Payload); + + var redeem = JsonSerializer.Deserialize(payloadString); + + if (redeem != null && redeem.RewardName == "Voice Lottery Ticket") + return true; + else + return false; + } + + public void Handle(MqttHandlerMessage message) + { + var payloadString = Encoding.ASCII.GetString(message.Payload); + var redeem = JsonSerializer.Deserialize(payloadString); + var messageToSay = $"Hey Programs guess who is getting a new voice? {redeem.UserName} is cuz they are a big baby!"; + + if (!sayingResponse.ResetVoiceForUser(redeem.UserName)) + { + messageToSay = $"Hey {redeem.UserName}! You need to get one first! You do that by subscribing! SHOW ME THE MONEY!"; + } + + sayingResponse.SaySomethingNiceAsync(messageToSay, client, + client.JoinedChannels.FirstOrDefault().ToString(), string.Empty).Wait(); + } + } +} diff --git a/Magic8HeadService/SayingResponse.cs b/Magic8HeadService/SayingResponse.cs index d9c5554..9775ec1 100644 --- a/Magic8HeadService/SayingResponse.cs +++ b/Magic8HeadService/SayingResponse.cs @@ -232,5 +232,21 @@ public string PickSaying(string mood) return pickedSaying.Phrase; } + + public bool ResetVoiceForUser(string username) + { + logger.LogInformation("{username}", username); + + // see if the user exists + var userSpeechConfigs = speechConfigAssociated.Where(u => u.Username == username).FirstOrDefault(); + + if (userSpeechConfigs != null) + { + speechConfigAssociated.Remove(userSpeechConfigs); + return true; + } + + return false; + } } }