geekbot/Geekbot.net/Commands/Cat.cs

55 lines
1.7 KiB
C#
Raw Normal View History

2017-09-19 20:39:49 +02:00
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Discord.Commands;
2017-10-12 16:34:10 +02:00
using Geekbot.net.Lib;
2017-09-19 20:39:49 +02:00
using Newtonsoft.Json;
namespace Geekbot.net.Commands
{
public class Cat : ModuleBase
{
private readonly IErrorHandler _errorHandler;
public Cat(IErrorHandler errorHandler)
{
_errorHandler = errorHandler;
}
2017-09-15 22:56:03 +02:00
[Command("cat", RunMode = RunMode.Async)]
2017-10-12 16:34:10 +02:00
[Remarks(CommandCategories.Randomness)]
2017-09-15 22:56:03 +02:00
[Summary("Return a random image of a cat.")]
public async Task Say()
{
try
2017-09-19 20:39:49 +02:00
{
using (var client = new HttpClient())
2017-09-19 20:39:49 +02:00
{
try
{
client.BaseAddress = new Uri("http://random.cat");
var response = await client.GetAsync("/meow.php");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
var catFile = JsonConvert.DeserializeObject<CatResponse>(stringResponse);
await ReplyAsync(catFile.file);
}
catch (HttpRequestException e)
{
await ReplyAsync($"Seems like the dog cought the cat (error occured)\r\n{e.Message}");
}
2017-09-19 20:39:49 +02:00
}
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
}
public class CatResponse
{
public string file { get; set; }
}
2017-04-12 21:49:04 +02:00
}