Skip to content

Commit

Permalink
Updated ConnectionCredentials so a username is optional.
Browse files Browse the repository at this point in the history
If no username is provided to the ConnectionCredentials, a default username of `justinfan` right-padded with 4-5 random digits will be generated.
  • Loading branch information
ChrisBlueStone committed Nov 19, 2023
1 parent 0442183 commit 1c75fb9
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions TwitchLib.Client.Models/ConnectionCredentials.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,26 @@ public class ConnectionCredentials

/// <summary>Constructor for ConnectionCredentials object.</summary>
public ConnectionCredentials(
string twitchUsername,
string twitchOAuth,
string twitchUsername = null,
string twitchOAuth = null,
string twitchWebsocketURI = DefaultWebSocketUri,
bool disableUsernameCheck = false,
Capabilities capabilities = null)
{
if (!disableUsernameCheck && !new Regex("^([a-zA-Z0-9][a-zA-Z0-9_]{3,25})$").Match(twitchUsername).Success)
if (twitchUsername != null &&
!disableUsernameCheck &&
!new Regex("^([a-zA-Z0-9][a-zA-Z0-9_]{3,25})$").Match(twitchUsername).Success)
{
throw new Exception($"Twitch username does not appear to be valid. {twitchUsername}");
}

TwitchUsername = twitchUsername.ToLower();
TwitchOAuth = twitchOAuth;
TwitchUsername = twitchUsername?.ToLower() ?? $"justinfan${1000 + new Random().Next(79999)}";
TwitchOAuth = twitchOAuth ?? string.Empty;

// Make sure proper formatting is applied to oauth
if (!twitchOAuth.Contains(":"))
if (!TwitchOAuth.Contains(":"))
{
TwitchOAuth = $"oauth:{twitchOAuth.Replace("oauth", "")}";
TwitchOAuth = $"oauth:{TwitchOAuth.Replace("oauth", "")}";
}

TwitchWebsocketURI = twitchWebsocketURI;
Expand Down

0 comments on commit 1c75fb9

Please sign in to comment.