2017-09-19 20:39:49 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Threading.Tasks;
|
2017-09-14 22:11:19 +02:00
|
|
|
|
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;
|
2017-09-14 22:11:19 +02:00
|
|
|
|
|
2017-10-02 21:57:48 +02:00
|
|
|
|
namespace Geekbot.net.Commands
|
2017-09-14 22:11:19 +02:00
|
|
|
|
{
|
|
|
|
|
public class Cat : ModuleBase
|
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
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.")]
|
2017-09-14 22:11:19 +02:00
|
|
|
|
public async Task Say()
|
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
try
|
2017-09-19 20:39:49 +02:00
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
using (var client = new HttpClient())
|
2017-09-19 20:39:49 +02:00
|
|
|
|
{
|
2017-10-26 00:55:04 +02:00
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
client.BaseAddress = new Uri("http://random.cat");
|
|
|
|
|
var response = await client.GetAsync("/meow.php");
|
|
|
|
|
response.EnsureSuccessStatusCode();
|
2017-09-14 22:11:19 +02:00
|
|
|
|
|
2017-10-26 00:55:04 +02:00
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
}
|
2017-10-26 00:55:04 +02:00
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
|
|
|
|
_errorHandler.HandleCommandException(e, Context);
|
|
|
|
|
}
|
2017-09-14 22:11:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class CatResponse
|
|
|
|
|
{
|
|
|
|
|
public string file { get; set; }
|
|
|
|
|
}
|
2017-04-12 21:49:04 +02:00
|
|
|
|
}
|