geekbot/Geekbot.net/Commands/Randomness/Dog/Dog.cs

54 lines
1.8 KiB
C#
Raw Normal View History

using System;
2017-09-19 20:39:49 +02:00
using System.Net.Http;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
2017-10-12 16:34:10 +02:00
using Geekbot.net.Lib;
using Geekbot.net.Lib.ErrorHandling;
2017-09-19 20:39:49 +02:00
using Newtonsoft.Json;
namespace Geekbot.net.Commands.Randomness.Dog
{
public class Dog : ModuleBase
{
private readonly IErrorHandler _errorHandler;
2017-12-29 01:53:50 +01:00
public Dog(IErrorHandler errorHandler)
{
_errorHandler = errorHandler;
}
2017-12-29 01:53:50 +01:00
2017-09-15 22:56:03 +02:00
[Command("dog", 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 dog.")]
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.dog");
var response = await client.GetAsync("/woof.json");
response.EnsureSuccessStatusCode();
var stringResponse = await response.Content.ReadAsStringAsync();
var dogFile = JsonConvert.DeserializeObject<DogResponseDto>(stringResponse);
var eb = new EmbedBuilder();
2018-04-30 23:44:19 +02:00
eb.ImageUrl = dogFile.Url;
await ReplyAsync("", false, eb.Build());
}
catch (HttpRequestException e)
{
await ReplyAsync($"Seems like the dog got lost (error occured)\r\n{e.Message}");
}
2017-09-19 20:39:49 +02:00
}
}
catch (Exception e)
{
_errorHandler.HandleCommandException(e, Context);
}
}
}
2017-05-06 20:35:31 +02:00
}