public async Task Del(CommandContext ctx, [Description("user's twitch name")] string name) { await ctx.TriggerTypingAsync(); if (StreamerStatuses.Data.Status.Any(prod => prod.Name == name)) { int idx = StreamerStatuses.Data.Status.FindIndex(prod => prod.Name == name); if (StreamerStatuses.Data.Status[idx].Channels.Contains(ctx.Channel.Id)) { StreamerStatuses.Data.Status[idx].Channels.Remove(ctx.Channel.Id); if (StreamerStatuses.Data.Status[idx].Channels.Count == 0) { StreamerStatuses.Data.Status.RemoveAt(idx); await ctx.RespondAsync($"Notification de stream de {name} supprimée du serveur (dernier channel notifié supprimé)."); } else { await ctx.RespondAsync($"Notification de stream de {name} supprimée de ce channel."); } } else { await ctx.RespondAsync($"Ce channel n'est pas notifié par ce live."); } await StreamerStatuses.Save(); } }
public async Task Add(CommandContext ctx, [Description("user's twitch name")] string name) { await ctx.TriggerTypingAsync(); string WEBSERVICE_URL = $"https://api.twitch.tv/kraken/users?login={name}"; try { var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL); if (webRequest != null) { webRequest.Method = "GET"; webRequest.Timeout = 12000; webRequest.ContentType = "application/json"; webRequest.Headers.Add("Client-ID", "0r570obyz0in1a85pqv16as54sfce1"); webRequest.Headers.Add("Accept", "application/vnd.twitchtv.v5+json"); using System.IO.Stream s = webRequest.GetResponse().GetResponseStream(); using System.IO.StreamReader sr = new System.IO.StreamReader(s); var jsonResponse = sr.ReadToEnd(); var twitch = JsonConvert.DeserializeObject <NameRequest>(jsonResponse); int id = int.Parse(twitch.Users[0].Id); //Console.WriteLine(string.Format("Response: {0}", jsonResponse)); if (StreamerStatuses.Data.Status.Any(prod => prod.Id == id)) { int idx = StreamerStatuses.Data.Status.FindIndex(prod => prod.Id == id); StreamerStatuses.Data.Status[idx].Channels.Add(ctx.Channel.Id); await ctx.RespondAsync($"Notification de live de {name} ajoutée a ce channel."); } else { Status stream = new Status(name, false, id, ctx.Channel.Id); StreamerStatuses.Data.Status.Add(stream); await ctx.RespondAsync($"Notification de live de {name} ajoutée a ce channel."); } await StreamerStatuses.Save(); } } catch { await ctx.RespondAsync("User twitch inexistant."); } }
//check et affichage par channel des lives private static async Task WebRequestAsync(Status id, DiscordClient client) { Random rnd = new Random(); string WEBSERVICE_URL = "https://api.twitch.tv/kraken/streams/" + id.Id; // string WEBSERVICE_URL = "https://api.twitch.tv/kraken/users?login=moman"; try { var webRequest = System.Net.WebRequest.Create(WEBSERVICE_URL); if (webRequest != null) { webRequest.Method = "GET"; webRequest.Timeout = 12000; webRequest.ContentType = "application/json"; webRequest.Headers.Add("Client-ID", Token); webRequest.Headers.Add("Accept", "application/vnd.twitchtv.v5+json"); using System.IO.Stream s = webRequest.GetResponse().GetResponseStream(); using System.IO.StreamReader sr = new System.IO.StreamReader(s); var jsonResponse = sr.ReadToEnd(); //Console.WriteLine(string.Format("Response: {0}", jsonResponse)); if (jsonResponse.Contains("display_name")) { var twitch = JsonConvert.DeserializeObject <Streamer>(jsonResponse); //return string.Format("Response: {0}", jsonResponse); if (!id.IsLive) { Console.WriteLine(string.Format($"Live {id.Name} up!")); // wrap it into an embed var embed = new DiscordEmbedBuilder { Title = twitch.Stream.Channel.Display_name, Description = twitch.Stream.Channel.Status, Url = twitch.Stream.Channel.Url, ImageUrl = twitch.Stream.Preview.Medium + rnd.Next(999999), ThumbnailUrl = twitch.Stream.Channel.Logo }; DiscordEmbedBuilder.EmbedFooter foot = new DiscordEmbedBuilder.EmbedFooter { Text = $"Joue à: " + twitch.Stream.Channel.Game, IconUrl = "https://puush.poneyy.fr/TDaq.png" }; embed.Footer = foot; DiscordChannel info; foreach (ulong channel in id.Channels) { info = await client.GetChannelAsync(channel); await info.SendMessageAsync(embed : embed); } /* DiscordChannel info = await client.GetChannelAsync(channel); * await info.SendMessageAsync(embed: embed);*/ StreamerStatuses.Data.Status.First(d => d.Id == id.Id).IsLive = true; await StreamerStatuses.Save(); client.DebugLogger.LogMessage(LogLevel.Info, "PoneyyBot", $"{id} en live", DateTime.Now); } else { client.DebugLogger.LogMessage(LogLevel.Info, "PoneyyBot", $"{id} en live", DateTime.Now); } } else if (id.IsLive) { // wrap it into an embed var embed = new DiscordEmbedBuilder { Title = id.Name, Description = "Fin du live:wave: :wave:", Url = "https://zerator.com/programmation" }; DiscordEmbedBuilder.EmbedFooter foot = new DiscordEmbedBuilder.EmbedFooter { Text = "Consultez la programmation pour plus d'informations!", IconUrl = "https://puush.poneyy.fr/TDaq.png" }; embed.Footer = foot; DiscordChannel info; /*info = await client.GetChannelAsync(id.Channels[0]); * await info.SendMessageAsync(embed: embed);*/ foreach (ulong channel in id.Channels) { info = await client.GetChannelAsync(channel); await info.SendMessageAsync(embed : embed); } StreamerStatuses.Data.Status.First(d => d.Id == id.Id).IsLive = false; await StreamerStatuses.Save(); client.DebugLogger.LogMessage(LogLevel.Info, "PoneyyBot", $"{id} plus en live", DateTime.Now); } //else Console.WriteLine($"{id} pas en live"); else { client.DebugLogger.LogMessage(LogLevel.Info, "PoneyyBot", $"{id} pas en live", DateTime.Now); } } } catch (Exception ex) { Console.WriteLine(ex.ToString()); } }