From 8c5e0ae823276e752109df10ff064961a8948333 Mon Sep 17 00:00:00 2001 From: Chris Date: Sat, 6 Apr 2024 13:29:30 -0500 Subject: [PATCH] Updated ConnectionCredentials to use an anonymous user by default (#257) * 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. * Updated call to Random to provide a min value Co-authored-by: AoshiW <48525283+AoshiW@users.noreply.github.com> --- .../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 90564003..9f3fa5fc 100644 --- a/TwitchLib.Client.Models/ConnectionCredentials.cs +++ b/TwitchLib.Client.Models/ConnectionCredentials.cs @@ -23,21 +23,25 @@ public partial class ConnectionCredentials /// Constructor for ConnectionCredentials object. public ConnectionCredentials( - string twitchUsername, - string twitchOAuth, + string? twitchUsername = null, + string? twitchOAuth = null, bool disableUsernameCheck = false, Capabilities? capabilities = null) { - if (!disableUsernameCheck && !GetUsernameCheckRegex().Match(twitchUsername).Success) + if (twitchUsername != null && + !disableUsernameCheck && + !GetUsernameCheckRegex().Match(twitchUsername).Success) + { throw new Exception($"Twitch username does not appear to be valid. {twitchUsername}"); + } - TwitchUsername = twitchUsername.ToLower(); - TwitchOAuth = twitchOAuth; + TwitchUsername = twitchUsername?.ToLower() ?? $"justinfan{new Random().Next(1000, 89999)}"; + 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", "")}"; } Capabilities = capabilities ?? new Capabilities();