From b81284bfe7adda7a9df2877392ee164740ec3faa Mon Sep 17 00:00:00 2001 From: runebaas Date: Sat, 28 Apr 2018 19:27:41 +0200 Subject: [PATCH] Add !chuck and !dad --- Geekbot.net/Commands/ChuckNorrisJokes.cs | 61 ++++++++++++++++++++++++ Geekbot.net/Commands/DadJokes.cs | 60 +++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 Geekbot.net/Commands/ChuckNorrisJokes.cs create mode 100644 Geekbot.net/Commands/DadJokes.cs diff --git a/Geekbot.net/Commands/ChuckNorrisJokes.cs b/Geekbot.net/Commands/ChuckNorrisJokes.cs new file mode 100644 index 0000000..1ac6c4d --- /dev/null +++ b/Geekbot.net/Commands/ChuckNorrisJokes.cs @@ -0,0 +1,61 @@ +using System; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Discord.Commands; +using Geekbot.net.Lib; +using Newtonsoft.Json; + +namespace Geekbot.net.Commands +{ + public class ChuckNorrisJokes : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public ChuckNorrisJokes(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("chuck", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Randomness)] + [Summary("A random chuck norris joke")] + public async Task Say() + { + try + { + using (var client = new HttpClient()) + { + try + { + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); + var response = await client.GetAsync("https://api.chucknorris.io/jokes/random"); + response.EnsureSuccessStatusCode(); + + var stringResponse = await response.Content.ReadAsStringAsync(); + var data = JsonConvert.DeserializeObject(stringResponse); + await ReplyAsync(data.value); + } + catch (HttpRequestException) + { + await ReplyAsync("Api down..."); + } + } + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + private class ChuckNorrisJokeResponse + { + public string category { get; set; } + public string icon_url { get; set; } + public string id { get; set; } + public string url { get; set; } + public string value { get; set; } + } + } +} \ No newline at end of file diff --git a/Geekbot.net/Commands/DadJokes.cs b/Geekbot.net/Commands/DadJokes.cs new file mode 100644 index 0000000..615f3d5 --- /dev/null +++ b/Geekbot.net/Commands/DadJokes.cs @@ -0,0 +1,60 @@ +using System; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Threading.Tasks; +using Discord; +using Discord.Commands; +using Geekbot.net.Lib; +using Newtonsoft.Json; + +namespace Geekbot.net.Commands +{ + public class DadJokes : ModuleBase + { + private readonly IErrorHandler _errorHandler; + + public DadJokes(IErrorHandler errorHandler) + { + _errorHandler = errorHandler; + } + + [Command("dad", RunMode = RunMode.Async)] + [Remarks(CommandCategories.Randomness)] + [Summary("A random dad joke")] + public async Task Say() + { + try + { + using (var client = new HttpClient()) + { + try + { + client.DefaultRequestHeaders.Accept.Clear(); + client.DefaultRequestHeaders.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json")); + var response = await client.GetAsync("https://icanhazdadjoke.com/"); + response.EnsureSuccessStatusCode(); + + var stringResponse = await response.Content.ReadAsStringAsync(); + var data = JsonConvert.DeserializeObject(stringResponse); + await ReplyAsync(data.joke); + } + catch (HttpRequestException) + { + await ReplyAsync("Api down..."); + } + } + } + catch (Exception e) + { + _errorHandler.HandleCommandException(e, Context); + } + } + + private class DadJokeResponse + { + public string id { get; set; } + public string joke { get; set; } + public string status { get; set; } + } + } +} \ No newline at end of file