Fixed command permissions. Moved to using Twitch's EventSub via websockets. Cleaned some code up. Added detection for subscription messages (no TTS), message deletion, full or partial chat clear. Removes messages from TTS queue if applicable. Added command aliases for static parameters. Word filters use compiled regex if possible. Fixed TTS voice deletion.

This commit is contained in:
Tom
2024-08-04 23:46:10 +00:00
parent 472bfcee5d
commit 75fcb8e0f8
61 changed files with 2268 additions and 925 deletions

View File

@@ -0,0 +1,69 @@
using System.Text.Json;
using Microsoft.Extensions.DependencyInjection;
using Serilog;
using TwitchChatTTS.Twitch.Socket.Messages;
namespace TwitchChatTTS.Twitch.Socket.Handlers
{
public sealed class NotificationHandler : ITwitchSocketHandler
{
public string Name => "notification";
private IDictionary<string, ITwitchSocketHandler> _handlers;
private readonly ILogger _logger;
private IDictionary<string, Type> _messageTypes;
private readonly JsonSerializerOptions _options;
public NotificationHandler(
[FromKeyedServices("twitch-notifications")] IEnumerable<ITwitchSocketHandler> handlers,
ILogger logger
)
{
_handlers = handlers.ToDictionary(h => h.Name, h => h);
_logger = logger;
_options = new JsonSerializerOptions() {
PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
};
_messageTypes = new Dictionary<string, Type>();
_messageTypes.Add("channel.ban", typeof(ChannelBanMessage));
_messageTypes.Add("channel.chat.message", typeof(ChannelChatMessage));
_messageTypes.Add("channel.chat.clear_user_messages", typeof(ChannelChatClearUserMessage));
_messageTypes.Add("channel.chat.clear", typeof(ChannelChatClearMessage));
_messageTypes.Add("channel.chat.message_delete", typeof(ChannelChatDeleteMessage));
_messageTypes.Add("channel.channel_points_custom_reward_redemption.add", typeof(ChannelCustomRedemptionMessage));
_messageTypes.Add("channel.subscription.message", typeof(ChannelSubscriptionMessage));
}
public async Task Execute(TwitchWebsocketClient sender, object? data)
{
if (sender == null)
return;
if (data == null)
{
_logger.Warning("Twitch websocket message data is null.");
return;
}
if (data is not NotificationMessage message)
return;
if (!_messageTypes.TryGetValue(message.Subscription.Type, out var type) || type == null)
{
_logger.Warning($"Could not find Twitch notification type [message type: {message.Subscription.Type}]");
return;
}
if (!_handlers.TryGetValue(message.Subscription.Type, out ITwitchSocketHandler? handler) || handler == null)
{
_logger.Warning($"Could not find Twitch notification handler [message type: {message.Subscription.Type}]");
return;
}
var d = JsonSerializer.Deserialize(message.Event.ToString()!, type, _options);
await handler.Execute(sender, d);
}
}
}