From a83d56f1b7f9ade9208648076345507c7d974d92 Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Fri, 18 May 2018 14:05:02 +0200 Subject: [PATCH] Added cat pics because cat pics are a must have feature --- DeukBot4/APIHandlers/CatPicHandler.cs | 31 +++++++++++++++++++ .../Commands/GeneralCommands.cs | 17 ++++++++++ DeukBot4/MessageHandlers/MainHandler.cs | 5 ++- 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 DeukBot4/APIHandlers/CatPicHandler.cs diff --git a/DeukBot4/APIHandlers/CatPicHandler.cs b/DeukBot4/APIHandlers/CatPicHandler.cs new file mode 100644 index 0000000..aabe352 --- /dev/null +++ b/DeukBot4/APIHandlers/CatPicHandler.cs @@ -0,0 +1,31 @@ +using System; +using System.IO; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using System.Web; +using Discord; + +namespace DeukBot4.APIHandlers +{ + public class CatPicHandler + { + private const string URL = "https://cataas.com"; + + public static async Task GetCatPicture(string saying = null) + { + var fullUrl = "/cat"; + if (saying != null) + fullUrl += "/says/" + HttpUtility.UrlPathEncode(saying); + + var client = new HttpClient {BaseAddress = new Uri(URL)}; + client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + var response = await client.GetAsync(fullUrl); // Blocking call! + if (!response.IsSuccessStatusCode) + throw new Exception("failure"); + + var res = await response.Content.ReadAsStreamAsync(); + return res; + } + } +} \ No newline at end of file diff --git a/DeukBot4/MessageHandlers/CommandHandler/Commands/GeneralCommands.cs b/DeukBot4/MessageHandlers/CommandHandler/Commands/GeneralCommands.cs index 07582af..487a23a 100644 --- a/DeukBot4/MessageHandlers/CommandHandler/Commands/GeneralCommands.cs +++ b/DeukBot4/MessageHandlers/CommandHandler/Commands/GeneralCommands.cs @@ -1,5 +1,7 @@ using System; +using System.Net; using System.Threading.Tasks; +using System.Web; using DeukBot4.APIHandlers; using DeukBot4.MessageHandlers.CommandHandler.RequestStructure; using DeukBot4.MessageHandlers.Permissions; @@ -77,5 +79,20 @@ namespace DeukBot4.MessageHandlers.CommandHandler { await request.SendMessageAsync(await CatFactsApi.GetRandomCatFact()); } + + [Command("catpic", PermissionLevel.Everyone)] + [CommandHelp("Returns a random cat picture", "Returns a random cat picture")] + [CommandParameters(ParameterMatcher.ParameterType.Remainder)] + public async Task RandomCatPc(CommandRequest request) + { + if (!string.IsNullOrWhiteSpace(request.Parameters[0].AsString())) + { + var saying = request.Parameters[0].AsString(); + await request.OriginalMessage.Channel.SendFileAsync(await CatPicHandler.GetCatPicture(saying), "cat_pic.png"); + return; + } + await request.OriginalMessage.Channel.SendFileAsync(await CatPicHandler.GetCatPicture(), "cat_pic.png"); + } + } } \ No newline at end of file diff --git a/DeukBot4/MessageHandlers/MainHandler.cs b/DeukBot4/MessageHandlers/MainHandler.cs index 3c33674..efbf17f 100644 --- a/DeukBot4/MessageHandlers/MainHandler.cs +++ b/DeukBot4/MessageHandlers/MainHandler.cs @@ -1,6 +1,8 @@ using System; using System.Linq; using System.Threading.Tasks; +using System.Web; +using DeukBot4.APIHandlers; using DeukBot4.Utilities; using Discord; using Discord.WebSocket; @@ -68,7 +70,8 @@ namespace DeukBot4.MessageHandlers var lowerSplit = lowerCasedContent.Split(' '); if (lowerSplit.Select(s => Lehvenstein.LevenshteinDistance(s, "delta")).Any(diff => diff <= 1)) { - await message.Channel.SendMessageAsync("uhh excuse me it's called Origin and it's an art"); + var warning = "uhh excuse me it's called\n Origin and it's an art"; + await message.Channel.SendFileAsync(await CatPicHandler.GetCatPicture(warning), "cat_pic.png"); } }