From 1c75fb9d89af2ad3b2761d1c4edfcf0f78148a5c Mon Sep 17 00:00:00 2001 From: Chris Date: Sun, 19 Nov 2023 00:55:49 -0600 Subject: [PATCH] Updated ConnectionCredentials so a username is optional. If no username is provided to the ConnectionCredentials, a default username of `justinfan` right-padded with 4-5 random digits will be generated. --- .../ConnectionCredentials.cs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/TwitchLib.Client.Models/ConnectionCredentials.cs b/TwitchLib.Client.Models/ConnectionCredentials.cs index cefa5e2c..9779fb2d 100644 --- a/TwitchLib.Client.Models/ConnectionCredentials.cs +++ b/TwitchLib.Client.Models/ConnectionCredentials.cs @@ -22,22 +22,26 @@ public class ConnectionCredentials /// Constructor for ConnectionCredentials object. 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;