From e4517fa0dbb86dce24803879b676324ea9859825 Mon Sep 17 00:00:00 2001 From: "HACKERS\\Wayne" Date: Sat, 26 Mar 2016 23:35:05 +0000 Subject: [PATCH 1/2] Added Beam --- NadekoBot/Classes/ServerSpecificConfig.cs | 1 + NadekoBot/Commands/StreamNotifications.cs | 84 +++++++++++++++++------ 2 files changed, 63 insertions(+), 22 deletions(-) diff --git a/NadekoBot/Classes/ServerSpecificConfig.cs b/NadekoBot/Classes/ServerSpecificConfig.cs index bf2e76e3..01a0deb3 100644 --- a/NadekoBot/Classes/ServerSpecificConfig.cs +++ b/NadekoBot/Classes/ServerSpecificConfig.cs @@ -121,6 +121,7 @@ namespace NadekoBot.Classes { public enum StreamType { Twitch, + Beam, Hitbox, YoutubeGaming } diff --git a/NadekoBot/Commands/StreamNotifications.cs b/NadekoBot/Commands/StreamNotifications.cs index cc58228b..032dde8d 100644 --- a/NadekoBot/Commands/StreamNotifications.cs +++ b/NadekoBot/Commands/StreamNotifications.cs @@ -12,28 +12,38 @@ using NadekoBot.Classes.Permissions; using NadekoBot.Modules; using Newtonsoft.Json.Linq; -namespace NadekoBot.Commands { - internal class StreamNotifications : DiscordCommand { +namespace NadekoBot.Commands +{ + internal class StreamNotifications : DiscordCommand + { - private readonly Timer checkTimer = new Timer { + private readonly Timer checkTimer = new Timer + { Interval = new TimeSpan(0, 0, 15).TotalMilliseconds, }; - public StreamNotifications(DiscordModule module) : base(module) { + public StreamNotifications(DiscordModule module) : base(module) + { checkTimer.Elapsed += async (s, e) => { - try { + try + { var streams = SpecificConfigurations.Default.AllConfigs.SelectMany(c => c.ObservingStreams); if (!streams.Any()) return; - foreach (var stream in streams) { + foreach (var stream in streams) + { Tuple data; - try { + try + { data = await GetStreamStatus(stream); - } catch { + } + catch + { continue; } - if (data.Item1 != stream.LastStatus) { + if (data.Item1 != stream.LastStatus) + { stream.LastStatus = data.Item1; var server = NadekoBot.Client.GetServer(stream.ServerId); var channel = server?.GetChannel(stream.ChannelId); @@ -47,23 +57,27 @@ namespace NadekoBot.Commands { msg += $"\n`Here is the Link:`【 http://www.hitbox.tv/{stream.Username}/ 】"; else if (stream.Type == StreamNotificationConfig.StreamType.Twitch) msg += $"\n`Here is the Link:`【 http://www.twitch.tv/{stream.Username}/ 】"; + else if (stream.Type == StreamNotificationConfig.StreamType.Beam) + msg += $"\n`Here is the Link:`【 http://www.beam.pro/{stream.Username}/ 】"; else if (stream.Type == StreamNotificationConfig.StreamType.YoutubeGaming) msg += $"\n`Here is the Link:`【 not implemented yet - {stream.Username} 】"; await channel.SendMessage(msg); } } - } catch { } + } + catch { } ConfigHandler.SaveConfig(); }; - checkTimer.Start(); } - private async Task> GetStreamStatus(StreamNotificationConfig stream) { + private async Task> GetStreamStatus(StreamNotificationConfig stream) + { bool isLive; string response; JObject data; - switch (stream.Type) { + switch (stream.Type) + { case StreamNotificationConfig.StreamType.Hitbox: response = await SearchHelper.GetResponseStringAsync($"https://api.hitbox.tv/media/status/{stream.Username}"); data = JObject.Parse(response); @@ -74,13 +88,19 @@ namespace NadekoBot.Commands { data = JObject.Parse(response); isLive = !string.IsNullOrWhiteSpace(data["stream"].ToString()); return new Tuple(isLive, isLive ? data["stream"]["viewers"].ToString() : "0"); + case StreamNotificationConfig.StreamType.Beam: + response = await SearchHelper.GetResponseStringAsync($"https://beam.pro/api/v1/channels/{stream.Username}"); + data = JObject.Parse(response); + isLive = data["online"].ToString() == "true"; + return new Tuple(isLive, isLive ? data["viewersCurrent"].ToString() : "0"); default: break; } return new Tuple(false, "0"); } - internal override void Init(CommandGroupBuilder cgb) { + internal override void Init(CommandGroupBuilder cgb) + { cgb.CreateCommand(Module.Prefix + "hitbox") .Alias(Module.Prefix + "hb") .Description("Notifies this channel when a certain user starts streaming." + @@ -97,6 +117,14 @@ namespace NadekoBot.Commands { .Parameter("username", ParameterType.Unparsed) .Do(TrackStream(StreamNotificationConfig.StreamType.Twitch)); + cgb.CreateCommand(Module.Prefix + "beam") + .Alias(Module.Prefix + "bm") + .Description("Notifies this channel when a certain user starts streaming." + + "\n**Usage**: ~beam SomeStreamer") + .AddCheck(SimpleCheckers.ManageServer()) + .Parameter("username", ParameterType.Unparsed) + .Do(TrackStream(StreamNotificationConfig.StreamType.Beam)); + cgb.CreateCommand(Module.Prefix + "removestream") .Alias(Module.Prefix + "rms") .Description("Removes notifications of a certain streamer on this channel." + @@ -113,7 +141,8 @@ namespace NadekoBot.Commands { var toRemove = config.ObservingStreams .FirstOrDefault(snc => snc.ChannelId == e.Channel.Id && snc.Username.ToLower().Trim() == username); - if (toRemove == null) { + if (toRemove == null) + { await e.Channel.SendMessage(":anger: No such stream."); return; } @@ -136,15 +165,18 @@ namespace NadekoBot.Commands { var streamsArray = streams as StreamNotificationConfig[] ?? streams.ToArray(); - if (streamsArray.Length == 0) { + if (streamsArray.Length == 0) + { await e.Channel.SendMessage("You are not following any streams on this server."); return; } var text = string.Join("\n", streamsArray.Select(snc => { - try { + try + { return $"`{snc.Username}`'s stream on **{e.Server.GetChannel(e.Channel.Id).Name}** channel. 【`{snc.Type.ToString()}`】"; - } catch { } + } + catch { } return ""; })); @@ -160,20 +192,26 @@ namespace NadekoBot.Commands { var config = SpecificConfigurations.Default.Of(e.Server.Id); - var stream = new StreamNotificationConfig { + var stream = new StreamNotificationConfig + { ServerId = e.Server.Id, ChannelId = e.Channel.Id, Username = username, Type = type, }; var exists = config.ObservingStreams.Contains(stream); - if (exists) { + if (exists) + { await e.Channel.SendMessage(":anger: I am already notifying that stream on this channel."); + return; } Tuple data; - try { + try + { data = await GetStreamStatus(stream); - } catch { + } + catch + { await e.Channel.SendMessage(":anger: Stream probably doesn't exist."); return; } @@ -183,6 +221,8 @@ namespace NadekoBot.Commands { msg += $"\n`Here is the Link:`【 http://www.hitbox.tv/{stream.Username}/ 】"; else if (type == StreamNotificationConfig.StreamType.Twitch) msg += $"\n`Here is the Link:`【 http://www.twitch.tv/{stream.Username}/ 】"; + else if (type == StreamNotificationConfig.StreamType.Beam) + msg += $"\n`Here is the Link:`【 https://beam.pro/{stream.Username}/ 】"; else if (type == StreamNotificationConfig.StreamType.YoutubeGaming) msg += $"\n`Here is the Link:` not implemented yet - {stream.Username}"; stream.LastStatus = data.Item1; From 7eb05080b08a24df246f327797dfb2d2f9a116cd Mon Sep 17 00:00:00 2001 From: Master Kwoth Date: Mon, 28 Mar 2016 15:51:17 +0200 Subject: [PATCH 2/2] fixed beam, thanks @LawlyPopz for implementing 99% of it --- NadekoBot/Commands/StreamNotifications.cs | 32 ++++++++++++----------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/NadekoBot/Commands/StreamNotifications.cs b/NadekoBot/Commands/StreamNotifications.cs index 032dde8d..f7a980d4 100644 --- a/NadekoBot/Commands/StreamNotifications.cs +++ b/NadekoBot/Commands/StreamNotifications.cs @@ -1,16 +1,13 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Runtime.CompilerServices; -using System.Text; -using System.Threading.Tasks; -using System.Timers; -using Discord.Commands; +using Discord.Commands; using NadekoBot.Classes; using NadekoBot.Classes.JSONModels; using NadekoBot.Classes.Permissions; using NadekoBot.Modules; using Newtonsoft.Json.Linq; +using System; +using System.Linq; +using System.Threading.Tasks; +using System.Timers; namespace NadekoBot.Commands { @@ -24,7 +21,8 @@ namespace NadekoBot.Commands public StreamNotifications(DiscordModule module) : base(module) { - checkTimer.Elapsed += async (s, e) => { + checkTimer.Elapsed += async (s, e) => + { try { var streams = SpecificConfigurations.Default.AllConfigs.SelectMany(c => c.ObservingStreams); @@ -91,8 +89,8 @@ namespace NadekoBot.Commands case StreamNotificationConfig.StreamType.Beam: response = await SearchHelper.GetResponseStringAsync($"https://beam.pro/api/v1/channels/{stream.Username}"); data = JObject.Parse(response); - isLive = data["online"].ToString() == "true"; - return new Tuple(isLive, isLive ? data["viewersCurrent"].ToString() : "0"); + isLive = data["online"].ToObject() == true; + return new Tuple(isLive, data["viewersCurrent"].ToString()); default: break; } @@ -131,7 +129,8 @@ namespace NadekoBot.Commands "\n**Usage**: ~rms SomeGuy") .AddCheck(SimpleCheckers.ManageServer()) .Parameter("username", ParameterType.Unparsed) - .Do(async e => { + .Do(async e => + { var username = e.GetArg("username")?.ToLower().Trim(); if (string.IsNullOrWhiteSpace(username)) return; @@ -156,7 +155,8 @@ namespace NadekoBot.Commands .Alias(Module.Prefix + "ls") .Description("Lists all streams you are following on this server." + "\n**Usage**: ~ls") - .Do(async e => { + .Do(async e => + { var config = SpecificConfigurations.Default.Of(e.Server.Id); @@ -171,7 +171,8 @@ namespace NadekoBot.Commands return; } - var text = string.Join("\n", streamsArray.Select(snc => { + var text = string.Join("\n", streamsArray.Select(snc => + { try { return $"`{snc.Username}`'s stream on **{e.Server.GetChannel(e.Channel.Id).Name}** channel. 【`{snc.Type.ToString()}`】"; @@ -185,7 +186,8 @@ namespace NadekoBot.Commands } private Func TrackStream(StreamNotificationConfig.StreamType type) => - async e => { + async e => + { var username = e.GetArg("username")?.ToLowerInvariant(); if (string.IsNullOrWhiteSpace(username)) return;