2024-01-05 05:07:41 -05:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
using TwitchLib.Client.Events;
|
2024-03-12 14:05:27 -04:00
|
|
|
using TwitchChatTTS.OBS.Socket;
|
|
|
|
using CommonSocketLibrary.Abstract;
|
|
|
|
using CommonSocketLibrary.Common;
|
2024-06-16 20:19:31 -04:00
|
|
|
using Serilog;
|
2024-03-12 14:05:27 -04:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using TwitchChatTTS;
|
|
|
|
using TwitchChatTTS.Seven;
|
2024-03-15 08:27:35 -04:00
|
|
|
using TwitchChatTTS.Chat.Commands;
|
2024-06-16 20:19:31 -04:00
|
|
|
using TwitchChatTTS.Hermes.Socket;
|
|
|
|
using HermesSocketLibrary.Socket.Data;
|
2024-01-05 05:07:41 -05:00
|
|
|
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public class ChatMessageHandler
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
private readonly User _user;
|
|
|
|
private readonly Configuration _configuration;
|
|
|
|
private readonly EmoteDatabase _emotes;
|
|
|
|
private readonly TTSPlayer _player;
|
|
|
|
private readonly ChatCommandManager _commands;
|
|
|
|
private readonly OBSSocketClient? _obsClient;
|
|
|
|
private readonly HermesSocketClient? _hermesClient;
|
|
|
|
private readonly ILogger _logger;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-01-05 05:07:41 -05:00
|
|
|
private Regex sfxRegex;
|
2024-06-16 20:19:31 -04:00
|
|
|
private HashSet<long> _chatters;
|
|
|
|
|
|
|
|
public HashSet<long> Chatters { get => _chatters; set => _chatters = value; }
|
2024-01-05 05:07:41 -05:00
|
|
|
|
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
public ChatMessageHandler(
|
2024-06-24 18:11:36 -04:00
|
|
|
User user,
|
2024-03-12 14:05:27 -04:00
|
|
|
TTSPlayer player,
|
2024-03-15 08:27:35 -04:00
|
|
|
ChatCommandManager commands,
|
2024-06-16 20:19:31 -04:00
|
|
|
EmoteDatabase emotes,
|
|
|
|
[FromKeyedServices("obs")] SocketClient<WebSocketMessage> obsClient,
|
|
|
|
[FromKeyedServices("hermes")] SocketClient<WebSocketMessage> hermesClient,
|
|
|
|
Configuration configuration,
|
|
|
|
ILogger logger
|
|
|
|
)
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_user = user;
|
2024-03-15 08:27:35 -04:00
|
|
|
_player = player;
|
|
|
|
_commands = commands;
|
2024-06-16 20:19:31 -04:00
|
|
|
_emotes = emotes;
|
|
|
|
_obsClient = obsClient as OBSSocketClient;
|
|
|
|
_hermesClient = hermesClient as HermesSocketClient;
|
|
|
|
_configuration = configuration;
|
|
|
|
_logger = logger;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
_chatters = null;
|
2024-01-05 05:07:41 -05:00
|
|
|
sfxRegex = new Regex(@"\(([A-Za-z0-9_-]+)\)");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public async Task<MessageResult> Handle(OnMessageReceivedArgs e)
|
|
|
|
{
|
|
|
|
if (_obsClient == null || _hermesClient == null || _obsClient.Connected && _chatters == null)
|
|
|
|
return new MessageResult(MessageStatus.NotReady, -1, -1);
|
|
|
|
if (_configuration.Twitch?.TtsWhenOffline != true && _obsClient.Live == false)
|
|
|
|
return new MessageResult(MessageStatus.NotReady, -1, -1);
|
|
|
|
|
2024-01-05 05:07:41 -05:00
|
|
|
var m = e.ChatMessage;
|
|
|
|
var msg = e.ChatMessage.Message;
|
2024-03-15 08:27:35 -04:00
|
|
|
var chatterId = long.Parse(m.UserId);
|
2024-06-16 20:19:31 -04:00
|
|
|
var tasks = new List<Task>();
|
2024-03-15 08:27:35 -04:00
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
var blocked = _user.ChatterFilters.TryGetValue(m.Username, out TTSUsernameFilter? filter) && filter.Tag == "blacklisted";
|
2024-06-16 20:19:31 -04:00
|
|
|
if (!blocked || m.IsBroadcaster)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
var commandResult = await _commands.Execute(msg, m);
|
2024-06-16 20:19:31 -04:00
|
|
|
if (commandResult != ChatCommandResult.Unknown)
|
|
|
|
return new MessageResult(MessageStatus.Command, -1, -1);
|
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
_logger.Error(ex, $"Failed executing a chat command [message: {msg}][chatter: {m.Username}][cid: {m.UserId}][mid: {m.Id}]");
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
2024-01-05 05:07:41 -05:00
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
if (blocked)
|
|
|
|
{
|
|
|
|
_logger.Debug($"Blocked message by {m.Username}: {msg}");
|
|
|
|
return new MessageResult(MessageStatus.Blocked, -1, -1);
|
2024-01-05 05:07:41 -05:00
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
if (_obsClient.Connected && !_chatters.Contains(chatterId))
|
|
|
|
{
|
|
|
|
tasks.Add(_hermesClient.Send(6, new ChatterMessage()
|
|
|
|
{
|
|
|
|
Id = chatterId,
|
|
|
|
Name = m.Username
|
|
|
|
}));
|
|
|
|
_chatters.Add(chatterId);
|
2024-03-12 14:05:27 -04:00
|
|
|
}
|
2024-01-05 05:07:41 -05:00
|
|
|
|
|
|
|
// Filter highly repetitive words (like emotes) from the message.
|
2024-03-15 08:27:35 -04:00
|
|
|
int totalEmoteUsed = 0;
|
2024-03-12 14:05:27 -04:00
|
|
|
var emotesUsed = new HashSet<string>();
|
2024-01-05 05:07:41 -05:00
|
|
|
var words = msg.Split(" ");
|
|
|
|
var wordCounter = new Dictionary<string, int>();
|
|
|
|
string filteredMsg = string.Empty;
|
2024-06-16 20:19:31 -04:00
|
|
|
var newEmotes = new Dictionary<string, string>();
|
|
|
|
foreach (var w in words)
|
|
|
|
{
|
|
|
|
if (wordCounter.ContainsKey(w))
|
2024-01-05 05:07:41 -05:00
|
|
|
wordCounter[w]++;
|
2024-06-16 20:19:31 -04:00
|
|
|
else
|
2024-01-05 05:07:41 -05:00
|
|
|
wordCounter.Add(w, 1);
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
var emoteId = _emotes.Get(w);
|
2024-03-15 08:27:35 -04:00
|
|
|
if (emoteId == null)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
emoteId = m.EmoteSet.Emotes.FirstOrDefault(e => e.Name == w)?.Id;
|
2024-06-16 20:19:31 -04:00
|
|
|
if (emoteId != null)
|
|
|
|
{
|
|
|
|
newEmotes.Add(emoteId, w);
|
|
|
|
_emotes.Add(w, emoteId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (emoteId != null)
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
emotesUsed.Add(emoteId);
|
|
|
|
totalEmoteUsed++;
|
|
|
|
}
|
2024-03-12 14:05:27 -04:00
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
if (wordCounter[w] <= 4 && (emoteId == null || totalEmoteUsed <= 5))
|
2024-01-05 05:07:41 -05:00
|
|
|
filteredMsg += w + " ";
|
|
|
|
}
|
2024-06-16 20:19:31 -04:00
|
|
|
if (_obsClient.Connected && newEmotes.Any())
|
|
|
|
tasks.Add(_hermesClient.Send(7, new EmoteDetailsMessage()
|
|
|
|
{
|
|
|
|
Emotes = newEmotes
|
|
|
|
}));
|
2024-01-05 05:07:41 -05:00
|
|
|
msg = filteredMsg;
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
// Replace filtered words.
|
2024-06-24 18:11:36 -04:00
|
|
|
if (_user.RegexFilters != null)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
foreach (var wf in _user.RegexFilters)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
|
|
|
if (wf.Search == null || wf.Replace == null)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (wf.IsRegex)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
var regex = new Regex(wf.Search);
|
|
|
|
msg = regex.Replace(msg, wf.Replace);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
catch (Exception)
|
|
|
|
{
|
|
|
|
wf.IsRegex = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
msg = msg.Replace(wf.Search, wf.Replace);
|
|
|
|
}
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
2024-01-05 05:07:41 -05:00
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
// Determine the priority of this message
|
2024-01-05 05:07:41 -05:00
|
|
|
int priority = 0;
|
2024-06-16 20:19:31 -04:00
|
|
|
if (m.IsStaff)
|
2024-01-05 05:07:41 -05:00
|
|
|
priority = int.MinValue;
|
2024-06-16 20:19:31 -04:00
|
|
|
else if (filter?.Tag == "priority")
|
2024-01-05 05:07:41 -05:00
|
|
|
priority = int.MinValue + 1;
|
2024-06-16 20:19:31 -04:00
|
|
|
else if (m.IsModerator)
|
2024-01-05 05:07:41 -05:00
|
|
|
priority = -100;
|
2024-06-16 20:19:31 -04:00
|
|
|
else if (m.IsVip)
|
2024-01-05 05:07:41 -05:00
|
|
|
priority = -10;
|
2024-06-16 20:19:31 -04:00
|
|
|
else if (m.IsPartner)
|
2024-01-05 05:07:41 -05:00
|
|
|
priority = -5;
|
2024-06-16 20:19:31 -04:00
|
|
|
else if (m.IsHighlighted)
|
2024-01-05 05:07:41 -05:00
|
|
|
priority = -1;
|
2024-03-15 08:27:35 -04:00
|
|
|
priority = Math.Min(priority, -m.SubscribedMonthCount * (m.IsSubscriber ? 2 : 1));
|
2024-01-05 05:07:41 -05:00
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
// Determine voice selected.
|
2024-06-24 18:11:36 -04:00
|
|
|
string voiceSelected = _user.DefaultTTSVoice;
|
|
|
|
if (long.TryParse(e.ChatMessage.UserId, out long userId) && _user.VoicesSelected?.ContainsKey(userId) == true)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-06-24 18:11:36 -04:00
|
|
|
var voiceId = _user.VoicesSelected[userId];
|
|
|
|
if (_user.VoicesAvailable.TryGetValue(voiceId, out string? voiceName) && voiceName != null)
|
2024-06-16 20:19:31 -04:00
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
voiceSelected = voiceName;
|
|
|
|
}
|
2024-01-05 05:07:41 -05:00
|
|
|
}
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
// Determine additional voices used
|
2024-06-24 18:11:36 -04:00
|
|
|
var matches = _user.WordFilterRegex?.Matches(msg).ToArray();
|
2024-06-16 20:19:31 -04:00
|
|
|
if (matches == null || matches.FirstOrDefault() == null || matches.First().Index < 0)
|
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
HandlePartialMessage(priority, voiceSelected, msg.Trim(), e);
|
2024-06-24 18:11:36 -04:00
|
|
|
return new MessageResult(MessageStatus.None, _user.TwitchUserId, chatterId, emotesUsed);
|
2024-03-15 08:27:35 -04:00
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
HandlePartialMessage(priority, voiceSelected, msg.Substring(0, matches.First().Index).Trim(), e);
|
|
|
|
foreach (Match match in matches)
|
|
|
|
{
|
2024-01-05 05:07:41 -05:00
|
|
|
var message = match.Groups[2].ToString();
|
2024-03-15 08:27:35 -04:00
|
|
|
if (string.IsNullOrWhiteSpace(message))
|
2024-01-05 05:07:41 -05:00
|
|
|
continue;
|
|
|
|
|
|
|
|
var voice = match.Groups[1].ToString();
|
|
|
|
voice = voice[0].ToString().ToUpper() + voice.Substring(1).ToLower();
|
|
|
|
HandlePartialMessage(priority, voice, message.Trim(), e);
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
if (tasks.Any())
|
|
|
|
await Task.WhenAll(tasks);
|
|
|
|
|
2024-06-24 18:11:36 -04:00
|
|
|
return new MessageResult(MessageStatus.None, _user.TwitchUserId, chatterId, emotesUsed);
|
2024-01-05 05:07:41 -05:00
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
private void HandlePartialMessage(int priority, string voice, string message, OnMessageReceivedArgs e)
|
|
|
|
{
|
|
|
|
if (string.IsNullOrWhiteSpace(message))
|
|
|
|
{
|
2024-01-05 05:07:41 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var m = e.ChatMessage;
|
|
|
|
var parts = sfxRegex.Split(message);
|
|
|
|
var badgesString = string.Join(", ", e.ChatMessage.Badges.Select(b => b.Key + " = " + b.Value));
|
2024-06-16 20:19:31 -04:00
|
|
|
|
|
|
|
if (parts.Length == 1)
|
|
|
|
{
|
|
|
|
_logger.Information($"Username: {m.Username}; User ID: {m.UserId}; Voice: {voice}; Priority: {priority}; Message: {message}; Month: {m.SubscribedMonthCount}; {badgesString}");
|
|
|
|
_player.Add(new TTSMessage()
|
|
|
|
{
|
2024-01-05 05:07:41 -05:00
|
|
|
Voice = voice,
|
|
|
|
Message = message,
|
|
|
|
Moderator = m.IsModerator,
|
|
|
|
Timestamp = DateTime.UtcNow,
|
|
|
|
Username = m.Username,
|
|
|
|
Bits = m.Bits,
|
|
|
|
Badges = m.Badges,
|
|
|
|
Priority = priority
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var sfxMatches = sfxRegex.Matches(message);
|
|
|
|
var sfxStart = sfxMatches.FirstOrDefault()?.Index ?? message.Length;
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
for (var i = 0; i < sfxMatches.Count; i++)
|
|
|
|
{
|
2024-01-05 05:07:41 -05:00
|
|
|
var sfxMatch = sfxMatches[i];
|
|
|
|
var sfxName = sfxMatch.Groups[1]?.ToString()?.ToLower();
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
if (!File.Exists("sfx/" + sfxName + ".mp3"))
|
|
|
|
{
|
2024-01-05 05:07:41 -05:00
|
|
|
parts[i * 2 + 2] = parts[i * 2] + " (" + parts[i * 2 + 1] + ")" + parts[i * 2 + 2];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
if (!string.IsNullOrWhiteSpace(parts[i * 2]))
|
|
|
|
{
|
|
|
|
_logger.Information($"Username: {m.Username}; User ID: {m.UserId}; Voice: {voice}; Priority: {priority}; Message: {parts[i * 2]}; Month: {m.SubscribedMonthCount}; {badgesString}");
|
|
|
|
_player.Add(new TTSMessage()
|
|
|
|
{
|
2024-01-05 05:07:41 -05:00
|
|
|
Voice = voice,
|
|
|
|
Message = parts[i * 2],
|
|
|
|
Moderator = m.IsModerator,
|
|
|
|
Timestamp = DateTime.UtcNow,
|
|
|
|
Username = m.Username,
|
|
|
|
Bits = m.Bits,
|
|
|
|
Badges = m.Badges,
|
|
|
|
Priority = priority
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
_logger.Information($"Username: {m.Username}; User ID: {m.UserId}; Voice: {voice}; Priority: {priority}; SFX: {sfxName}; Month: {m.SubscribedMonthCount}; {badgesString}");
|
|
|
|
_player.Add(new TTSMessage()
|
|
|
|
{
|
2024-01-05 05:07:41 -05:00
|
|
|
Voice = voice,
|
|
|
|
Message = sfxName,
|
|
|
|
File = $"sfx/{sfxName}.mp3",
|
|
|
|
Moderator = m.IsModerator,
|
|
|
|
Timestamp = DateTime.UtcNow,
|
|
|
|
Username = m.Username,
|
|
|
|
Bits = m.Bits,
|
|
|
|
Badges = m.Badges,
|
|
|
|
Priority = priority
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
if (!string.IsNullOrWhiteSpace(parts.Last()))
|
|
|
|
{
|
|
|
|
_logger.Information($"Username: {m.Username}; User ID: {m.UserId}; Voice: {voice}; Priority: {priority}; Message: {parts.Last()}; Month: {m.SubscribedMonthCount}; {badgesString}");
|
|
|
|
_player.Add(new TTSMessage()
|
|
|
|
{
|
2024-01-05 05:07:41 -05:00
|
|
|
Voice = voice,
|
|
|
|
Message = parts.Last(),
|
|
|
|
Moderator = m.IsModerator,
|
|
|
|
Timestamp = DateTime.UtcNow,
|
|
|
|
Username = m.Username,
|
|
|
|
Bits = m.Bits,
|
|
|
|
Badges = m.Badges,
|
|
|
|
Priority = priority
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|