diff --git a/Magic8HeadService/Commands/IMbhCommand.cs b/Magic8HeadService/Commands/IMbhCommand.cs index ee57d8b..5e93e7e 100644 --- a/Magic8HeadService/Commands/IMbhCommand.cs +++ b/Magic8HeadService/Commands/IMbhCommand.cs @@ -1,10 +1,4 @@ -using TwitchLib.Client; -using TwitchLib.Client.Enums; using TwitchLib.Client.Events; -using TwitchLib.Client.Extensions; -using TwitchLib.Client.Models; -using TwitchLib.Communication.Clients; -using TwitchLib.Communication.Models; namespace Magic8HeadService { diff --git a/Magic8HeadService/Commands/MbhCommand.cs b/Magic8HeadService/Commands/MbhCommand.cs index 0af0b50..21f790b 100644 --- a/Magic8HeadService/Commands/MbhCommand.cs +++ b/Magic8HeadService/Commands/MbhCommand.cs @@ -1,12 +1,9 @@ - -using System.Collections.Generic; -using System.Linq; using Magic8HeadService; using Magic8HeadService.Services; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.Logging; using MrBigHead.Shared; -using TwitchLib.Client; +using System.Linq; using TwitchLib.Client.Events; using TwitchLib.Client.Interfaces; diff --git a/Magic8HeadService/Commands/ReadLcCommand.cs b/Magic8HeadService/Commands/ReadLcCommand.cs new file mode 100644 index 0000000..060cd78 --- /dev/null +++ b/Magic8HeadService/Commands/ReadLcCommand.cs @@ -0,0 +1,62 @@ + +using Magic8HeadService; +using Magic8HeadService.Services; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.Logging; +using TwitchLib.Client; +using TwitchLib.Client.Events; +using TwitchLib.Client.Interfaces; + +public class ReadLcCommand : ICommandMbhToTwitch +{ + private readonly ITwitchClient client; + private readonly IConfiguration config; + private readonly IMessageStackService messageStackService; + private readonly ISayingResponse sayingResponse; + private readonly IMessageChecker messageChecker; + private readonly ICommandTracker commandTracker; + private readonly ILogger logger; + + public string Name => "readlc"; + + public ReadLcCommand(ITwitchClient client, IConfiguration config, IMessageStackService messageStackService, + ISayingResponse sayingResponse, IMessageChecker messageChecker, ICommandTracker commandTracker, + ILogger logger) + { + this.client = client; + this.config = config; + this.messageStackService = messageStackService; + this.sayingResponse = sayingResponse; + this.messageChecker = messageChecker; + this.commandTracker = commandTracker; + this.logger = logger; + } + + public void Handle(OnChatCommandReceivedArgs args) + { + var lastMessage = messageStackService.GetNextMessage(); + + if (lastMessage is not null) + { + if (lastMessage.IsSubscriber + || lastMessage.IsVip + || lastMessage.IsModerator) + { + var username = lastMessage.Username; + var channel = lastMessage.Channel; + + var commandTrackerEntity = commandTracker.Add(username, "readlc"); + + var message = $"Speaking for {username}: who typed {messageChecker.CheckMessage(lastMessage.Message)}"; + + sayingResponse.SaySomethingNiceAsync(message, client, channel, username) + .Wait(); + } + else + { + client.SendMessage(lastMessage.Channel, + $"Hey {lastMessage.Username}, the readlc command is for subscribers and vips only."); + } + } + } +} diff --git a/Magic8HeadService/Commands/SayCommand.cs b/Magic8HeadService/Commands/SayCommand.cs index 896da5a..78f4f4f 100644 --- a/Magic8HeadService/Commands/SayCommand.cs +++ b/Magic8HeadService/Commands/SayCommand.cs @@ -1,6 +1,5 @@ using Magic8HeadService.Services; using Microsoft.Extensions.Logging; -using TwitchLib.Client; using TwitchLib.Client.Events; using TwitchLib.Client.Interfaces; diff --git a/Magic8HeadService/IMessageStackService.cs b/Magic8HeadService/IMessageStackService.cs new file mode 100644 index 0000000..216f07b --- /dev/null +++ b/Magic8HeadService/IMessageStackService.cs @@ -0,0 +1,11 @@ +using TwitchLib.Client.Models; + +namespace Magic8HeadService +{ + public interface IMessageStackService + { + ChatMessage GetNextMessage(); + ChatMessage GetPreviousMessage(int previousIndex); + void PutMessage(ChatMessage message); + } +} diff --git a/Magic8HeadService/MessageStackService.cs b/Magic8HeadService/MessageStackService.cs new file mode 100644 index 0000000..08096ce --- /dev/null +++ b/Magic8HeadService/MessageStackService.cs @@ -0,0 +1,33 @@ +using System.Collections.Generic; +using TwitchLib.Client.Models; + +namespace Magic8HeadService +{ + public class MessageStackService : IMessageStackService + { + private readonly Stack messageStack; + + public MessageStackService() + { + messageStack = new Stack(30); + } + public ChatMessage GetNextMessage() + { + messageStack.TryPop(out var result); + return result; + } + + public ChatMessage GetPreviousMessage(int previousIndex) + { + throw new System.NotImplementedException(); + } + + public void PutMessage(ChatMessage message) + { + if (message == null) return; + + messageStack.TrimExcess(); + messageStack.Push(message); + } + } +} diff --git a/Magic8HeadService/Program.cs b/Magic8HeadService/Program.cs index faffa8c..d5ee8d5 100644 --- a/Magic8HeadService/Program.cs +++ b/Magic8HeadService/Program.cs @@ -101,6 +101,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) => services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); services.AddHostedService(); diff --git a/Magic8HeadService/SayingResponse.cs b/Magic8HeadService/SayingResponse.cs index 2a6b4c0..d9c5554 100644 --- a/Magic8HeadService/SayingResponse.cs +++ b/Magic8HeadService/SayingResponse.cs @@ -137,12 +137,6 @@ public async Task SaySomethingNiceAsync(string message, ITwitchClient client, st var speechConfig = GetSpeechConfig(commandTrackerEntity, username); - //if (client != null) - //{ - // client.SendMessage(channel, - // $"Hey {username}, you are using {speechConfig.SpeechSynthesisVoiceName}"); - //} - var ssmlMessage = ConvertToSsml(speechConfig, message); using (var result = await speechSynthesizer.StartSpeakingSsmlAsync(ssmlMessage)) diff --git a/Magic8HeadService/TwitchBot.cs b/Magic8HeadService/TwitchBot.cs index 58d34ee..8d14831 100644 --- a/Magic8HeadService/TwitchBot.cs +++ b/Magic8HeadService/TwitchBot.cs @@ -1,22 +1,19 @@ +using Magic8HeadService.MqttHandlers; using Microsoft.Extensions.Logging; -using MrBigHead.Shared; +using MQTTnet; +using MQTTnet.Client; using System; -using System.Linq; using System.Collections.Generic; -using TwitchLib.Client; +using System.Linq; +using System.Text; +using System.Threading; +using System.Threading.Tasks; using TwitchLib.Client.Enums; using TwitchLib.Client.Events; using TwitchLib.Client.Extensions; -using TwitchLib.Client.Models; -using Microsoft.Extensions.Configuration; using TwitchLib.Client.Interfaces; +using TwitchLib.Client.Models; using TwitchLib.Communication.Events; -using MQTTnet.Client; -using MQTTnet; -using System.Threading.Tasks; -using System.Threading; -using System.Text; -using Magic8HeadService.MqttHandlers; namespace Magic8HeadService { @@ -31,10 +28,12 @@ public class TwitchBot private readonly ILogger logger; private readonly ISayingResponse sayingResponse; private readonly IDadJokeService dadJokeService; + private readonly IMessageStackService messageStackService; public TwitchBot(ITwitchClient client, ConnectionCredentials clientCredentials, TwitchBotConfiguration twitchBotConfiguration, ISayingResponse sayingResponse, IDadJokeService dadJokeService, IEnumerable listOfCommands, - ICommandMbhTwitchHelp helpCommand, MqttFactory mqttFactory, IEnumerable mqttHandlers, ILogger logger) + ICommandMbhTwitchHelp helpCommand, MqttFactory mqttFactory, IEnumerable mqttHandlers, + IMessageStackService messageStackService, ILogger logger) { this.client = client; @@ -42,7 +41,7 @@ public TwitchBot(ITwitchClient client, ConnectionCredentials clientCredentials, this.logger = logger; this.sayingResponse = sayingResponse; this.dadJokeService = dadJokeService; - + this.messageStackService = messageStackService; var listOfNames = listOfCommands.Select(x => x.Name); this.logger.LogInformation($"-------------- List of Names : {string.Join(',', listOfNames)}"); dictOfCommands = listOfCommands @@ -163,6 +162,16 @@ public void Client_OnJoinedChannel(object sender, OnJoinedChannelArgs e) public void Client_OnMessageReceived(object sender, OnMessageReceivedArgs e) { + if (!e.ChatMessage.IsVip + && !e.ChatMessage.IsModerator + && !e.ChatMessage.IsSubscriber + && !e.ChatMessage.IsMe + && !e.ChatMessage.IsStaff + && !e.ChatMessage.Message.StartsWith('!')) + { + messageStackService.PutMessage(e.ChatMessage); + } + if (e.ChatMessage.Message.Contains("badword")) this.client.TimeoutUser(e.ChatMessage.Channel, e.ChatMessage.Username, TimeSpan.FromMinutes(30), "Bad word! 30 minute timeout!"); } diff --git a/Magic8HeadService/Worker.cs b/Magic8HeadService/Worker.cs index 5018f89..b916fd8 100644 --- a/Magic8HeadService/Worker.cs +++ b/Magic8HeadService/Worker.cs @@ -28,6 +28,7 @@ public class Worker : BackgroundService readonly ISayingResponse scopedSayingResponse; readonly IDadJokeService scopedDadJokeService; readonly MqttFactory scopedMqttFactory; + readonly IMessageStackService scopedMessageStackService; public Worker(IServiceProvider service, IConfiguration config, TwitchBotConfiguration twitchBotConfiguration, ILogger logger) @@ -58,13 +59,17 @@ public Worker(IServiceProvider service, IConfiguration config, TwitchBotConfigur scope.ServiceProvider .GetRequiredService(); + scopedMessageStackService = + scope.ServiceProvider + .GetRequiredService(); + var listOfCommands = scope.ServiceProvider.GetServices(); var helpCommand = scope.ServiceProvider.GetService(); var mqttHandlers = scope.ServiceProvider.GetServices(); var twitchBot = new TwitchBot(twitchClient, connectionCredentials, twitchBotConfiguration, scopedSayingResponse, scopedDadJokeService, listOfCommands, - helpCommand, scopedMqttFactory, mqttHandlers, + helpCommand, scopedMqttFactory, mqttHandlers, scopedMessageStackService, logger); SetupGPIO();