private static async Task <string> HandleCommands(DiscordClient client, string reply, string[] words)
{
if (words == null || words.Length < 0)
{
return("An error occurred.");
}
switch (words[0])
{
case "!stream":
if (words.Length > 1)
{
string json = await Program.RequestAsync("https://api.twitch.tv/kraken/streams/" + words[1].ToLower() + "?stream_type=all", true);
if (json != null)
{
var streams = JsonConvert.DeserializeObject <Json.Streams>(json);
if (streams != null)
{
if (streams.Stream == null)
{
reply = "The channel is currently *offline*";
}
else
{
long ticks = DateTime.UtcNow.Ticks - streams.Stream.CreatedAt.Ticks;
TimeSpan ts = new TimeSpan(ticks);
reply = "**[" + NullToEmpty(streams.Stream.Channel.DisplayName) + "]**" + (streams.Stream.Channel.IsPartner ? @"\*" : "") + " " + (streams.Stream.IsPlaylist ? "(Playlist)" : "")
+ "\n**Title**: " + NullToEmpty(streams.Stream.Channel.Status).Replace("*", @"\*")
+ "\n**Game:** " + NullToEmpty(streams.Stream.Game) + "\n**Viewers**: " + streams.Stream.Viewers
+ "\n**Uptime**: " + ts.ToString(@"d' day" + (ts.Days == 1 ? "" : "s") + @" 'hh\:mm\:ss")
+ "\n**Quality**: " + streams.Stream.VideoHeight + "p" + Math.Ceiling(streams.Stream.FramesPerSecond);
}
}
}
}
else
{
reply = "**Usage:** !stream channel";
}
break;
case "!channel":
if (words.Length > 1)
{
string json = await Program.RequestAsync("https://api.twitch.tv/kraken/channels/" + words[1].ToLower(), true);
if (json != null)
{
var channel = JsonConvert.DeserializeObject <Json.Channel>(json);
if (channel != null && channel.DisplayName != null)
{
reply = "**[" + NullToEmpty(channel.DisplayName) + "]**"
+ "\n**Partner**: " + (channel.IsPartner ? "Yes" : "No")
+ "\n**Title**: " + NullToEmpty(channel.Status).Replace("*", @"\*")
+ "\n**Registered**: " + NullToEmpty(channel.Registered.ToString("yyyy-MM-dd HH:mm")) + " UTC"
+ "\n**Followers**: " + channel.Followers;
}
}
}
else
{
reply = "**Usage:** !channel channel";
}
break;
case "!source":
reply = "https://github.com/Ianchandler1990/BotVentic";
break;
case "!frozen":
if (words.Length >= 2 && words[1] != "pizza")
{
break;
}
// Fall through to frozenpizza
goto case "!frozenpizza";
case "!frozenpizza":
reply = "*starts making a frozen pizza*";
break;
case "!update":
if (words.Length > 1)
{
switch (words[1])
{
case "emotes":
await Program.UpdateAllEmotesAsync();
reply = "*updated list of known emotes*";
break;
}
}
break;
case "!bot":
int users = 0;
foreach (var server in client.Servers)
{
users += server.UserCount;
}
try
{
reply = $"Connected via `{client.GatewaySocket.Host}`\n";
reply += $"Memory Usage is {Math.Ceiling(System.Diagnostics.Process.GetCurrentProcess().WorkingSet64 / 1024.0)} KB\n";
reply += $"Serving {users} users on {client.Servers.Count()} servers.\n";
reply += $"Uptime {(DateTime.UtcNow - Program.StartedAt).ToString(@"d\ \d\a\y\s\,\ h\ \h\o\u\r\s")}\n";
reply += "Available commands: `!bot` `!update emotes` `!frozen pizza` `!foodporn` `!source` `!stream <Twitch channel name>` `!channel <Twitch channel name>`";
}
catch (Exception ex) when(ex is ArgumentNullException || ex is OverflowException || ex is PlatformNotSupportedException)
{
reply = $"Error: {ex.Message}";
Console.WriteLine(ex.ToString());
}
break;
case "!foodporn":
try
{
var rnd = new Random();
var page = rnd.Next(1, 10);
string downloadString = await Program.RequestAsync($"http://foodporndaily.com/explore/food/page/{page}/");
string regexImgSrc = @"<img[^>]*?src\s*=\s*[""']?([^'"" >]+?)[ '""][^>]*?>";
var matchesImgSrc = Regex.Matches(downloadString, regexImgSrc, RegexOptions.IgnoreCase | RegexOptions.Singleline);
int image = rnd.Next(1, matchesImgSrc.Count);
reply = matchesImgSrc[image].Groups[1].Value;
}
catch (Exception ex)
{
reply = $"Could not get the foodporn image. Error: {ex.Message }";
Console.WriteLine(ex.ToString());
}
break;
}
return(reply);
}