"e" command, finished twitch and hitbox stream notifications (hopefuly)

This commit is contained in:
Master Kwoth 2016-03-13 00:25:49 +01:00
parent 08e1ab0cfc
commit 400df43875
3 changed files with 90 additions and 42 deletions

View File

@ -81,7 +81,7 @@ namespace NadekoBot.Classes.JSONModels {
public static bool IsUserBlacklisted(ulong id) => NadekoBot.Config.UserBlacklist.Contains(id); public static bool IsUserBlacklisted(ulong id) => NadekoBot.Config.UserBlacklist.Contains(id);
} }
public class StreamNotificationConfig { public class StreamNotificationConfig : IEquatable<StreamNotificationConfig> {
public string Username { get; set; } public string Username { get; set; }
public StreamType Type { get; set; } public StreamType Type { get; set; }
public ulong ServerId { get; set; } public ulong ServerId { get; set; }
@ -93,5 +93,14 @@ namespace NadekoBot.Classes.JSONModels {
Hitbox, Hitbox,
YoutubeGaming YoutubeGaming
} }
public bool Equals(StreamNotificationConfig other) =>
this.Username.ToLower().Trim() == other.Username.ToLower().Trim() &&
this.Type == other.Type &&
this.ServerId == other.ServerId;
public override int GetHashCode() {
return (int) ((int) ServerId + Username.Length + (int) Type);
}
} }
} }

View File

@ -15,7 +15,7 @@ namespace NadekoBot.Commands {
internal class StreamNotifications : DiscordCommand { internal class StreamNotifications : DiscordCommand {
private readonly Timer checkTimer = new Timer { private readonly Timer checkTimer = new Timer {
Interval = new TimeSpan(0, 0, 30).TotalMilliseconds, Interval = new TimeSpan(0, 0, 15).TotalMilliseconds,
}; };
public StreamNotifications(DiscordModule module) : base(module) { public StreamNotifications(DiscordModule module) : base(module) {
@ -25,7 +25,12 @@ namespace NadekoBot.Commands {
if (streams == null || !streams.Any()) return; if (streams == null || !streams.Any()) return;
foreach (var stream in streams) { foreach (var stream in streams) {
var data = await GetStreamStatus(stream); Tuple<bool, string> data;
try {
data = await GetStreamStatus(stream);
} catch {
continue;
}
if (data.Item1 != stream.LastStatus) { if (data.Item1 != stream.LastStatus) {
stream.LastStatus = data.Item1; stream.LastStatus = data.Item1;
@ -37,8 +42,12 @@ namespace NadekoBot.Commands {
$"**{(data.Item1 ? "ONLINE" : "OFFLINE")}** with " + $"**{(data.Item1 ? "ONLINE" : "OFFLINE")}** with " +
$"**{data.Item2}** viewers."; $"**{data.Item2}** viewers.";
if (stream.LastStatus) if (stream.LastStatus)
msg += $"\n`Here is the Link:`【http://www.hitbox.tv/{stream.Username}】"; if (stream.Type == StreamNotificationConfig.StreamType.Hitbox)
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/channels/{stream.Username}】";
else if (stream.Type == StreamNotificationConfig.StreamType.YoutubeGaming)
msg += $"\n`Here is the Link:`【not implemented yet - {stream.Username}】";
await channel.SendMessage(msg); await channel.SendMessage(msg);
} }
} }
@ -51,13 +60,19 @@ namespace NadekoBot.Commands {
private async Task<Tuple<bool, string>> GetStreamStatus(StreamNotificationConfig stream) { private async Task<Tuple<bool, string>> GetStreamStatus(StreamNotificationConfig stream) {
bool isLive; bool isLive;
string response;
JObject data;
switch (stream.Type) { switch (stream.Type) {
case StreamNotificationConfig.StreamType.Hitbox: case StreamNotificationConfig.StreamType.Hitbox:
var response = await SearchHelper.GetResponseStringAsync($"https://api.hitbox.tv/media/status/{stream.Username}"); response = await SearchHelper.GetResponseStringAsync($"https://api.hitbox.tv/media/status/{stream.Username}");
var data = JObject.Parse(response); data = JObject.Parse(response);
isLive = data["media_is_live"].ToString() == "1"; isLive = data["media_is_live"].ToString() == "1";
return new Tuple<bool, string>(isLive, data["media_views"].ToString()); return new Tuple<bool, string>(isLive, data["media_views"].ToString());
break; case StreamNotificationConfig.StreamType.Twitch:
response = await SearchHelper.GetResponseStringAsync($"https://api.twitch.tv/kraken/streams/{Uri.EscapeUriString(stream.Username)}");
data = JObject.Parse(response);
isLive = !string.IsNullOrWhiteSpace(data["stream"].ToString());
return new Tuple<bool, string>(isLive, isLive ? data["stream"]["viewers"].ToString() : "0");
default: default:
break; break;
} }
@ -70,36 +85,19 @@ namespace NadekoBot.Commands {
.Description("Notifies this channel when a certain user starts streaming." + .Description("Notifies this channel when a certain user starts streaming." +
"\n**Usage**: ~hitbox SomeStreamer") "\n**Usage**: ~hitbox SomeStreamer")
.Parameter("username", ParameterType.Unparsed) .Parameter("username", ParameterType.Unparsed)
.Do(async e => { .Do(TrackStream(StreamNotificationConfig.StreamType.Hitbox));
var username = e.GetArg("username");
if (string.IsNullOrWhiteSpace(username))
return;
var stream = new StreamNotificationConfig { cgb.CreateCommand(Module.Prefix + "twitch")
ServerId = e.Server.Id, .Alias(Module.Prefix + "tw")
ChannelId = e.Channel.Id, .Description("Notifies this channel when a certain user starts streaming." +
Username = username, "\n**Usage**: ~twitch SomeStreamer")
Type = StreamNotificationConfig.StreamType.Hitbox, .Parameter("username", ParameterType.Unparsed)
}; .Do(TrackStream(StreamNotificationConfig.StreamType.Twitch));
Tuple<bool, string> data;
try {
data = await GetStreamStatus(stream);
} catch {
await e.Channel.SendMessage(":anger: Stream probably doesn't exist.");
return;
}
var msg = $"Stream is currently **{(data.Item1 ? "ONLINE" : "OFFLINE")}**";
if (data.Item1)
msg += $"\n`Here is the Link:` http://www.hitbox.tv/{stream.Username}";
await e.Channel.SendMessage($":ok: I will notify this channel when status changes.\n{msg}");
NadekoBot.Config.ObservingStreams.Add(stream);
ConfigHandler.SaveConfig();
});
cgb.CreateCommand(Module.Prefix + "hitboxremove") cgb.CreateCommand(Module.Prefix + "removestream")
.Alias(Module.Prefix + "hbr") .Alias(Module.Prefix + "rms")
.Description("Removes hitbox notifications of a certain user on this channel." + .Description("Removes notifications of a certain streamer on this channel." +
"\n**Usage**: ~hbr SomeGuy") "\n**Usage**: ~srm SomeGuy")
.Parameter("username", ParameterType.Unparsed) .Parameter("username", ParameterType.Unparsed)
.Do(async e => { .Do(async e => {
var username = e.GetArg("username")?.ToLower().Trim(); var username = e.GetArg("username")?.ToLower().Trim();
@ -116,12 +114,12 @@ namespace NadekoBot.Commands {
NadekoBot.Config.ObservingStreams.Remove(toRemove); NadekoBot.Config.ObservingStreams.Remove(toRemove);
ConfigHandler.SaveConfig(); ConfigHandler.SaveConfig();
await e.Channel.SendMessage($":ok: Removed `{toRemove.Username}`'s stream from notifications"); await e.Channel.SendMessage($":ok: Removed `{toRemove.Username}`'s stream from hitbox notifications");
}); });
cgb.CreateCommand(Module.Prefix + "hitboxlist") cgb.CreateCommand(Module.Prefix + "liststreams")
.Alias(Module.Prefix + "hbl") .Alias(Module.Prefix + "ls")
.Description("Lists all hitbox streams you are following on this server." + .Description("Lists all streams you are following on this server." +
"\n**Usage**: ~hbl") "\n**Usage**: ~hbl")
.Do(async e => { .Do(async e => {
var streams = NadekoBot.Config.ObservingStreams.Where(snc => var streams = NadekoBot.Config.ObservingStreams.Where(snc =>
@ -136,13 +134,49 @@ namespace NadekoBot.Commands {
var text = string.Join("\n", streamsArray.Select(snc => { var text = string.Join("\n", streamsArray.Select(snc => {
try { try {
return $"{snc.Username}'s stream on {e.Server.GetChannel(e.Channel.Id).Name} channel"; return $"`{snc.Username}`'s stream on **{e.Server.GetChannel(e.Channel.Id).Name}** channel. 【`{snc.Type.ToString()}`】";
} catch { } } catch { }
return ""; return "";
})); }));
await e.Channel.SendMessage($"You are following **{streamsArray.Length}** hitbox streamers on this server.\n" + text); await e.Channel.SendMessage($"You are following **{streamsArray.Length}** hitbox streamers on this server.\n\n" + text);
}); });
} }
private Func<CommandEventArgs, Task> TrackStream(StreamNotificationConfig.StreamType type) =>
async e => {
var username = e.GetArg("username");
if (string.IsNullOrWhiteSpace(username))
return;
var stream = new StreamNotificationConfig {
ServerId = e.Server.Id,
ChannelId = e.Channel.Id,
Username = username,
Type = type,
};
if (NadekoBot.Config.ObservingStreams.Contains(stream)) {
await e.Channel.SendMessage(":anger: I am already notifying that stream on this channel.");
}
Tuple<bool, string> data;
try {
data = await GetStreamStatus(stream);
} catch {
await e.Channel.SendMessage(":anger: Stream probably doesn't exist.");
return;
}
var msg = $"Stream is currently **{(data.Item1 ? "ONLINE" : "OFFLINE")}** with **{data.Item2}** viewers";
if (data.Item1)
if (type == StreamNotificationConfig.StreamType.Hitbox)
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/channels/{stream.Username}";
else if (type == StreamNotificationConfig.StreamType.YoutubeGaming)
msg += $"\n`Here is the Link:` not implemented yet - {stream.Username}";
stream.LastStatus = data.Item1;
await e.Channel.SendMessage($":ok: I will notify this channel when status changes.\n{msg}");
NadekoBot.Config.ObservingStreams.Add(stream);
ConfigHandler.SaveConfig();
};
} }
} }

View File

@ -28,6 +28,11 @@ namespace NadekoBot.Modules {
manager.CreateCommands("", cgb => { manager.CreateCommands("", cgb => {
cgb.AddCheck(Classes.Permissions.PermissionChecker.Instance); cgb.AddCheck(Classes.Permissions.PermissionChecker.Instance);
cgb.CreateCommand("e")
.Do(async e => {
await e.Channel.SendMessage($"{e.User.Name} did it. 😒 🔫");
});
cgb.CreateCommand("\\o\\") cgb.CreateCommand("\\o\\")
.Description("Nadeko replies with /o/") .Description("Nadeko replies with /o/")