private async void CheckViews(object state) { var api = new TwitchLib.TwitchAPI(clientId: ClientId); var v5Stream = new TwitchLib.Streams.V5(api); StreamByUser myStream = null; try { myStream = await v5Stream.GetStreamByUserAsync(ChannelId); } catch (JsonReaderException ex) { Logger.LogError($"Unable to read stream from Twitch: {ex}"); return; } if (_CurrentViewerCount != (myStream.Stream?.Viewers ?? 0)) { _CurrentViewerCount = (myStream.Stream?.Viewers ?? 0); Updated?.Invoke(null, new ServiceUpdatedEventArgs { ServiceName = Name, NewViewers = _CurrentViewerCount }); } }
private async void CheckViews(object state) { if (!(state is TwitchLib.Streams.V5)) { return; } TwitchLib.Streams.V5 v5Stream = state as TwitchLib.Streams.V5; StreamByUser myStream = null; try { myStream = await v5Stream.GetStreamByUserAsync(ChannelId); } catch (JsonReaderException ex) { Logger.LogError($"Unable to read stream from Twitch: {ex}"); return; } catch (Exception) { Logger.LogError($"Error while communicating with Twitch"); return; } if (_CurrentViewerCount != (myStream.Stream?.Viewers ?? 0)) { _CurrentViewerCount = (myStream.Stream?.Viewers ?? 0); Updated?.Invoke(null, new ServiceUpdatedEventArgs { ServiceName = Name, NewViewers = _CurrentViewerCount }); } }
public async Task <int> GetViewerCountAsync() { StreamByUser streamByUserAsync = await _twitchApi.Streams.v5.GetStreamByUserAsync(_twitchClientSettings.TwitchChannelId); return(streamByUserAsync.Stream.Viewers); }
#pragma warning disable CA1054 // Uri parameters should not be strings public async Task SetStream(CommandContext ctx, [Description("Stream Url")] string url) #pragma warning restore CA1054 // Uri parameters should not be strings { // Dumb Discord.NET if (url.StartsWith('-')) { await ctx.RespondAsync("Invalid stream url"); return; } else if (ctx.Client.CurrentUser.Presence.Activity.ActivityType == ActivityType.Streaming) { await ctx.RespondAsync("Sorry, I'm already watching somebody else's stream"); return; } string name = url; // Probably a url if (url.Contains(".", StringComparison.Ordinal)) { Match match = Regex.Match(url, @"\.tv\/([^\/]*)"); if (!match.Success) { await ctx.RespondAsync("Unable to process stream url"); return; } name = match.Groups[1].Value; } Users users; try { users = await this.twitchAPI.V5.Users.GetUserByNameAsync(name); } #pragma warning disable CA1031 // Do not catch general exception types catch (Exception e) #pragma warning restore CA1031 // Do not catch general exception types { Log.Error(e, "Unable to grab Twitch users"); await ctx.RespondAsync("Invalid stream url"); return; } if (users.Matches.Length == 0) { await ctx.RespondAsync($"Streamer not found"); return; } StreamByUser userStream = await this.twitchAPI.V5.Streams.GetStreamByUserAsync(users.Matches[0].Id); if (userStream.Stream == null) { await ctx.RespondAsync($"Stream seems to be offline"); return; } else if (userStream.Stream.Game != "Intruder") { await ctx.RespondAsync($"I only promote **Intruder**{(userStream.Stream.Game.Length > 0 ? $", not **{userStream.Stream.Game}**," : string.Empty)} streams"); return; } await this.activityManager.SetStreamAsync(ctx.User.Id, name.Capitalize(), $"https://twitch.tv/{name}"); await ctx.Message.CreateReactionAsync(DiscordEmoji.FromName(ctx.Client, ":heavy_check_mark:")); }