//this is async/void because it is an event handler public async void PollTwitchAPI(object source, ElapsedEventArgs e) { TwitchTokenData tokenData = await GetTokenAsync(); TwitchAPIData data = await RequestTwitchDataAsync(tokenData); //checks if BotToilet is streaming and that the message explaining so has not been sent already if (data.Data.Length != 0 && !alreadySentMessage) { //standard embed building EmbedAuthorBuilder embedAuthorBuilder = new EmbedAuthorBuilder() { Name = "BotToilet is now streaming!", Url = "https://www.twitch.tv/bottoilet" }; EmbedBuilder embedBuilder = new EmbedBuilder() { Author = embedAuthorBuilder, Color = new Color?(Color.Purple), ImageUrl = "https://static-cdn.jtvnw.net/previews-ttv/live_user_bottoilet-1920x1080.jpg?r=" + new Random().Next().ToString(), //cache buster Description = "https://www.twitch.tv/bottoilet" }; embedBuilder.AddField("Title", data.Data[0].Title, true); embedBuilder.AddField("Started (Eastern Time):", data.Data[0].StartedAt.ToLocalTime(), true); //gets the streaming channel and send messages channel = client.GetChannel(streamingChannelId) as ISocketMessageChannel; await channel.SendMessageAsync("BotToilet has gone live on Twitch!", embed: embedBuilder.Build()); alreadySentMessage = true; } //checks if BotToilet went offline thus resetting the alreadySentMessgae flag else if (data.Data.Length == 0 && alreadySentMessage) { alreadySentMessage = false; } await RevokeTokenAsync(tokenData); }
private async Task <TwitchAPIData> RequestTwitchDataAsync(TwitchTokenData tokenData) { this.httpClient.DefaultRequestHeaders.Add("Client-ID", clientId); this.httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + tokenData.AccessToken); string url = "https://api.twitch.tv/helix/streams?user_login=bottoilet"; //makes request and ensures success HttpResponseMessage response = await this.httpClient.GetAsync(url); response.EnsureSuccessStatusCode(); //converts JSON to TwitchAPIData string resp = await response.Content.ReadAsStringAsync(); TwitchAPIData data = JsonConvert.DeserializeObject <TwitchAPIData>(resp); //Clean up this.httpClient.DefaultRequestHeaders.Clear(); return(data); }