private static void FirstTimeSetup()
        {
            var HasNotAuthenticated = true;

            AppState = Guid.NewGuid();

            ConsoleHelper.WriteLine("Hi there! It looks like you're starting ATCB for the first time.");
            ConsoleHelper.WriteLine("Just so you know, I'm going to need some permissions from your Twitch account to run correctly.");
            while (HasNotAuthenticated)
            {
                ConsoleHelper.WriteLine("I'll open up the authentication page in your default browser, press any key once you've successfully authenticated.");
                Thread.Sleep(5000);
                System.Diagnostics.Process.Start($"{OAuthUrl}&state={AppState.ToString()}");
                System.Console.ReadKey(true);
                ConsoleHelper.WriteLine("Checking for authentication...");
                try
                {
                    var AccessToken = Authenticator.GetAccessTokenByStateAsync(AppState).Result;
                    HasNotAuthenticated = false;
                }
                catch (Exception)
                {
                    ConsoleHelper.WriteLine("Authentication failed, uh oh. Let's send you back to Twitch's authentication page and try again!");
                }
            }
            ConsoleHelper.WriteLine("Neat-o! We've hooked ourselves an access token! Look's like you're all good to go!");
            File.WriteAllText($"{AppDirectory}setupcomplete.txt", AppState.ToString());
        }
Example #2
0
        /// <summary>
        /// Initializes a new TwitchChatBot object, which handles all communications between Twitch and the client.
        /// </summary>
        /// <param name="authenticator">A pre-initialized WebAuthenticator.</param>
        /// <param name="appState">The global AppState.</param>
        public TwitchChatBot(WebAuthenticator authenticator, Guid appState)
        {
            this.authenticator = authenticator;
            this.appState      = appState;
            twitchApi          = new TwitchAPI(ClientId);

            // Retrieve access tokens and usernames for logging in
            userAccessToken = authenticator.GetAccessTokenByStateAsync(appState).Result;
            botAccessToken  = authenticator.GetBotAccessTokenByValidStateAsync(appState).Result;
            Username        = authenticator.GetUsernameFromOAuthAsync(userAccessToken).Result;
            Botname         = authenticator.GetUsernameFromOAuthAsync(botAccessToken).Result;

            // If the either of the usernames are blank, then we have to refresh the tokens.
            if (string.IsNullOrEmpty(Username))
            {
                ConsoleHelper.WriteLine("Refreshing user access token...");
                userAccessToken = RefreshAccessToken(appState, ClientId, userAccessToken).Result;
                Username        = authenticator.GetUsernameFromOAuthAsync(userAccessToken).Result;
            }
            if (string.IsNullOrEmpty(Botname))
            {
                ConsoleHelper.WriteLine("Refreshing bot access token...");
                botAccessToken = RefreshAccessToken(BotState, ClientId, botAccessToken).Result;
                Botname        = authenticator.GetUsernameFromOAuthAsync(botAccessToken).Result;
            }

            twitchApi.Settings.AccessToken = botAccessToken;
            userClient        = new TwitchClient(new ConnectionCredentials(Username, userAccessToken), Username);
            botClient         = new TwitchClient(new ConnectionCredentials(Botname, botAccessToken), Username);
            commandFactory    = new CommandFactory();
            speechSynthesizer = new SpeechSynthesis();

            // ATCB-made events
            ConsoleHelper.OnConsoleCommand += (sender, e) => { PerformConsoleCommand((e as ConsoleCommandEventArgs).Message); };

            // User client events
            userClient.OnConnected   += OnUserConnected;
            userClient.OnBeingHosted += OnUserBeingHosted;

            // Bot client events
            botClient.OnConnected           += OnBotConnected;
            botClient.OnConnectionError     += OnBotConnectionError;
            botClient.OnMessageReceived     += OnMessageReceived;
            botClient.OnMessageSent         += OnBotMessageSent;
            botClient.OnChatCommandReceived += OnChatCommandReceived;
            botClient.OnNewSubscriber       += OnNewSubscriber;
            botClient.OnReSubscriber        += OnReSubscriber;
        }
        /// <summary>
        /// Initializes a new TwitchChatBot object, which handles all communications between Twitch and the client.
        /// </summary>
        /// <param name="authenticator">A pre-initialized WebAuthenticator.</param>
        /// <param name="appState">The global AppState.</param>
        public TwitchChatBot(WebAuthenticator authenticator, Guid appState, ApplicationSettings settings)
        {
            Settings = settings;

            this.authenticator = authenticator;
            this.appState      = appState;
            twitchApi          = new TwitchAPI(ClientId);

            // Retrieve access tokens and usernames for logging in
            userAccessToken = authenticator.GetAccessTokenByStateAsync(appState).Result;
            botAccessToken  = authenticator.GetBotAccessTokenByValidStateAsync(appState).Result;
            Username        = authenticator.GetUsernameFromOAuthAsync(userAccessToken).Result;
            Botname         = authenticator.GetUsernameFromOAuthAsync(botAccessToken).Result;

            // If the either of the usernames are blank, then we have to refresh the tokens.
            if (string.IsNullOrEmpty(Username))
            {
                ConsoleHelper.WriteLine("Refreshing user access token...");
                userAccessToken = RefreshAccessToken(appState, ClientId, userAccessToken).Result;
                Username        = authenticator.GetUsernameFromOAuthAsync(userAccessToken).Result;
            }
            if (string.IsNullOrEmpty(Botname))
            {
                ConsoleHelper.WriteLine("Refreshing bot access token...");
                botAccessToken = RefreshAccessToken(BotState, ClientId, botAccessToken).Result;
                Botname        = authenticator.GetUsernameFromOAuthAsync(botAccessToken).Result;
            }

            twitchApi.Settings.AccessToken = userAccessToken;
            userClient        = new TwitchClient(new ConnectionCredentials(Username, userAccessToken), Username);
            botClient         = new TwitchClient(new ConnectionCredentials(Botname, botAccessToken), Username);
            followerService   = new FollowerService(twitchApi);
            liveStreamMonitor = new LiveStreamMonitor(twitchApi);
            commandFactory    = new CommandFactory();
            speechSynthesizer = new SpeechSynthesizer();

            // ATCB-made events
            ConsoleHelper.OnConsoleCommand += (sender, e) => { PerformConsoleCommand((e as ConsoleCommandEventArgs).Message); };

            // TwitchLib Service events
            followerService.OnNewFollowersDetected += OnNewFollowersDetected;
            liveStreamMonitor.OnStreamOnline       += OnStreamOnline;
            liveStreamMonitor.OnStreamOffline      += OnStreamOffline;

            // User client events
            userClient.OnConnected       += OnUserConnected;
            userClient.OnConnectionError += OnUserConnectionError;
            userClient.OnBeingHosted     += OnUserBeingHosted;
            userClient.OnWhisperReceived += OnWhisperReceived;

            // Bot client events
            botClient.OnConnected           += OnBotConnected;
            botClient.OnConnectionError     += OnBotConnectionError;
            botClient.OnMessageReceived     += OnMessageReceived;
            botClient.OnUserJoined          += OnJoined;
            botClient.OnUserLeft            += OnLeft;
            botClient.OnMessageSent         += OnBotMessageSent;
            botClient.OnChatCommandReceived += OnChatCommandReceived;
            botClient.OnNewSubscriber       += OnNewSubscriber;
            botClient.OnReSubscriber        += OnReSubscriber;
            botClient.OnGiftedSubscription  += OnGiftSubscriber;

            // Set up TwitchLib services
            followerService.SetChannelByName(Username);
            liveStreamMonitor.SetStreamsByUsername(new List <string>(new[] { Username }));
        }