Added cat pics because cat pics are a must have feature

This commit is contained in:
Deukhoofd 2018-05-18 14:05:02 +02:00
parent 32fa37b633
commit a83d56f1b7
No known key found for this signature in database
GPG Key ID: B4C087AC81641654
3 changed files with 52 additions and 1 deletions

View File

@ -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<Stream> 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;
}
}
}

View File

@ -1,5 +1,7 @@
using System; using System;
using System.Net;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web;
using DeukBot4.APIHandlers; using DeukBot4.APIHandlers;
using DeukBot4.MessageHandlers.CommandHandler.RequestStructure; using DeukBot4.MessageHandlers.CommandHandler.RequestStructure;
using DeukBot4.MessageHandlers.Permissions; using DeukBot4.MessageHandlers.Permissions;
@ -77,5 +79,20 @@ namespace DeukBot4.MessageHandlers.CommandHandler
{ {
await request.SendMessageAsync(await CatFactsApi.GetRandomCatFact()); 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");
}
} }
} }

View File

@ -1,6 +1,8 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Web;
using DeukBot4.APIHandlers;
using DeukBot4.Utilities; using DeukBot4.Utilities;
using Discord; using Discord;
using Discord.WebSocket; using Discord.WebSocket;
@ -68,7 +70,8 @@ namespace DeukBot4.MessageHandlers
var lowerSplit = lowerCasedContent.Split(' '); var lowerSplit = lowerCasedContent.Split(' ');
if (lowerSplit.Select(s => Lehvenstein.LevenshteinDistance(s, "delta")).Any(diff => diff <= 1)) 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");
} }
} }