geekbot/Geekbot.net/Commands/Dog.cs

41 lines
1.3 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.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 Dog : ModuleBase
{
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()
{
2017-09-19 20:39:49 +02:00
using (var client = new HttpClient())
{
try
{
client.BaseAddress = new Uri("http://random.dog");
var response = await client.GetAsync("/woof.json");
response.EnsureSuccessStatusCode();
2017-09-19 20:39:49 +02:00
var stringResponse = await response.Content.ReadAsStringAsync();
var dogFile = JsonConvert.DeserializeObject<DogResponse>(stringResponse);
await ReplyAsync(dogFile.url);
}
catch (HttpRequestException e)
{
await ReplyAsync($"Seems like the dog got lost (error occured)\r\n{e.Message}");
}
}
}
}
public class DogResponse
{
public string url { get; set; }
}
2017-05-06 20:35:31 +02:00
}