Pandas, rip restsharp, stuff

This commit is contained in:
Runebaas 2017-09-19 20:39:49 +02:00
parent 3a5a0df846
commit d88e9a6f18
10 changed files with 182 additions and 73 deletions

View file

@ -1,6 +1,8 @@
using System.Threading.Tasks;
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Discord.Commands;
using RestSharp;
using Newtonsoft.Json;
namespace Geekbot.net.Modules
{
@ -10,10 +12,23 @@ namespace Geekbot.net.Modules
[Summary("Return a random image of a cat.")]
public async Task Say()
{
var catClient = new RestClient("http://random.cat");
var request = new RestRequest("meow.php", Method.GET);
using (var client = new HttpClient())
{
try
{
client.BaseAddress = new Uri("http://random.cat");
var response = await client.GetAsync("/meow.php");
response.EnsureSuccessStatusCode();
catClient.ExecuteAsync<CatResponse>(request, async response => { await ReplyAsync(response.Data.file); });
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}");
}
}
}
}

View file

@ -28,7 +28,6 @@ namespace Geekbot.net.Modules
var dubtriqua = "";
var ns = GetIntArray(number);
Console.WriteLine(ns.Length);
if (ns[7] == ns[6])
{
dubtriqua = "DUBS";

View file

@ -1,7 +1,8 @@
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Discord.Commands;
using RestSharp;
using Newtonsoft.Json;
namespace Geekbot.net.Modules
{
@ -11,11 +12,23 @@ namespace Geekbot.net.Modules
[Summary("Return a random image of a dog.")]
public async Task Say()
{
var dogClient = new RestClient("http://random.dog");
var request = new RestRequest("woof.json", Method.GET);
Console.WriteLine(dogClient.BaseUrl);
using (var client = new HttpClient())
{
try
{
client.BaseAddress = new Uri("http://random.dog");
var response = await client.GetAsync("/woof.json");
response.EnsureSuccessStatusCode();
dogClient.ExecuteAsync<DogResponse>(request, async response => { await ReplyAsync(response.Data.url); });
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}");
}
}
}
}

View file

@ -0,0 +1,23 @@
using System.Threading.Tasks;
using Discord.Commands;
using Geekbot.net.Lib;
namespace Geekbot.net.Modules
{
public class Panda : ModuleBase
{
private readonly IPandaProvider pandaImages;
public Panda(IPandaProvider pandaImages)
{
this.pandaImages = pandaImages;
}
[Command("panda", RunMode = RunMode.Async)]
[Summary("Get a random panda")]
public async Task PandaCommand()
{
await ReplyAsync(pandaImages.GetRandomPanda());
}
}
}