示例#1
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 }));
        }