Skip to content

Commit

Permalink
Merge pull request #166 from pulcher/add_re_roll_redeem
Browse files Browse the repository at this point in the history
Add re roll redeem
  • Loading branch information
pulcher authored Jul 16, 2024
2 parents 56288f6 + cc146b0 commit e3df1a1
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions Magic8HeadService/ISayingResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
51 changes: 51 additions & 0 deletions Magic8HeadService/MqttHandlers/Redeems/ReRollVoiceHandler .cs
Original file line number Diff line number Diff line change
@@ -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<Worker> logger;

public ReRollVoiceHandler(ITwitchClient client, ISayingResponse sayingResponse, ILogger<Worker> 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<MqttRedeemPayload>(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<MqttRedeemPayload>(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();
}
}
}
16 changes: 16 additions & 0 deletions Magic8HeadService/SayingResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}

0 comments on commit e3df1a1

Please sign in to comment.