Using Serilog. Added partial OBS batch request support. Added update checking. Added more commands. Added enabled/disabled TTS voices. And more.

This commit is contained in:
Tom
2024-06-17 00:19:31 +00:00
parent d4004d6230
commit 706cd06930
67 changed files with 1933 additions and 925 deletions

View File

@ -1,70 +1,42 @@
using System.Collections.Concurrent;
namespace TwitchChatTTS.Seven
{
public class EmoteCounter {
public IDictionary<string, IDictionary<long, int>> Counters { get; set; }
public class EmoteDatabase
{
private readonly IDictionary<string, string> _emotes;
public IDictionary<string, string> Emotes { get => _emotes.AsReadOnly(); }
public EmoteCounter() {
Counters = new ConcurrentDictionary<string, IDictionary<long, int>>();
public EmoteDatabase()
{
_emotes = new Dictionary<string, string>();
}
public void Add(long userId, IEnumerable<string> emoteIds) {
foreach (var emote in emoteIds) {
if (Counters.TryGetValue(emote, out IDictionary<long, int>? subcounters)) {
if (subcounters.TryGetValue(userId, out int counter))
subcounters[userId] = counter + 1;
else
subcounters.Add(userId, 1);
} else {
Counters.Add(emote, new ConcurrentDictionary<long, int>());
Counters[emote].Add(userId, 1);
}
}
}
public void Clear() {
Counters.Clear();
}
public int Get(long userId, string emoteId) {
if (Counters.TryGetValue(emoteId, out IDictionary<long, int>? subcounters)) {
if (subcounters.TryGetValue(userId, out int counter))
return counter;
}
return -1;
}
}
public class EmoteDatabase {
private IDictionary<string, string> Emotes { get; }
public EmoteDatabase() {
Emotes = new Dictionary<string, string>();
}
public void Add(string emoteName, string emoteId) {
if (Emotes.ContainsKey(emoteName))
Emotes[emoteName] = emoteId;
public void Add(string emoteName, string emoteId)
{
if (_emotes.ContainsKey(emoteName))
_emotes[emoteName] = emoteId;
else
Emotes.Add(emoteName, emoteId);
_emotes.Add(emoteName, emoteId);
}
public void Clear() {
Emotes.Clear();
public void Clear()
{
_emotes.Clear();
}
public string? Get(string emoteName) {
return Emotes.TryGetValue(emoteName, out string? emoteId) ? emoteId : null;
public string? Get(string emoteName)
{
return _emotes.TryGetValue(emoteName, out string? emoteId) ? emoteId : null;
}
public void Remove(string emoteName) {
if (Emotes.ContainsKey(emoteName))
Emotes.Remove(emoteName);
public void Remove(string emoteName)
{
if (_emotes.ContainsKey(emoteName))
_emotes.Remove(emoteName);
}
}
public class EmoteSet {
public class EmoteSet
{
public string Id { get; set; }
public string Name { get; set; }
public int Flags { get; set; }
@ -75,7 +47,8 @@ namespace TwitchChatTTS.Seven
public int Capacity { get; set; }
}
public class Emote {
public class Emote
{
public string Id { get; set; }
public string Name { get; set; }
public int Flags { get; set; }

View File

@ -1,46 +1,59 @@
using System.Text.Json;
using TwitchChatTTS.Helpers;
using Microsoft.Extensions.Logging;
using Serilog;
using TwitchChatTTS.Seven;
using TwitchChatTTS;
public class SevenApiClient {
public class SevenApiClient
{
public static readonly string API_URL = "https://7tv.io/v3";
public static readonly string WEBSOCKET_URL = "wss://events.7tv.io/v3";
private WebClientWrap Web { get; }
private ILogger<SevenApiClient> Logger { get; }
private long? Id { get; }
private ILogger Logger { get; }
public SevenApiClient(ILogger<SevenApiClient> logger, TwitchBotToken token) {
public SevenApiClient(ILogger logger)
{
Logger = logger;
Id = long.TryParse(token?.BroadcasterId, out long id) ? id : -1;
Web = new WebClientWrap(new JsonSerializerOptions() {
Web = new WebClientWrap(new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
});
}
public async Task<EmoteDatabase?> GetSevenEmotes() {
if (Id == null)
throw new NullReferenceException(nameof(Id));
try {
var details = await Web.GetJson<UserDetails>($"{API_URL}/users/twitch/" + Id);
if (details == null)
return null;
var emotes = new EmoteDatabase();
if (details.EmoteSet != null)
foreach (var emote in details.EmoteSet.Emotes)
emotes.Add(emote.Name, emote.Id);
Logger.LogInformation($"Loaded {details.EmoteSet?.Emotes.Count() ?? 0} emotes from 7tv.");
return emotes;
} catch (JsonException e) {
Logger.LogError(e, "Failed to fetch emotes from 7tv. 2");
} catch (Exception e) {
Logger.LogError(e, "Failed to fetch emotes from 7tv.");
public async Task<EmoteSet?> FetchChannelEmoteSet(string twitchId) {
try
{
var details = await Web.GetJson<UserDetails>($"{API_URL}/users/twitch/" + twitchId);
return details?.EmoteSet;
}
catch (JsonException e)
{
Logger.Error(e, "Failed to fetch emotes from 7tv due to improper JSON.");
}
catch (Exception e)
{
Logger.Error(e, "Failed to fetch emotes from 7tv.");
}
return null;
}
public async Task<IEnumerable<Emote>?> FetchGlobalSevenEmotes()
{
try
{
var emoteSet = await Web.GetJson<EmoteSet>($"{API_URL}/emote-sets/6353512c802a0e34bac96dd2");
return emoteSet?.Emotes;
}
catch (JsonException e)
{
Logger.Error(e, "Failed to fetch emotes from 7tv due to improper JSON.");
}
catch (Exception e)
{
Logger.Error(e, "Failed to fetch emotes from 7tv.");
}
return null;
}

View File

@ -2,7 +2,7 @@ using System.Text.Json;
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using TwitchChatTTS.Seven.Socket.Data;
namespace TwitchChatTTS.Seven.Socket.Handlers
@ -11,9 +11,11 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
{
private ILogger Logger { get; }
private EmoteDatabase Emotes { get; }
private object _lock = new object();
public int OperationCode { get; set; } = 0;
public DispatchHandler(ILogger<DispatchHandler> logger, EmoteDatabase emotes) {
public DispatchHandler(ILogger logger, EmoteDatabase emotes)
{
Logger = logger;
Emotes = emotes;
}
@ -22,33 +24,82 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
{
if (message is not DispatchMessage obj || obj == null)
return;
ApplyChanges(obj?.Body?.Pulled, cf => cf.OldValue, true);
ApplyChanges(obj?.Body?.Pushed, cf => cf.Value, false);
ApplyChanges(obj?.Body?.Removed, cf => cf.OldValue, true);
ApplyChanges(obj?.Body?.Updated, cf => cf.OldValue, false, cf => cf.Value);
}
private void ApplyChanges(IEnumerable<ChangeField>? fields, Func<ChangeField, object> getter, bool removing) {
if (fields == null)
private void ApplyChanges(IEnumerable<ChangeField>? fields, Func<ChangeField, object> getter, bool removing, Func<ChangeField, object>? updater = null)
{
if (fields == null || !fields.Any() || removing && updater != null)
return;
foreach (var val in fields) {
foreach (var val in fields)
{
var value = getter(val);
if (value == null)
continue;
var o = JsonSerializer.Deserialize<EmoteField>(value.ToString(), new JsonSerializerOptions() {
var o = JsonSerializer.Deserialize<EmoteField>(value.ToString(), new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
});
if (o == null)
continue;
if (removing) {
Emotes.Remove(o.Name);
Logger.LogInformation($"Removed 7tv emote: {o.Name} (id: {o.Id})");
} else {
Emotes.Add(o.Name, o.Id);
Logger.LogInformation($"Added 7tv emote: {o.Name} (id: {o.Id})");
lock (_lock)
{
if (removing)
{
RemoveEmoteById(o.Id);
Logger.Information($"Removed 7tv emote: {o.Name} (id: {o.Id})");
}
else if (updater != null)
{
RemoveEmoteById(o.Id);
var update = updater(val);
var u = JsonSerializer.Deserialize<EmoteField>(update.ToString(), new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
});
if (u != null)
{
Emotes.Add(u.Name, u.Id);
Logger.Information($"Updated 7tv emote: from '{o.Name}' to '{u.Name}' (id: {u.Id})");
}
else
{
Logger.Warning("Failed to update 7tv emote.");
}
}
else
{
Emotes.Add(o.Name, o.Id);
Logger.Information($"Added 7tv emote: {o.Name} (id: {o.Id})");
}
}
}
}
private void RemoveEmoteById(string id)
{
string? key = null;
foreach (var e in Emotes.Emotes)
{
if (e.Value == id)
{
key = e.Key;
break;
}
}
if (key != null)
Emotes.Remove(key);
}
}
}

View File

@ -1,7 +1,7 @@
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using TwitchChatTTS.Seven.Socket.Context;
using TwitchChatTTS.Seven.Socket.Data;
@ -10,17 +10,18 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
public class EndOfStreamHandler : IWebSocketHandler
{
private ILogger Logger { get; }
private Configuration Configuration { get; }
private User User { get; }
private IServiceProvider ServiceProvider { get; }
private string[] ErrorCodes { get; }
private int[] ReconnectDelay { get; }
public int OperationCode { get; set; } = 7;
public EndOfStreamHandler(ILogger<EndOfStreamHandler> logger, Configuration configuration, IServiceProvider serviceProvider) {
public EndOfStreamHandler(ILogger logger, User user, IServiceProvider serviceProvider)
{
Logger = logger;
Configuration = configuration;
User = user;
ServiceProvider = serviceProvider;
ErrorCodes = [
@ -59,37 +60,40 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
{
if (message is not EndOfStreamMessage obj || obj == null)
return;
var code = obj.Code - 4000;
if (code >= 0 && code < ErrorCodes.Length)
Logger.LogWarning($"Received end of stream message (reason: {ErrorCodes[code]}, code: {obj.Code}, message: {obj.Message}).");
Logger.Warning($"Received end of stream message (reason: {ErrorCodes[code]}, code: {obj.Code}, message: {obj.Message}).");
else
Logger.LogWarning($"Received end of stream message (code: {obj.Code}, message: {obj.Message}).");
Logger.Warning($"Received end of stream message (code: {obj.Code}, message: {obj.Message}).");
await sender.DisconnectAsync();
if (code >= 0 && code < ReconnectDelay.Length && ReconnectDelay[code] < 0) {
Logger.LogError($"7tv client will remain disconnected due to a bad client implementation.");
if (code >= 0 && code < ReconnectDelay.Length && ReconnectDelay[code] < 0)
{
Logger.Error($"7tv client will remain disconnected due to a bad client implementation.");
return;
}
if (string.IsNullOrWhiteSpace(Configuration.Seven?.UserId))
if (string.IsNullOrWhiteSpace(User.SevenEmoteSetId))
return;
var context = ServiceProvider.GetRequiredService<ReconnectContext>();
await Task.Delay(ReconnectDelay[code]);
//var base_url = "@" + string.Join(",", Configuration.Seven.SevenId.Select(sub => sub.Type + "<" + string.Join(",", sub.Condition?.Select(e => e.Key + "=" + e.Value) ?? new string[0]) + ">"));
var base_url = $"@emote_set.*<object_id={Configuration.Seven.UserId.Trim()}>";
var base_url = $"@emote_set.*<object_id={User.SevenEmoteSetId}>";
string url = $"{SevenApiClient.WEBSOCKET_URL}{base_url}";
Logger.LogDebug($"7tv websocket reconnecting to {url}.");
Logger.Debug($"7tv websocket reconnecting to {url}.");
await sender.ConnectAsync(url);
if (context.SessionId != null) {
if (context.SessionId != null)
{
await sender.Send(34, new ResumeMessage() { SessionId = context.SessionId });
Logger.LogInformation("Resumed connection to 7tv websocket.");
} else {
Logger.LogDebug("7tv websocket session id not available.");
Logger.Information("Resumed connection to 7tv websocket.");
}
else
{
Logger.Information("Resumed connection to 7tv websocket on a different session.");
}
}
}

View File

@ -1,6 +1,6 @@
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.Logging;
using Serilog;
using TwitchChatTTS.Seven.Socket.Data;
namespace TwitchChatTTS.Seven.Socket.Handlers
@ -10,7 +10,8 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
private ILogger Logger { get; }
public int OperationCode { get; set; } = 6;
public ErrorHandler(ILogger<ErrorHandler> logger) {
public ErrorHandler(ILogger logger)
{
Logger = logger;
}

View File

@ -1,6 +1,6 @@
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.Logging;
using Serilog;
using TwitchChatTTS.Seven.Socket.Data;
namespace TwitchChatTTS.Seven.Socket.Handlers
@ -10,7 +10,8 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
private ILogger Logger { get; }
public int OperationCode { get; set; } = 4;
public ReconnectHandler(ILogger<ReconnectHandler> logger) {
public ReconnectHandler(ILogger logger)
{
Logger = logger;
}
@ -19,7 +20,7 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
if (message is not ReconnectMessage obj || obj == null)
return;
Logger.LogInformation($"7tv server wants us to reconnect (reason: {obj.Reason}).");
Logger.Information($"7tv server wants us to reconnect (reason: {obj.Reason}).");
}
}
}

View File

@ -1,6 +1,6 @@
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.Logging;
using Serilog;
using TwitchChatTTS.Seven.Socket.Data;
namespace TwitchChatTTS.Seven.Socket.Handlers
@ -11,7 +11,8 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
private Configuration Configuration { get; }
public int OperationCode { get; set; } = 1;
public SevenHelloHandler(ILogger<SevenHelloHandler> logger, Configuration configuration) {
public SevenHelloHandler(ILogger logger, Configuration configuration)
{
Logger = logger;
Configuration = configuration;
}
@ -23,10 +24,10 @@ namespace TwitchChatTTS.Seven.Socket.Handlers
if (sender is not SevenSocketClient seven || seven == null)
return;
seven.Connected = true;
seven.ConnectionDetails = obj;
Logger.LogInformation("Connected to 7tv websockets.");
Logger.Information("Connected to 7tv websockets.");
}
}
}

View File

@ -1,4 +1,4 @@
using Microsoft.Extensions.Logging;
using Serilog;
using CommonSocketLibrary.Socket.Manager;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.DependencyInjection;
@ -7,28 +7,34 @@ namespace TwitchChatTTS.Seven.Socket.Managers
{
public class SevenHandlerManager : WebSocketHandlerManager
{
public SevenHandlerManager(ILogger<SevenHandlerManager> logger, IServiceProvider provider) : base(logger) {
try {
public SevenHandlerManager(ILogger logger, IServiceProvider provider) : base(logger)
{
try
{
var basetype = typeof(IWebSocketHandler);
var assembly = GetType().Assembly;
var types = assembly.GetTypes().Where(t => t.IsClass && basetype.IsAssignableFrom(t) && t.AssemblyQualifiedName?.Contains(".Seven.") == true);
foreach (var type in types) {
foreach (var type in types)
{
var key = "7tv-" + type.Name.Replace("Handlers", "Hand#lers")
.Replace("Handler", "")
.Replace("Hand#lers", "Handlers")
.ToLower();
var handler = provider.GetKeyedService<IWebSocketHandler>(key);
if (handler == null) {
logger.LogError("Failed to find 7tv websocket handler: " + type.AssemblyQualifiedName);
if (handler == null)
{
logger.Error("Failed to find 7tv websocket handler: " + type.AssemblyQualifiedName);
continue;
}
Logger.LogDebug($"Linked type {type.AssemblyQualifiedName} to 7tv websocket handler {handler.GetType().AssemblyQualifiedName}.");
Logger.Debug($"Linked type {type.AssemblyQualifiedName} to 7tv websocket handler {handler.GetType().AssemblyQualifiedName}.");
Add(handler);
}
} catch (Exception e) {
Logger.LogError(e, "Failed to load 7tv websocket handler types.");
}
catch (Exception e)
{
Logger.Error(e, "Failed to load 7tv websocket handler types.");
}
}
}

View File

@ -2,14 +2,14 @@ using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using CommonSocketLibrary.Socket.Manager;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
namespace TwitchChatTTS.Seven.Socket.Managers
{
public class SevenHandlerTypeManager : WebSocketHandlerTypeManager
{
public SevenHandlerTypeManager(
ILogger<SevenHandlerTypeManager> factory,
ILogger factory,
[FromKeyedServices("7tv")] HandlerManager<WebSocketClient,
IWebSocketHandler> handlers
) : base(factory, handlers)

View File

@ -1,23 +1,26 @@
using CommonSocketLibrary.Common;
using CommonSocketLibrary.Abstract;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Serilog;
using TwitchChatTTS.Seven.Socket.Data;
using System.Text.Json;
namespace TwitchChatTTS.Seven.Socket
{
public class SevenSocketClient : WebSocketClient {
public class SevenSocketClient : WebSocketClient
{
public SevenHelloMessage? ConnectionDetails { get; set; }
public SevenSocketClient(
ILogger<SevenSocketClient> logger,
ILogger logger,
[FromKeyedServices("7tv")] HandlerManager<WebSocketClient, IWebSocketHandler> handlerManager,
[FromKeyedServices("7tv")] HandlerTypeManager<WebSocketClient, IWebSocketHandler> typeManager
) : base(logger, handlerManager, typeManager, new JsonSerializerOptions() {
) : base(logger, handlerManager, typeManager, new JsonSerializerOptions()
{
PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
}) {
})
{
ConnectionDetails = null;
}
}

View File

@ -8,5 +8,12 @@ namespace TwitchChatTTS.Seven
public int EmoteCapacity { get; set; }
public int? EmoteSetId { get; set; }
public EmoteSet EmoteSet { get; set; }
public SevenUser User { get; set; }
}
public class SevenUser
{
public string Id { get; set; }
public string Username { get; set; }
}
}