Cleaned up code
This commit is contained in:
parent
36ae613736
commit
9fd43502f5
@ -1,26 +1,23 @@
|
||||
using TwitchChatTTS.Helpers;
|
||||
using TwitchChatTTS;
|
||||
using System.Text.Json;
|
||||
using HermesSocketLibrary.Requests.Messages;
|
||||
using TwitchChatTTS.Hermes;
|
||||
using Serilog;
|
||||
|
||||
public class HermesApiClient
|
||||
{
|
||||
private readonly TwitchBotAuth _token;
|
||||
private readonly WebClientWrap _web;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
public const string BASE_URL = "tomtospeech.com";
|
||||
|
||||
public HermesApiClient(TwitchBotAuth token, Configuration configuration, ILogger logger)
|
||||
public HermesApiClient(Configuration configuration, ILogger logger)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(configuration.Hermes?.Token))
|
||||
{
|
||||
throw new Exception("Ensure you have written your API key in \".token\" file, in the same folder as this application.");
|
||||
}
|
||||
|
||||
_token = token;
|
||||
_web = new WebClientWrap(new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = false,
|
||||
@ -30,42 +27,6 @@ public class HermesApiClient
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
|
||||
public async Task<TwitchBotAuth?> AuthorizeTwitch()
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.Debug($"Attempting to authorize Twitch API...");
|
||||
var authorize = await _web.GetJson<TwitchBotAuth>($"https://{HermesApiClient.BASE_URL}/api/account/reauthorize");
|
||||
if (authorize != null)
|
||||
{
|
||||
_token.AccessToken = authorize.AccessToken;
|
||||
_token.RefreshToken = authorize.RefreshToken;
|
||||
_token.UserId = authorize.UserId;
|
||||
_token.BroadcasterId = authorize.BroadcasterId;
|
||||
_token.ExpiresIn = authorize.ExpiresIn;
|
||||
_token.UpdatedAt = DateTime.Now;
|
||||
_logger.Information("Updated Twitch API tokens.");
|
||||
_logger.Debug($"Twitch API Auth data [user id: {_token.UserId}][id: {_token.BroadcasterId}][expires in: {_token.ExpiresIn}][expires at: {_token.ExpiresAt.ToShortTimeString()}]");
|
||||
}
|
||||
else if (authorize != null)
|
||||
{
|
||||
_logger.Error("Twitch API Authorization failed: " + authorize.AccessToken + " | " + authorize.RefreshToken + " | " + authorize.UserId + " | " + authorize.BroadcasterId);
|
||||
return null;
|
||||
}
|
||||
return _token;
|
||||
}
|
||||
catch (JsonException)
|
||||
{
|
||||
_logger.Debug($"Failed to Authorize Twitch API due to JSON error.");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Failed to authorize to Twitch API.");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public async Task<TTSVersion?> GetLatestTTSVersion()
|
||||
{
|
||||
return await _web.GetJson<TTSVersion>($"https://{BASE_URL}/api/info/version");
|
||||
@ -81,110 +42,4 @@ public class HermesApiClient
|
||||
throw new NullReferenceException("Invalid value found while fetching for hermes account data.");
|
||||
return account;
|
||||
}
|
||||
|
||||
public async Task<TwitchBotToken> FetchTwitchBotToken()
|
||||
{
|
||||
var token = await _web.GetJson<TwitchBotToken>($"https://{BASE_URL}/api/token/bot");
|
||||
if (token == null || token.ClientId == null || token.AccessToken == null || token.RefreshToken == null || token.ClientSecret == null)
|
||||
throw new Exception("Failed to fetch Twitch API token from Hermes.");
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
public async Task<string> FetchTTSDefaultVoice()
|
||||
{
|
||||
var data = await _web.GetJson<string>($"https://{BASE_URL}/api/settings/tts/default");
|
||||
if (data == null)
|
||||
throw new Exception("Failed to fetch TTS default voice from Hermes.");
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TTSChatterSelectedVoice>> FetchTTSChatterSelectedVoices()
|
||||
{
|
||||
var voices = await _web.GetJson<IEnumerable<TTSChatterSelectedVoice>>($"https://{BASE_URL}/api/settings/tts/selected");
|
||||
if (voices == null)
|
||||
throw new Exception("Failed to fetch TTS chatter selected voices from Hermes.");
|
||||
|
||||
return voices;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<string>> FetchTTSEnabledVoices()
|
||||
{
|
||||
var voices = await _web.GetJson<IEnumerable<string>>($"https://{BASE_URL}/api/settings/tts");
|
||||
if (voices == null)
|
||||
throw new Exception("Failed to fetch TTS enabled voices from Hermes.");
|
||||
|
||||
return voices;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<TTSWordFilter>> FetchTTSWordFilters()
|
||||
{
|
||||
var filters = await _web.GetJson<IEnumerable<TTSWordFilter>>($"https://{BASE_URL}/api/settings/tts/filter/words");
|
||||
if (filters == null)
|
||||
throw new Exception("Failed to fetch TTS word filters from Hermes.");
|
||||
|
||||
return filters;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Redemption>> FetchRedemptions()
|
||||
{
|
||||
var redemptions = await _web.GetJson<IEnumerable<Redemption>>($"https://{BASE_URL}/api/settings/redemptions", new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = false,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
});
|
||||
if (redemptions == null)
|
||||
throw new Exception("Failed to fetch redemptions from Hermes.");
|
||||
|
||||
return redemptions;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Group>> FetchGroups()
|
||||
{
|
||||
var groups = await _web.GetJson<IEnumerable<Group>>($"https://{BASE_URL}/api/settings/groups", new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = false,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
});
|
||||
if (groups == null)
|
||||
throw new Exception("Failed to fetch groups from Hermes.");
|
||||
|
||||
return groups;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<GroupChatter>> FetchGroupChatters()
|
||||
{
|
||||
var chatters = await _web.GetJson<IEnumerable<GroupChatter>>($"https://{BASE_URL}/api/settings/groups/users", new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = false,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
});
|
||||
if (chatters == null)
|
||||
throw new Exception("Failed to fetch groups from Hermes.");
|
||||
|
||||
return chatters;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<GroupPermission>> FetchGroupPermissions()
|
||||
{
|
||||
var permissions = await _web.GetJson<IEnumerable<GroupPermission>>($"https://{BASE_URL}/api/settings/groups/permissions", new JsonSerializerOptions()
|
||||
{
|
||||
PropertyNameCaseInsensitive = false,
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
|
||||
});
|
||||
if (permissions == null)
|
||||
throw new Exception("Failed to fetch group permissions from Hermes.");
|
||||
|
||||
return permissions;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<RedeemableAction>> FetchRedeemableActions()
|
||||
{
|
||||
var actions = await _web.GetJson<IEnumerable<RedeemableAction>>($"https://{BASE_URL}/api/settings/redemptions/actions");
|
||||
if (actions == null)
|
||||
throw new Exception("Failed to fetch redeemable actions from Hermes.");
|
||||
|
||||
return actions;
|
||||
}
|
||||
}
|
@ -1,21 +0,0 @@
|
||||
public class TwitchBotAuth
|
||||
{
|
||||
public string? UserId { get; set; }
|
||||
public string? AccessToken { get; set; }
|
||||
public string? RefreshToken { get; set; }
|
||||
public string? BroadcasterId { get; set; }
|
||||
public long? ExpiresIn
|
||||
{
|
||||
get => _expiresIn;
|
||||
set
|
||||
{
|
||||
_expiresIn = value;
|
||||
if (value != null)
|
||||
ExpiresAt = DateTime.UtcNow + TimeSpan.FromSeconds((double) value);
|
||||
}
|
||||
}
|
||||
public DateTime ExpiresAt { get; set; }
|
||||
public DateTime UpdatedAt { get; set; }
|
||||
|
||||
private long? _expiresIn;
|
||||
}
|
@ -32,6 +32,7 @@ using CommonSocketLibrary.Backoff;
|
||||
using TwitchChatTTS.Chat.Speech;
|
||||
using TwitchChatTTS.Chat.Messaging;
|
||||
using TwitchChatTTS.Chat.Observers;
|
||||
using TwitchChatTTS.Chat.Commands.Limits;
|
||||
|
||||
// dotnet publish -r linux-x64 -p:PublishSingleFile=true --self-contained true
|
||||
// dotnet publish -r win-x64 -p:PublishSingleFile=true --self-contained true
|
||||
@ -85,7 +86,6 @@ s.AddSingleton<ICommandManager, CommandManager>();
|
||||
s.AddSingleton<TTSPlayer>();
|
||||
s.AddSingleton<IRedemptionManager, RedemptionManager>();
|
||||
s.AddSingleton<HermesApiClient>();
|
||||
s.AddSingleton<TwitchBotAuth>();
|
||||
s.AddSingleton<TwitchApiClient>();
|
||||
|
||||
s.AddSingleton<SevenApiClient>();
|
||||
@ -94,6 +94,8 @@ s.AddSingleton<IEmoteDatabase, EmoteDatabase>();
|
||||
s.AddSingleton<TTSConsumer>();
|
||||
s.AddSingleton<TTSPublisher>();
|
||||
|
||||
s.AddSingleton<ChatMessageReader>();
|
||||
|
||||
// OBS websocket
|
||||
s.AddKeyedSingleton<IWebSocketHandler, HelloHandler>("obs");
|
||||
s.AddKeyedSingleton<IWebSocketHandler, IdentifiedHandler>("obs");
|
||||
@ -118,8 +120,6 @@ s.AddKeyedSingleton<SocketClient<WebSocketMessage>, SevenSocketClient>("7tv");
|
||||
// Nightbot
|
||||
s.AddSingleton<NightbotApiClient>();
|
||||
|
||||
s.AddSingleton<ChatMessageReader>();
|
||||
|
||||
// twitch websocket
|
||||
s.AddKeyedSingleton<IBackoff>("twitch", new ExponentialBackoff(1000, 120 * 1000));
|
||||
s.AddSingleton<ITwitchConnectionManager, TwitchConnectionManager>();
|
||||
|
@ -110,7 +110,7 @@ namespace TwitchChatTTS
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Failed to fetch TTS message for ");
|
||||
_logger.Error(e, $"Failed to fetch TTS message [message: {message.Message.Trim()}][chatter id: {group.ChatterId}].");
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -44,9 +44,9 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
|
||||
_messageTypes.Add("channel.subscription.gift", typeof(ChannelSubscriptionGiftMessage));
|
||||
}
|
||||
|
||||
public async Task Execute(TwitchWebsocketClient sender, object data)
|
||||
public Task Execute(TwitchWebsocketClient sender, object data)
|
||||
{
|
||||
Task.Run(async () =>
|
||||
return Task.Run(() =>
|
||||
{
|
||||
if (sender == null)
|
||||
return;
|
||||
@ -66,6 +66,7 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
|
||||
}
|
||||
|
||||
var d = JsonSerializer.Deserialize(message.Event.ToString()!, type, _options);
|
||||
ArgumentNullException.ThrowIfNull(d);
|
||||
Task.Run(async () => await handler.Execute(sender, d));
|
||||
});
|
||||
}
|
||||
|
@ -78,21 +78,19 @@ namespace TwitchChatTTS.Twitch.Socket
|
||||
{
|
||||
if (_identified?.UID == client.UID)
|
||||
{
|
||||
_logger.Warning($"Identified Twitch client has disconnected [client: {client.UID}][main: {_identified.UID}][backup: {_backup?.UID}]");
|
||||
_logger.Debug($"Identified Twitch client has disconnected [client: {client.UID}][main: {_identified.UID}][backup: {_backup?.UID}]");
|
||||
_identified = null;
|
||||
reconnecting = true;
|
||||
}
|
||||
else if (_backup?.UID == client.UID)
|
||||
{
|
||||
_logger.Warning($"Backup Twitch client has disconnected [client: {client.UID}][main: {_identified?.UID}][backup: {_backup.UID}]");
|
||||
_logger.Debug($"Backup Twitch client has disconnected [client: {client.UID}][main: {_identified?.UID}][backup: {_backup.UID}]");
|
||||
_backup = null;
|
||||
}
|
||||
else if (client.ReceivedReconnecting)
|
||||
{
|
||||
_logger.Debug($"Twitch client disconnected due to reconnection [client: {client.UID}][main: {_identified?.UID}][backup: {_backup?.UID}]");
|
||||
}
|
||||
else
|
||||
_logger.Error($"Twitch client disconnected from unknown source [client: {client.UID}][main: {_identified?.UID}][backup: {_backup?.UID}]");
|
||||
_logger.Warning($"Twitch client disconnected from unknown source [client: {client.UID}][main: {_identified?.UID}][backup: {_backup?.UID}]");
|
||||
}
|
||||
|
||||
if (reconnecting)
|
||||
@ -111,7 +109,7 @@ namespace TwitchChatTTS.Twitch.Socket
|
||||
{
|
||||
if (_backup != null && _backup.UID == client.UID)
|
||||
{
|
||||
_logger.Information($"Twitch client has been identified [client: {client.UID}][main: {_identified?.UID}][backup: {_backup.UID}]");
|
||||
_logger.Debug($"Twitch client has been identified [client: {client.UID}][main: {_identified?.UID}][backup: {_backup.UID}]");
|
||||
_identified = _backup;
|
||||
_backup = null;
|
||||
}
|
||||
@ -120,7 +118,7 @@ namespace TwitchChatTTS.Twitch.Socket
|
||||
}
|
||||
else if (_identified.UID == client.UID)
|
||||
{
|
||||
_logger.Warning($"Twitch client has been re-identified [client: {client.UID}][main: {_identified.UID}][backup: {_backup?.UID}]");
|
||||
_logger.Debug($"Twitch client has been re-identified [client: {client.UID}][main: {_identified.UID}][backup: {_backup?.UID}]");
|
||||
}
|
||||
else if (_backup == null || _backup.UID != client.UID)
|
||||
{
|
||||
|
@ -17,7 +17,6 @@ namespace TwitchChatTTS.Twitch.Socket
|
||||
private readonly IDictionary<string, string> _subscriptions;
|
||||
private readonly IBackoff _backoff;
|
||||
private readonly Configuration _configuration;
|
||||
private DateTime _lastReceivedMessageTimestamp;
|
||||
private bool _disconnected;
|
||||
private readonly object _lock;
|
||||
|
||||
@ -157,8 +156,6 @@ namespace TwitchChatTTS.Twitch.Socket
|
||||
return;
|
||||
}
|
||||
|
||||
_lastReceivedMessageTimestamp = DateTime.UtcNow;
|
||||
|
||||
string content = message.Payload?.ToString() ?? string.Empty;
|
||||
if (message.Metadata.MessageType != "session_keepalive")
|
||||
_logger.Debug("Twitch RX #" + message.Metadata.MessageType + ": " + content);
|
||||
|
Loading…
Reference in New Issue
Block a user