Cleaned code up. Added OBS & 7tv ws support. Added dependency injection. App loads from yml file.

This commit is contained in:
Tom
2024-03-12 18:05:27 +00:00
parent 9cd6725570
commit b5cc6b5706
66 changed files with 1795 additions and 456 deletions

84
Seven/Emotes.cs Normal file
View File

@ -0,0 +1,84 @@
using System.Collections.Concurrent;
namespace TwitchChatTTS.Seven
{
public class EmoteCounter {
public IDictionary<string, IDictionary<long, int>> Counters { get; set; }
public EmoteCounter() {
Counters = new ConcurrentDictionary<string, IDictionary<long, int>>();
}
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;
else
Emotes.Add(emoteName, emoteId);
}
public void Clear() {
Emotes.Clear();
}
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 class EmoteSet {
public string Id { get; set; }
public string Name { get; set; }
public int Flags { get; set; }
public bool Immutable { get; set; }
public bool Privileged { get; set; }
public IList<Emote> Emotes { get; set; }
public int EmoteCount { get; set; }
public int Capacity { get; set; }
}
public class Emote {
public string Id { get; set; }
public string Name { get; set; }
public int Flags { get; set; }
}
}

47
Seven/SevenApiClient.cs Normal file
View File

@ -0,0 +1,47 @@
using System.Text.Json;
using TwitchChatTTS.Helpers;
using Microsoft.Extensions.Logging;
using TwitchChatTTS;
using TwitchChatTTS.Seven;
public class SevenApiClient {
private WebClientWrap Web { get; }
private Configuration Configuration { get; }
private ILogger<SevenApiClient> Logger { get; }
private long? Id { get; }
public SevenApiClient(Configuration configuration, ILogger<SevenApiClient> logger, TwitchBotToken token) {
Configuration = configuration;
Logger = logger;
Id = long.TryParse(token?.BroadcasterId, out long id) ? id : -1;
Web = new WebClientWrap(new JsonSerializerOptions() {
PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
});
}
public async Task<EmoteDatabase?> GetSevenEmotes() {
if (Id is null)
throw new NullReferenceException(nameof(Id));
try {
var details = await Web.GetJson<UserDetails>("https://7tv.io/v3/users/twitch/" + Id);
if (details is null)
return null;
var emotes = new EmoteDatabase();
if (details.EmoteSet is not 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.");
}
return null;
}
}

View File

@ -0,0 +1,9 @@
namespace TwitchChatTTS.Seven.Socket.Context
{
public class ReconnectContext
{
public string? Protocol;
public string Url;
public string? SessionId;
}
}

View File

@ -0,0 +1,12 @@
namespace TwitchChatTTS.Seven.Socket.Context
{
public class SevenHelloContext
{
public IEnumerable<SevenSubscriptionConfiguration>? Subscriptions;
}
public class SevenSubscriptionConfiguration {
public string? Type;
public IDictionary<string, string>? Condition;
}
}

View File

@ -0,0 +1,30 @@
namespace TwitchChatTTS.Seven.Socket.Data
{
public class ChangeMapMessage
{
public object Id { get; set; }
public byte Kind { get; set; }
public bool? Contextual { get; set; }
public object Actor { get; set; }
public IEnumerable<ChangeField>? Added { get; set; }
public IEnumerable<ChangeField>? Updated { get; set; }
public IEnumerable<ChangeField>? Removed { get; set; }
public IEnumerable<ChangeField>? Pushed { get; set; }
public IEnumerable<ChangeField>? Pulled { get; set; }
}
public class ChangeField {
public string Key { get; set; }
public int? Index { get; set; }
public bool Nested { get; set; }
public object OldValue { get; set; }
public object Value { get; set; }
}
public class EmoteField {
public string Id { get; set; }
public string Name { get; set; }
public string ActorId { get; set; }
public int Flags { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace TwitchChatTTS.Seven.Socket.Data
{
public class DispatchMessage
{
public object EventType { get; set; }
public ChangeMapMessage Body { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace TwitchChatTTS.Seven.Socket.Data
{
public class EndOfStreamMessage
{
public int Code { get; set; }
public string Message { get; set; }
}
}

View File

@ -0,0 +1,7 @@
namespace TwitchChatTTS.Seven.Socket.Data
{
public class ErrorMessage
{
}
}

View File

@ -0,0 +1,7 @@
namespace TwitchChatTTS.Seven.Socket.Data
{
public class IdentifyMessage
{
}
}

View File

@ -0,0 +1,7 @@
namespace TwitchChatTTS.Seven.Socket.Data
{
public class ReconnectMessage
{
public string Reason { get; set; }
}
}

View File

@ -0,0 +1,7 @@
namespace TwitchChatTTS.Seven.Socket.Data
{
public class ResumeMessage
{
public string SessionId { get; set; }
}
}

View File

@ -0,0 +1,9 @@
namespace TwitchChatTTS.Seven.Socket.Data
{
public class SevenHelloMessage
{
public uint HeartbeatInterval { get; set; }
public string SessionId { get; set; }
public int SubscriptionLimit { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace TwitchChatTTS.Seven.Socket.Data
{
public class SubscribeMessage
{
public string? Type { get; set; }
public IDictionary<string, string>? Condition { get; set; }
}
}

View File

@ -0,0 +1,8 @@
namespace TwitchChatTTS.Seven.Socket.Data
{
public class UnsubscribeMessage
{
public string Type { get; set; }
public IDictionary<string, string>? Condition { get; set; }
}
}

View File

@ -0,0 +1,46 @@
using System.Text.Json;
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using TwitchChatTTS.Seven.Socket.Data;
namespace TwitchChatTTS.Seven.Socket.Handlers
{
public class DispatchHandler : IWebSocketHandler
{
private ILogger Logger { get; }
private IServiceProvider ServiceProvider { get; }
public int OperationCode { get; set; } = 0;
public DispatchHandler(ILogger<DispatchHandler> logger, IServiceProvider serviceProvider) {
Logger = logger;
ServiceProvider = serviceProvider;
}
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
{
if (message is not DispatchMessage obj || obj == null)
return;
Do(obj?.Body?.Pulled, cf => cf.OldValue);
Do(obj?.Body?.Pushed, cf => cf.Value);
}
private void Do(IEnumerable<ChangeField>? fields, Func<ChangeField, object> getter) {
if (fields is null)
return;
//ServiceProvider.GetRequiredService<EmoteDatabase>()
foreach (var val in fields) {
if (getter(val) == null)
continue;
var o = JsonSerializer.Deserialize<EmoteField>(val.OldValue.ToString(), new JsonSerializerOptions() {
PropertyNameCaseInsensitive = false,
PropertyNamingPolicy = JsonNamingPolicy.SnakeCaseLower
});
}
}
}
}

View File

@ -0,0 +1,88 @@
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using TwitchChatTTS.Seven.Socket.Context;
using TwitchChatTTS.Seven.Socket.Data;
namespace TwitchChatTTS.Seven.Socket.Handlers
{
public class EndOfStreamHandler : IWebSocketHandler
{
private ILogger Logger { get; }
private IServiceProvider ServiceProvider { get; }
private string[] ErrorCodes { get; }
private int[] ReconnectDelay { get; }
public int OperationCode { get; set; } = 7;
public EndOfStreamHandler(ILogger<EndOfStreamHandler> logger, IServiceProvider serviceProvider) {
Logger = logger;
ServiceProvider = serviceProvider;
ErrorCodes = [
"Server Error",
"Unknown Operation",
"Invalid Payload",
"Auth Failure",
"Already Identified",
"Rate Limited",
"Restart",
"Maintenance",
"Timeout",
"Already Subscribed",
"Not Subscribed",
"Insufficient Privilege",
"Inactivity?"
];
ReconnectDelay = [
1000,
-1,
-1,
-1,
-1,
3000,
1000,
300000,
1000,
-1,
-1,
1000,
1000
];
}
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
{
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}).");
else
Logger.LogWarning($"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.");
return;
}
var context = ServiceProvider.GetRequiredService<ReconnectContext>();
await Task.Delay(ReconnectDelay[code]);
Logger.LogInformation($"7tv client reconnecting.");
await sender.ConnectAsync($"{context.Protocol ?? "wss"}://{context.Url}");
if (context.SessionId is null) {
await sender.Send(33, new object());
} else {
await sender.Send(34, new ResumeMessage() {
SessionId = context.SessionId
});
}
}
}
}

View File

@ -0,0 +1,23 @@
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.Logging;
using TwitchChatTTS.Seven.Socket.Data;
namespace TwitchChatTTS.Seven.Socket.Handlers
{
public class ErrorHandler : IWebSocketHandler
{
private ILogger Logger { get; }
public int OperationCode { get; set; } = 6;
public ErrorHandler(ILogger<ErrorHandler> logger) {
Logger = logger;
}
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
{
if (message is not ErrorMessage obj || obj == null)
return;
}
}
}

View File

@ -0,0 +1,25 @@
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.Logging;
using TwitchChatTTS.Seven.Socket.Data;
namespace TwitchChatTTS.Seven.Socket.Handlers
{
public class ReconnectHandler : IWebSocketHandler
{
private ILogger Logger { get; }
public int OperationCode { get; set; } = 4;
public ReconnectHandler(ILogger<ReconnectHandler> logger) {
Logger = logger;
}
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
{
if (message is not ReconnectMessage obj || obj == null)
return;
Logger.LogInformation($"7tv server wants us to reconnect (reason: {obj.Reason}).");
}
}
}

View File

@ -0,0 +1,56 @@
using CommonSocketLibrary.Abstract;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.Logging;
using TwitchChatTTS.Seven.Socket.Context;
using TwitchChatTTS.Seven.Socket.Data;
namespace TwitchChatTTS.Seven.Socket.Handlers
{
public class SevenHelloHandler : IWebSocketHandler
{
private ILogger Logger { get; }
private SevenHelloContext Context { get; }
public int OperationCode { get; set; } = 1;
public SevenHelloHandler(ILogger<SevenHelloHandler> logger, SevenHelloContext context) {
Logger = logger;
Context = context;
}
public async Task Execute<Data>(SocketClient<WebSocketMessage> sender, Data message)
{
if (message is not SevenHelloMessage obj || obj == null)
return;
if (sender is not SevenSocketClient seven || seven == null)
return;
seven.Connected = true;
seven.ConnectionDetails = obj;
// if (Context.Subscriptions == null || !Context.Subscriptions.Any()) {
// Logger.LogWarning("No subscriptions have been set for the 7tv websocket client.");
// return;
// }
//await Task.Delay(TimeSpan.FromMilliseconds(1000));
//await sender.Send(33, new IdentifyMessage());
//await Task.Delay(TimeSpan.FromMilliseconds(5000));
//await sender.SendRaw("{\"op\":35,\"d\":{\"type\":\"emote_set.*\",\"condition\":{\"object_id\":\"64505914b9fc508169ffe7cc\"}}}");
//await sender.SendRaw(File.ReadAllText("test.txt"));
// foreach (var sub in Context.Subscriptions) {
// if (string.IsNullOrWhiteSpace(sub.Type)) {
// Logger.LogWarning("Non-existent or empty subscription type found on the 7tv websocket client.");
// continue;
// }
// Logger.LogDebug($"Subscription Type: {sub.Type} | Condition: {string.Join(", ", sub.Condition?.Select(e => e.Key + "=" + e.Value) ?? new string[0])}");
// await sender.Send(35, new SubscribeMessage() {
// Type = sub.Type,
// Condition = sub.Condition
// });
// }
}
}
}

View File

@ -0,0 +1,35 @@
using Microsoft.Extensions.Logging;
using CommonSocketLibrary.Socket.Manager;
using CommonSocketLibrary.Common;
using Microsoft.Extensions.DependencyInjection;
namespace TwitchChatTTS.Seven.Socket.Manager
{
public class SevenHandlerManager : WebSocketHandlerManager
{
public SevenHandlerManager(ILogger<SevenHandlerManager> 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) {
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);
continue;
}
Logger.LogDebug($"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.");
}
}
}
}

View File

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

View File

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

12
Seven/UserDetails.cs Normal file
View File

@ -0,0 +1,12 @@
namespace TwitchChatTTS.Seven
{
public class UserDetails
{
public string Id { get; set; }
public string Platform { get; set; }
public string Username { get; set; }
public int EmoteCapacity { get; set; }
public int? EmoteSetId { get; set; }
public EmoteSet EmoteSet { get; set; }
}
}