Skip to content

Commit

Permalink
Merge pull request #137 from pulcher/add_readlc
Browse files Browse the repository at this point in the history
Add readlc
  • Loading branch information
pulcher authored Nov 14, 2023
2 parents 5dbf0af + 184642d commit 3ef02b9
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 31 deletions.
6 changes: 0 additions & 6 deletions Magic8HeadService/Commands/IMbhCommand.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down
5 changes: 1 addition & 4 deletions Magic8HeadService/Commands/MbhCommand.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
62 changes: 62 additions & 0 deletions Magic8HeadService/Commands/ReadLcCommand.cs
Original file line number Diff line number Diff line change
@@ -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<Worker> logger;

public string Name => "readlc";

public ReadLcCommand(ITwitchClient client, IConfiguration config, IMessageStackService messageStackService,
ISayingResponse sayingResponse, IMessageChecker messageChecker, ICommandTracker commandTracker,
ILogger<Worker> 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.");
}
}
}
}
1 change: 0 additions & 1 deletion Magic8HeadService/Commands/SayCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using Magic8HeadService.Services;
using Microsoft.Extensions.Logging;
using TwitchLib.Client;
using TwitchLib.Client.Events;
using TwitchLib.Client.Interfaces;

Expand Down
11 changes: 11 additions & 0 deletions Magic8HeadService/IMessageStackService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using TwitchLib.Client.Models;

namespace Magic8HeadService
{
public interface IMessageStackService
{
ChatMessage GetNextMessage();
ChatMessage GetPreviousMessage(int previousIndex);
void PutMessage(ChatMessage message);
}
}
33 changes: 33 additions & 0 deletions Magic8HeadService/MessageStackService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Collections.Generic;
using TwitchLib.Client.Models;

namespace Magic8HeadService
{
public class MessageStackService : IMessageStackService
{
private readonly Stack<ChatMessage> messageStack;

public MessageStackService()
{
messageStack = new Stack<ChatMessage>(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);
}
}
}
1 change: 1 addition & 0 deletions Magic8HeadService/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
services.AddScoped<IVoiceService, VoiceService>();
services.AddScoped<IDadJokeService, DadJokeService>();
services.AddScoped<ICommandMbhTwitchHelp, HelpCommandReal>();
services.AddScoped<IMessageStackService, MessageStackService>();

services.AddHostedService<Worker>();

Expand Down
6 changes: 0 additions & 6 deletions Magic8HeadService/SayingResponse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
35 changes: 22 additions & 13 deletions Magic8HeadService/TwitchBot.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -31,18 +28,20 @@ public class TwitchBot
private readonly ILogger<Worker> 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<ICommandMbhToTwitch> listOfCommands,
ICommandMbhTwitchHelp helpCommand, MqttFactory mqttFactory, IEnumerable<IMqttHandler> mqttHandlers, ILogger<Worker> logger)
ICommandMbhTwitchHelp helpCommand, MqttFactory mqttFactory, IEnumerable<IMqttHandler> mqttHandlers,
IMessageStackService messageStackService, ILogger<Worker> logger)
{
this.client = client;

this.helpCommandReal = (ICommandMbhToTwitch)helpCommand;
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
Expand Down Expand Up @@ -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!");
}
Expand Down
7 changes: 6 additions & 1 deletion Magic8HeadService/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Worker> logger)
Expand Down Expand Up @@ -58,13 +59,17 @@ public Worker(IServiceProvider service, IConfiguration config, TwitchBotConfigur
scope.ServiceProvider
.GetRequiredService<MqttFactory>();

scopedMessageStackService =
scope.ServiceProvider
.GetRequiredService<IMessageStackService>();

var listOfCommands = scope.ServiceProvider.GetServices<ICommandMbhToTwitch>();
var helpCommand = scope.ServiceProvider.GetService<ICommandMbhTwitchHelp>();
var mqttHandlers = scope.ServiceProvider.GetServices<IMqttHandler>();

var twitchBot = new TwitchBot(twitchClient, connectionCredentials, twitchBotConfiguration,
scopedSayingResponse, scopedDadJokeService, listOfCommands,
helpCommand, scopedMqttFactory, mqttHandlers,
helpCommand, scopedMqttFactory, mqttHandlers, scopedMessageStackService,
logger);

SetupGPIO();
Expand Down

0 comments on commit 3ef02b9

Please sign in to comment.