2017-09-14 22:11:19 +02:00
|
|
|
|
using System;
|
2017-09-19 20:39:49 +02:00
|
|
|
|
using System.Net.Http;
|
2017-09-14 22:11:19 +02:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Discord.Commands;
|
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 Dog : ModuleBase
|
|
|
|
|
{
|
2017-09-15 22:56:03 +02:00
|
|
|
|
[Command("dog", RunMode = RunMode.Async)]
|
|
|
|
|
[Summary("Return a random image of a dog.")]
|
2017-09-14 22:11:19 +02:00
|
|
|
|
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-14 22:11:19 +02:00
|
|
|
|
|
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}");
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-09-14 22:11:19 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class DogResponse
|
|
|
|
|
{
|
|
|
|
|
public string url { get; set; }
|
|
|
|
|
}
|
2017-05-06 20:35:31 +02:00
|
|
|
|
}
|