Twitch connection now relies on events to connect. Added logging for when TTS filter is not a regex. Minor code clean up.
This commit is contained in:
104
TTS.cs
104
TTS.cs
@@ -1,7 +1,6 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Serilog;
|
||||
using NAudio.Wave.SampleProviders;
|
||||
using org.mariuszgromada.math.mxparser;
|
||||
using TwitchChatTTS.Hermes.Socket;
|
||||
using TwitchChatTTS.Seven.Socket;
|
||||
@@ -13,10 +12,10 @@ using TwitchChatTTS.Twitch.Socket.Messages;
|
||||
using TwitchChatTTS.Twitch.Socket;
|
||||
using TwitchChatTTS.Chat.Commands;
|
||||
using System.Text;
|
||||
using TwitchChatTTS.Chat.Speech;
|
||||
using TwitchChatTTS.Veadotube;
|
||||
using TwitchChatTTS.Bus;
|
||||
using System.Reactive.Linq;
|
||||
using System.Net.WebSockets;
|
||||
|
||||
namespace TwitchChatTTS
|
||||
{
|
||||
@@ -36,8 +35,6 @@ namespace TwitchChatTTS
|
||||
private readonly ICommandFactory _commandFactory;
|
||||
private readonly ICommandManager _commandManager;
|
||||
private readonly IEmoteDatabase _emotes;
|
||||
private readonly TTSPlayer _player;
|
||||
private readonly AudioPlaybackEngine _playback;
|
||||
private readonly ServiceBusCentral _bus;
|
||||
private readonly Configuration _configuration;
|
||||
private readonly ILogger _logger;
|
||||
@@ -54,8 +51,6 @@ namespace TwitchChatTTS
|
||||
ICommandFactory commandFactory,
|
||||
ICommandManager commandManager,
|
||||
IEmoteDatabase emotes,
|
||||
TTSPlayer player,
|
||||
AudioPlaybackEngine playback,
|
||||
ServiceBusCentral bus,
|
||||
Configuration configuration,
|
||||
ILogger logger
|
||||
@@ -72,8 +67,6 @@ namespace TwitchChatTTS
|
||||
_commandFactory = commandFactory;
|
||||
_commandManager = commandManager;
|
||||
_emotes = emotes;
|
||||
_player = player;
|
||||
_playback = playback;
|
||||
_bus = bus;
|
||||
_configuration = configuration;
|
||||
_logger = logger;
|
||||
@@ -87,7 +80,7 @@ namespace TwitchChatTTS
|
||||
|
||||
if (string.IsNullOrWhiteSpace(_configuration.Hermes?.Token))
|
||||
{
|
||||
_logger.Error("Hermes API token not set in the configuration file.");
|
||||
_logger.Error("Tom to Speech API token not set in the yml file.");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -113,56 +106,70 @@ namespace TwitchChatTTS
|
||||
_logger.Warning("Failed to check for version updates.");
|
||||
}
|
||||
|
||||
var disposables = new List<IDisposable>();
|
||||
|
||||
// 7tv
|
||||
var twitchTopic = _bus.GetTopic("twitch id");
|
||||
twitchTopic.FirstAsync().Subscribe(async (data) =>
|
||||
var connected = _bus.GetTopic("tts_connected");
|
||||
disposables.Add(connected.FirstAsync().Subscribe(async (data) =>
|
||||
{
|
||||
var twitchId = data.Value?.ToString();
|
||||
if (twitchId == null)
|
||||
if (data.Value is not User user)
|
||||
{
|
||||
_logger.Warning("Something went wrong. Unable to fetch 7tv data.");
|
||||
return;
|
||||
}
|
||||
if (user.TwitchUserId == default)
|
||||
{
|
||||
_logger.Warning("Unable to fetch 7tv data. If this is wrong, ensure your Tom to Speech token is valid.");
|
||||
return;
|
||||
}
|
||||
|
||||
var emoteSet = await _sevenApiClient.FetchChannelEmoteSet(_user.TwitchUserId);
|
||||
if (emoteSet != null)
|
||||
{
|
||||
_user.SevenEmoteSetId = emoteSet.Id;
|
||||
_logger.Debug($"Fetched the 7tv emote set id [emote set id: {emoteSet.Id}]");
|
||||
}
|
||||
|
||||
await InitializeEmotes(_sevenApiClient, emoteSet);
|
||||
await InitializeSevenTv();
|
||||
});
|
||||
}));
|
||||
|
||||
await InitializeHermesWebsocket();
|
||||
|
||||
_playback.AddOnMixerInputEnded((object? s, SampleProviderEventArgs e) =>
|
||||
disposables.Add(connected.FirstAsync().Subscribe(async (data) =>
|
||||
{
|
||||
if (_player.Playing?.Audio == e.SampleProvider)
|
||||
if (data.Value is not User user)
|
||||
{
|
||||
_player.Playing = null;
|
||||
_logger.Warning("Something went wrong. Not connecting to Twitch.");
|
||||
return;
|
||||
}
|
||||
if (user.TwitchUserId == default)
|
||||
{
|
||||
_logger.Warning("Not connecting to Twitch. If this is wrong, ensure your Tom to Speech token is valid.");
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
try
|
||||
{
|
||||
_veado.Initialize();
|
||||
await _veado.Connect();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Warning(e, "Failed to connect to Veado websocket server.");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await _twitch.Connect();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Failed to connect to Twitch websocket server.");
|
||||
await Task.Delay(TimeSpan.FromSeconds(30));
|
||||
return;
|
||||
}
|
||||
try
|
||||
{
|
||||
await _twitch.Connect();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Failed to connect to Twitch websocket server.");
|
||||
}
|
||||
}));
|
||||
|
||||
_commandManager.Update(_commandFactory);
|
||||
|
||||
await InitializeVeadotube();
|
||||
await InitializeHermesWebsocket();
|
||||
await InitializeObs();
|
||||
|
||||
// Check if user has successfully connected.
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(TimeSpan.FromSeconds(5));
|
||||
if (_user.TwitchUserId == default)
|
||||
_logger.Warning("Ensure your Tom to Speech token in the tts.config.yml file is valid.");
|
||||
});
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken)
|
||||
@@ -181,6 +188,10 @@ namespace TwitchChatTTS
|
||||
_hermes.Initialize();
|
||||
await _hermes.Connect();
|
||||
}
|
||||
catch (WebSocketException e) when (e.Message.Contains("The server returned status code '502'"))
|
||||
{
|
||||
_logger.Error("Could not connect to Tom to Speech server.");
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Error(e, "Connecting to hermes failed. Skipping hermes websockets.");
|
||||
@@ -213,6 +224,19 @@ namespace TwitchChatTTS
|
||||
}
|
||||
}
|
||||
|
||||
private async Task InitializeVeadotube()
|
||||
{
|
||||
try
|
||||
{
|
||||
_veado.Initialize();
|
||||
await _veado.Connect();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Warning(e, "Failed to connect to Veado websocket server.");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task InitializeEmotes(SevenApiClient sevenapi, EmoteSet? channelEmotes)
|
||||
{
|
||||
var globalEmotes = await sevenapi.FetchGlobalSevenEmotes();
|
||||
|
||||
Reference in New Issue
Block a user