private async Task StartTwitchMonitoring() { var api = new TwitchLib.TwitchAPI(clientId: ClientId); Service = new FollowerService(api); Service.SetChannelByName(Channel); await Service.StartService(); var v5 = new TwitchLib.Channels.V5(api); var follows = await v5.GetAllFollowersAsync(ChannelId); _CurrentFollowerCount = follows.Count; Service.OnNewFollowersDetected += Service_OnNewFollowersDetected; var v5Stream = CreateTwitchStream(api); if (v5Stream == null) { await Task.Delay(2000); await StartTwitchMonitoring(); return; } var myStream = await v5Stream.GetStreamByUserAsync(ChannelId); _CurrentViewerCount = myStream.Stream?.Viewers ?? 0; Logger.LogInformation($"Now monitoring Twitch with {_CurrentFollowerCount} followers and {_CurrentViewerCount} Viewers"); _Timer = new Timer(CheckViews, v5Stream, 0, 5000); }
public TwitchFollowerService(TwitchAPI twitchApi, TwitchClientSettings settings) { _twitchApi = twitchApi; _settings = settings; _followerService = new FollowerService(twitchApi); _followerService.SetChannelByName(settings.TwitchChannel); _followerService.StartService().Wait(); _followerService.OnNewFollowersDetected += FollowerServiceOnOnNewFollowersDetected; }
/// <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 })); }