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;
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
using TwitchChatTTS;
|
|
|
|
using TwitchChatTTS.Seven;
|
2024-03-15 08:27:35 -04:00
|
|
|
using TwitchChatTTS.Chat.Commands;
|
2024-01-05 05:07:41 -05:00
|
|
|
|
|
|
|
|
|
|
|
public class ChatMessageHandler {
|
2024-03-15 08:27:35 -04:00
|
|
|
private ILogger<ChatMessageHandler> _logger { get; }
|
|
|
|
private Configuration _configuration { get; }
|
|
|
|
public EmoteCounter _emoteCounter { get; }
|
|
|
|
private EmoteDatabase _emotes { get; }
|
|
|
|
private TTSPlayer _player { get; }
|
|
|
|
private ChatCommandManager _commands { get; }
|
|
|
|
private OBSSocketClient? _obsClient { get; }
|
|
|
|
private IServiceProvider _serviceProvider { get; }
|
|
|
|
|
2024-01-05 05:07:41 -05:00
|
|
|
private Regex sfxRegex;
|
|
|
|
|
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
public ChatMessageHandler(
|
|
|
|
ILogger<ChatMessageHandler> logger,
|
|
|
|
Configuration configuration,
|
|
|
|
EmoteCounter emoteCounter,
|
|
|
|
EmoteDatabase emotes,
|
|
|
|
TTSPlayer player,
|
2024-03-15 08:27:35 -04:00
|
|
|
ChatCommandManager commands,
|
2024-03-12 14:05:27 -04:00
|
|
|
[FromKeyedServices("obs")] SocketClient<WebSocketMessage> client,
|
2024-03-15 08:27:35 -04:00
|
|
|
IServiceProvider serviceProvider
|
2024-03-12 14:05:27 -04:00
|
|
|
) {
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger = logger;
|
|
|
|
_configuration = configuration;
|
|
|
|
_emoteCounter = emoteCounter;
|
|
|
|
_emotes = emotes;
|
|
|
|
_player = player;
|
|
|
|
_commands = commands;
|
|
|
|
_obsClient = client as OBSSocketClient;
|
|
|
|
_serviceProvider = serviceProvider;
|
|
|
|
|
2024-01-05 05:07:41 -05:00
|
|
|
sfxRegex = new Regex(@"\(([A-Za-z0-9_-]+)\)");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
public async Task<MessageResult> Handle(OnMessageReceivedArgs e) {
|
|
|
|
if (_configuration.Twitch?.TtsWhenOffline != true && _obsClient?.Live == false)
|
2024-03-12 14:05:27 -04:00
|
|
|
return MessageResult.Blocked;
|
2024-03-15 08:27:35 -04:00
|
|
|
|
|
|
|
var user = _serviceProvider.GetRequiredService<User>();
|
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);
|
|
|
|
|
|
|
|
var blocked = user.ChatterFilters.TryGetValue(m.Username, out TTSUsernameFilter? filter) && filter.Tag == "blacklisted";
|
|
|
|
|
|
|
|
if (!blocked || m.IsBroadcaster) {
|
|
|
|
try {
|
|
|
|
var commandResult = await _commands.Execute(msg, m);
|
|
|
|
if (commandResult != ChatCommandResult.Unknown) {
|
|
|
|
return MessageResult.Command;
|
|
|
|
}
|
|
|
|
} catch (Exception ex) {
|
|
|
|
_logger.LogError(ex, "Failed at executing command.");
|
|
|
|
}
|
2024-01-05 05:07:41 -05:00
|
|
|
}
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
if (blocked) {
|
|
|
|
_logger.LogTrace($"Blocked message by {m.Username}: {msg}");
|
2024-01-05 05:07:41 -05:00
|
|
|
return MessageResult.Blocked;
|
|
|
|
}
|
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
// Replace filtered words.
|
2024-03-15 08:27:35 -04:00
|
|
|
if (user.RegexFilters != null) {
|
|
|
|
foreach (var wf in user.RegexFilters) {
|
2024-03-12 14:05:27 -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-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;
|
|
|
|
foreach (var w in words) {
|
|
|
|
if (wordCounter.ContainsKey(w)) {
|
|
|
|
wordCounter[w]++;
|
|
|
|
} else {
|
|
|
|
wordCounter.Add(w, 1);
|
|
|
|
}
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
var emoteId = _emotes?.Get(w);
|
|
|
|
if (emoteId == null)
|
|
|
|
emoteId = m.EmoteSet.Emotes.FirstOrDefault(e => e.Name == w)?.Id;
|
|
|
|
if (emoteId != null) {
|
|
|
|
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 + " ";
|
|
|
|
}
|
|
|
|
msg = filteredMsg;
|
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
// Adding twitch emotes to the counter.
|
2024-03-15 08:27:35 -04:00
|
|
|
foreach (var emote in e.ChatMessage.EmoteSet.Emotes) {
|
|
|
|
_logger.LogTrace("Twitch emote name used: " + emote.Name);
|
|
|
|
emotesUsed.Add(emote.Id);
|
|
|
|
}
|
2024-03-12 14:05:27 -04:00
|
|
|
|
|
|
|
if (long.TryParse(e.ChatMessage.UserId, out long userId))
|
2024-03-15 08:27:35 -04:00
|
|
|
_emoteCounter.Add(userId, emotesUsed);
|
2024-03-12 14:05:27 -04:00
|
|
|
if (emotesUsed.Any())
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogDebug("Emote counters for user #" + userId + ": " + string.Join(" | ", emotesUsed.Select(e => e + "=" + _emoteCounter.Get(userId, e))));
|
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;
|
|
|
|
if (m.IsStaff) {
|
|
|
|
priority = int.MinValue;
|
2024-03-12 14:05:27 -04:00
|
|
|
} else if (filter?.Tag == "priority") {
|
2024-01-05 05:07:41 -05:00
|
|
|
priority = int.MinValue + 1;
|
|
|
|
} else if (m.IsModerator) {
|
|
|
|
priority = -100;
|
|
|
|
} else if (m.IsVip) {
|
|
|
|
priority = -10;
|
|
|
|
} else if (m.IsPartner) {
|
|
|
|
priority = -5;
|
|
|
|
} else if (m.IsHighlighted) {
|
|
|
|
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.
|
|
|
|
string voiceSelected = user.DefaultTTSVoice;
|
|
|
|
if (user.VoicesSelected?.ContainsKey(userId) == true) {
|
|
|
|
var voiceId = user.VoicesSelected[userId];
|
|
|
|
if (user.VoicesAvailable.TryGetValue(voiceId, out string? voiceName) && voiceName != null) {
|
|
|
|
voiceSelected = voiceName;
|
|
|
|
}
|
2024-01-05 05:07:41 -05:00
|
|
|
}
|
|
|
|
|
2024-03-15 08:27:35 -04:00
|
|
|
// Determine additional voices used
|
|
|
|
var voicesRegex = user.GenerateEnabledVoicesRegex();
|
|
|
|
var matches = voicesRegex?.Matches(msg).ToArray();
|
|
|
|
if (matches == null || matches.FirstOrDefault() == null || matches.FirstOrDefault().Index == 0) {
|
|
|
|
HandlePartialMessage(priority, voiceSelected, msg.Trim(), e);
|
|
|
|
return MessageResult.None;
|
|
|
|
}
|
|
|
|
|
|
|
|
HandlePartialMessage(priority, voiceSelected, msg.Substring(0, matches.FirstOrDefault().Index).Trim(), e);
|
2024-01-05 05:07:41 -05:00
|
|
|
foreach (Match match in matches) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
return MessageResult.None;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void HandlePartialMessage(int priority, string voice, string message, OnMessageReceivedArgs e) {
|
|
|
|
if (string.IsNullOrWhiteSpace(message)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var m = e.ChatMessage;
|
|
|
|
var parts = sfxRegex.Split(message);
|
|
|
|
var badgesString = string.Join(", ", e.ChatMessage.Badges.Select(b => b.Key + " = " + b.Value));
|
|
|
|
|
|
|
|
if (parts.Length == 1) {
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogInformation($"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;
|
|
|
|
|
|
|
|
for (var i = 0; i < sfxMatches.Count; i++) {
|
|
|
|
var sfxMatch = sfxMatches[i];
|
|
|
|
var sfxName = sfxMatch.Groups[1]?.ToString()?.ToLower();
|
|
|
|
|
|
|
|
if (!File.Exists("sfx/" + sfxName + ".mp3")) {
|
|
|
|
parts[i * 2 + 2] = parts[i * 2] + " (" + parts[i * 2 + 1] + ")" + parts[i * 2 + 2];
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(parts[i * 2])) {
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogInformation($"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-03-15 08:27:35 -04:00
|
|
|
_logger.LogInformation($"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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(parts.Last())) {
|
2024-03-15 08:27:35 -04:00
|
|
|
_logger.LogInformation($"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
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|