Added Nightbot integration. Changed from client credentials flow to implicit code grant flow.

This commit is contained in:
Tom
2024-08-11 21:22:37 +00:00
parent 13bb6a9aa8
commit 0ad063cebd
9 changed files with 260 additions and 23 deletions

View File

@ -17,6 +17,7 @@ namespace TwitchChatTTS.Twitch.Redemptions
private readonly User _user;
private readonly OBSSocketClient _obs;
private readonly HermesSocketClient _hermes;
private readonly NightbotApiClient _nightbot;
private readonly AudioPlaybackEngine _playback;
private readonly ILogger _logger;
private readonly Random _random;
@ -27,6 +28,7 @@ namespace TwitchChatTTS.Twitch.Redemptions
User user,
[FromKeyedServices("obs")] SocketClient<WebSocketMessage> obs,
[FromKeyedServices("hermes")] SocketClient<WebSocketMessage> hermes,
NightbotApiClient nightbot,
AudioPlaybackEngine playback,
ILogger logger)
{
@ -34,6 +36,7 @@ namespace TwitchChatTTS.Twitch.Redemptions
_user = user;
_obs = (obs as OBSSocketClient)!;
_hermes = (hermes as HermesSocketClient)!;
_nightbot = nightbot;
_playback = playback;
_logger = logger;
_random = new Random();
@ -191,6 +194,21 @@ namespace TwitchChatTTS.Twitch.Redemptions
_playback.PlaySound(action.Data["file_path"]);
_logger.Debug($"Played an audio file for channel point redeem [file: {action.Data["file_path"]}][chatter: {senderDisplayName}][chatter id: {senderId}]");
break;
case "NIGHTBOT_PLAY":
await _nightbot.Play();
break;
case "NIGHTBOT_PAUSE":
await _nightbot.Pause();
break;
case "NIGHTBOT_SKIP":
await _nightbot.Skip();
break;
case "NIGHTBOT_CLEAR_PLAYLIST":
await _nightbot.ClearPlaylist();
break;
case "NIGHTBOT_CLEAR_QUEUE":
await _nightbot.ClearQueue();
break;
default:
_logger.Warning($"Unknown redeemable action has occured. Update needed? [type: {action.Type}][chatter: {senderDisplayName}][chatter id: {senderId}]");
break;

View File

@ -7,14 +7,12 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
{
public string Name => "session_welcome";
private readonly HermesApiClient _hermes;
private readonly TwitchApiClient _api;
private readonly User _user;
private readonly ILogger _logger;
public SessionWelcomeHandler(HermesApiClient hermes, TwitchApiClient api, User user, ILogger logger)
public SessionWelcomeHandler(TwitchApiClient api, User user, ILogger logger)
{
_hermes = hermes;
_api = api;
_user = user;
_logger = logger;
@ -32,18 +30,24 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
}
int waited = 0;
while (_user.TwitchUserId <= 0 && ++waited < 3)
while ((_user.TwitchUserId <= 0 || _user.TwitchConnection == null) && ++waited < 5)
await Task.Delay(TimeSpan.FromSeconds(1));
try
if (_user.TwitchConnection == null)
{
await _hermes.AuthorizeTwitch();
var token = await _hermes.FetchTwitchBotToken();
_api.Initialize(token);
_logger.Error("Ensure you have linked either your Twitch account or TTS' bot to your TTS account. Twitch client will not be connecting.");
return;
}
catch (Exception)
{
_logger.Error("Ensure you have your Twitch account linked on TTS. Restart application once you do.");
_api.Initialize(_user.TwitchConnection.ClientId, _user.TwitchConnection.AccessToken);
var span = DateTime.Now - _user.TwitchConnection.ExpiresAt;
var timeLeft = span.TotalDays >= 2 ? Math.Floor(span.TotalDays) + " days" : (span.TotalHours >= 2 ? Math.Floor(span.TotalHours) + " hours" : Math.Floor(span.TotalMinutes) + " minutes");
if (span.TotalDays >= 3)
_logger.Information($"Twitch connection has {timeLeft} before it is revoked.");
else if (span.TotalMinutes >= 0)
_logger.Warning($"Twitch connection has {timeLeft} before it is revoked. Refreshing the token is soon required.");
else {
_logger.Error("Twitch connection has its permissions revoked. Refresh the token. Twith client will not be connecting.");
return;
}
@ -88,7 +92,7 @@ namespace TwitchChatTTS.Twitch.Socket.Handlers
await Subscribe(sender, subscription, message.Session.Id, broadcasterId, "1");
foreach (var subscription in subscriptionsv2)
await Subscribe(sender, subscription, message.Session.Id, broadcasterId, "2");
await Subscribe(sender, "channel.raid", broadcasterId, async () => await _api.CreateChannelRaidEventSubscription("1", message.Session.Id, to: broadcasterId));
sender.Identify(message.Session.Id);

View File

@ -95,9 +95,9 @@ public class TwitchApiClient
return await _web.GetJson<EventResponse<NotificationInfo>>("https://api.twitch.tv/helix/eventsub/subscriptions" + query);
}
public void Initialize(TwitchBotToken token)
public void Initialize(string clientId, string accessToken)
{
_web.AddHeader("Authorization", "Bearer " + token.AccessToken);
_web.AddHeader("Client-Id", token.ClientId);
_web.AddHeader("Authorization", "Bearer " + accessToken);
_web.AddHeader("Client-Id", clientId);
}
}