Skip to content

Commit

Permalink
add some logging and validation for the TwitchAPI token
Browse files Browse the repository at this point in the history
  • Loading branch information
Felk committed Aug 15, 2024
1 parent 3ad6732 commit d209c04
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
33 changes: 33 additions & 0 deletions TPP.Core/Chat/TwitchChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using TPP.Core.Overlay;
using TPP.Core.Utils;
using TPP.Persistence;
using TwitchLib.Api.Auth;
using TwitchLib.Client;
using TwitchLib.Client.Events;
using TwitchLib.Client.Models;
Expand Down Expand Up @@ -117,8 +118,40 @@ private async Task OnConnectionError(object? sender, OnConnectionErrorArgs e)
await _twitchClient.DisconnectAsync();
}

/// Copied from TPP.Core's README.md
private static readonly Dictionary<string, string> ScopesAndWhatTheyAreNeededFor = new()
{
["chat:read"] = "Read messages from chat (via IRC/TMI)",
["chat:edit"] = "Send messages to chat (via IRC/TMI)",
["user:bot"] = "Appear in chat as bot",
["user:read:chat"] = "Read messages from chat. (via EventSub)",
["user:write:chat"] = "Send messages to chat. (via Twitch API)",
["user:manage:whispers"] = "Sending and receiving whispers",
["moderator:read:chatters"] = "Read the chatters list in the channel (e.g. for badge drops)",
["moderator:read:followers"] = "Read the followers list (currently old core)",
["moderator:manage:banned_users"] = "Timeout, ban and unban users (tpp automod, mod commands)",
["moderator:manage:chat_messages"] = "Delete chat messages (tpp automod, purge invalid bets)",
["moderator:manage:chat_settings"] = "Change chat settings, e.g. emote-only mode (mod commands)",
["channel:read:subscriptions"] = "Reacting to incoming subscriptions",
};

private void ValidateScopes(HashSet<string> presentScopes)
{
foreach ((string scope, string neededFor) in ScopesAndWhatTheyAreNeededFor)
if (!presentScopes.Contains(scope))
_logger.LogWarning("Missing Twitch-API scope '{Scope}', needed for: {NeededFor}", scope, neededFor);
}

public async Task Start(CancellationToken cancellationToken)
{
_logger.LogDebug("Validating API access token...");
ValidateAccessTokenResponse validateResult = await TwitchApi.Validate();
_logger.LogInformation(
"Successfully validated Twitch API access token! Client-ID: {ClientID}, User-ID: {UserID}, " +
"Login: {Login}, Expires in: {Expires}s, Scopes: {Scopes}", validateResult.ClientId,
validateResult.UserId, validateResult.Login, validateResult.ExpiresIn, validateResult.Scopes);
ValidateScopes(validateResult.Scopes.ToHashSet());

await _twitchClient.ConnectAsync();
_logger.LogInformation("Connected to Twitch, channels: {Channels}",
string.Join(", ", _twitchClient.JoinedChannels.Select(c => c.Channel)));
Expand Down
1 change: 1 addition & 0 deletions TPP.Core/Chat/TwitchEventSubChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ await Task.WhenAll(
}
}
}
_logger.LogDebug("Finished setting up EventSub subscriptions");
}

public enum JoinResult { Ok, NotEnabled, AlreadyJoined, UserNotFound, StreamOffline }
Expand Down
5 changes: 5 additions & 0 deletions TPP.Core/TwitchApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using TPP.Core.Chat;
using TPP.Twitch.EventSub;
using TwitchLib.Api;
using TwitchLib.Api.Auth;
using TwitchLib.Api.Core.Enums;
using TwitchLib.Api.Core.Exceptions;
using TwitchLib.Api.Helix.Models.Chat.ChatSettings;
Expand Down Expand Up @@ -53,6 +54,10 @@ await Retrying(async api =>
});
}

// Meta
public Task<ValidateAccessTokenResponse> Validate() =>
Retrying(api => api.Auth.ValidateAccessTokenAsync());

// Chat (and whispers)
public Task<GetChattersResponse> GetChattersAsync(
string broadcasterId, string moderatorId, int first = 100, string? after = null) =>
Expand Down

0 comments on commit d209c04

Please sign in to comment.