v3 .catfact

This commit is contained in:
Toastie (DCS Team) 2024-07-29 18:49:57 +12:00
parent 586f5ba4b0
commit 3a597a49ea
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
2 changed files with 24 additions and 11 deletions

View file

@ -399,10 +399,14 @@ public partial class Searches : EllieModule<SearchesService>
[Cmd] [Cmd]
public async Task Catfact() public async Task Catfact()
{ {
using var http = _httpFactory.CreateClient(); var maybeFact = await _service.GetCatFactAsync();
var response = await http.GetStringAsync("https://catfact.ninja/fact");
if (!maybeFact.TryPickT0(out var fact, out var error))
{
await HandleErrorAsync(error);
return;
}
var fact = JObject.Parse(response)["fact"].ToString();
await Response().Confirm("🐈" + GetText(strs.catfact), fact).SendAsync(); await Response().Confirm("🐈" + GetText(strs.catfact), fact).SendAsync();
} }

View file

@ -2,14 +2,8 @@
using EllieBot.Modules.Searches.Common; using EllieBot.Modules.Searches.Common;
using Newtonsoft.Json; using Newtonsoft.Json;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using OneOf.Types; using System.Text.Json;
using SixLabors.Fonts; using OneOf;
using SixLabors.ImageSharp;
using SixLabors.ImageSharp.Drawing.Processing;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using Color = SixLabors.ImageSharp.Color;
using Image = SixLabors.ImageSharp.Image;
namespace EllieBot.Modules.Searches.Services; namespace EllieBot.Modules.Searches.Services;
@ -458,6 +452,21 @@ public class SearchesService : IEService
return ErrorType.Unknown; return ErrorType.Unknown;
} }
} }
public async Task<OneOf<string, ErrorType>> GetCatFactAsync()
{
using var http = _httpFactory.CreateClient();
var response = await http.GetStringAsync("https://catfact.ninja/fact").ConfigureAwait(false);
var doc = JsonDocument.Parse(response);
if (!doc.RootElement.TryGetProperty("fact", out var factElement))
{
return ErrorType.Unknown;
}
return factElement.ToString();
}
} }
public enum ErrorType public enum ErrorType