refactored .bible and .quran, moved to their own folder and created ReligiousApiService for their logic

This commit is contained in:
Toastie (DCS Team) 2024-07-31 19:49:47 +12:00
parent 2764bb390b
commit 219c122f1c
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
9 changed files with 178 additions and 124 deletions

View file

@ -77,7 +77,6 @@ csharp_style_var_when_type_is_apparent = true:suggestion
# Expression-bodied members
csharp_style_expression_bodied_accessors = true:suggestion
csharp_style_expression_bodied_constructors = when_on_single_line:suggestion
csharp_style_expression_bodied_indexers = true:suggestion
csharp_style_expression_bodied_lambdas = true:suggestion
csharp_style_expression_bodied_local_functions = true:suggestion

View file

@ -0,0 +1,13 @@
using System.Text.Json.Serialization;
namespace EllieBot.Modules.Searches;
public class BibleVerse
{
[JsonPropertyName("book_name")]
public required string BookName { get; set; }
public required int Chapter { get; set; }
public required int Verse { get; set; }
public required string Text { get; set; }
}

View file

@ -0,0 +1,7 @@
namespace EllieBot.Modules.Searches;
public class BibleVerses
{
public string? Error { get; set; }
public BibleVerse[]? Verses { get; set; }
}

View file

@ -0,0 +1,20 @@
#nullable disable
using System.Text.Json.Serialization;
namespace EllieBot.Modules.Searches;
public sealed class QuranAyah
{
[JsonPropertyName("number")]
public int Number { get; set; }
[JsonPropertyName("audio")]
public string Audio { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("text")]
public string Text { get; set; }
}

View file

@ -0,0 +1,15 @@
using System.Text.Json.Serialization;
namespace EllieBot.Modules.Searches;
public sealed class QuranResponse<T>
{
[JsonPropertyName("code")]
public required int Code { get; set; }
[JsonPropertyName("status")]
public required string Status { get; set; }
[JsonPropertyName("data")]
public required T[] Data { get; set; }
}

View file

@ -0,0 +1,63 @@
using EllieBot.Modules.Searches.Common;
using OneOf;
using OneOf.Types;
using System.Net;
using System.Net.Http.Json;
namespace EllieBot.Modules.Searches;
public sealed class ReligiousApiService : IEService
{
private readonly IHttpClientFactory _httpFactory;
public ReligiousApiService(IHttpClientFactory httpFactory)
{
_httpFactory = httpFactory;
}
public async Task<OneOf<BibleVerse, Error<string>>> GetBibleVerseAsync(string book, string chapterAndVerse)
{
if (string.IsNullOrWhiteSpace(book) || string.IsNullOrWhiteSpace(chapterAndVerse))
return new Error<string>("Invalid input.");
book = Uri.EscapeDataString(book);
chapterAndVerse = Uri.EscapeDataString(chapterAndVerse);
using var http = _httpFactory.CreateClient();
try
{
var res = await http.GetFromJsonAsync<BibleVerses>($"https://bible-api.com/{book} {chapterAndVerse}");
if (res is null || res.Error is not null || res.Verses is null || res.Verses.Length == 0)
{
return new Error<string>(res?.Error ?? "No verse found.");
}
return res.Verses[0];
}
catch (HttpRequestException ex) when (ex.StatusCode == HttpStatusCode.NotFound)
{
return new Error<string>("No verse found.");
}
}
public async Task<OneOf<QuranResponse<QuranAyah>, Error<LocStr>>> GetQuranVerseAsync(string ayah)
{
if (string.IsNullOrWhiteSpace(ayah))
return new Error<LocStr>(strs.invalid_input);
ayah = Uri.EscapeDataString(ayah);
using var http = _httpFactory.CreateClient();
var res = await http.GetFromJsonAsync<QuranResponse<QuranAyah>>(
$"https://api.alquran.cloud/v1/ayah/{ayah}/editions/en.asad,ar.alafasy");
if (res is null or not { Code: 200 })
{
return new Error<LocStr>(strs.not_found);
}
return res;
}
}

View file

@ -0,0 +1,60 @@
namespace EllieBot.Modules.Searches;
public partial class Searches
{
public partial class ReligiousCommands : EllieModule<ReligiousApiService>
{
private readonly IHttpClientFactory _httpFactory;
public ReligiousCommands(IHttpClientFactory httpFactory)
=> _httpFactory = httpFactory;
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task Bible(string book, string chapterAndVerse)
{
var res = await _service.GetBibleVerseAsync(book, chapterAndVerse);
if (!res.TryPickT0(out var verse, out var error))
{
await Response().Error(error.Value).SendAsync();
return;
}
await Response()
.Embed(_sender.CreateEmbed()
.WithOkColor()
.WithTitle($"{verse.BookName} {verse.Chapter}:{verse.Verse}")
.WithDescription(verse.Text))
.SendAsync();
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task Quran(string ayah)
{
var res = await _service.GetQuranVerseAsync(ayah);
if (!res.TryPickT0(out var qr, out var error))
{
await Response().Error(error.Value).SendAsync();
return;
}
var english = qr.Data[0];
var arabic = qr.Data[1];
using var http = _httpFactory.CreateClient();
await using var audio = await http.GetStreamAsync(arabic.Audio);
await Response()
.Embed(_sender.CreateEmbed()
.WithOkColor()
.AddField("Arabic", arabic.Text)
.AddField("English", english.Text)
.WithFooter(arabic.Number.ToString()))
.File(audio, Uri.EscapeDataString(ayah) + ".mp3")
.SendAsync();
}
}
}

View file

@ -1,103 +0,0 @@
#nullable disable
using EllieBot.Modules.Searches.Common;
using System.Net.Http.Json;
using System.Text.Json.Serialization;
namespace EllieBot.Modules.Searches;
public partial class Searches
{
public partial class ReligiousCommands : EllieModule
{
private readonly IHttpClientFactory _httpFactory;
public ReligiousCommands(IHttpClientFactory httpFactory)
{
_httpFactory = httpFactory;
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task Bible(string book, string chapterAndVerse)
{
var obj = new BibleVerses();
try
{
using var http = _httpFactory.CreateClient();
obj = await http.GetFromJsonAsync<BibleVerses>($"https://bible-api.com/{book} {chapterAndVerse}");
}
catch
{
}
if (obj.Error is not null || obj.Verses is null || obj.Verses.Length == 0)
await Response().Error(obj.Error ?? "No verse found.").SendAsync();
else
{
var v = obj.Verses[0];
await Response()
.Embed(_sender.CreateEmbed()
.WithOkColor()
.WithTitle($"{v.BookName} {v.Chapter}:{v.Verse}")
.WithDescription(v.Text))
.SendAsync();
}
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task Quran(string ayah)
{
using var http = _httpFactory.CreateClient();
var obj = await http.GetFromJsonAsync<QuranResponse<QuranAyah>>($"https://api.alquran.cloud/v1/ayah/{Uri.EscapeDataString(ayah)}/editions/en.asad,ar.alafasy");
if(obj is null or not { Code: 200 })
{
await Response().Error("No verse found.").SendAsync();
return;
}
var english = obj.Data[0];
var arabic = obj.Data[1];
await using var audio = await http.GetStreamAsync(arabic.Audio);
await Response()
.Embed(_sender.CreateEmbed()
.WithOkColor()
.AddField("Arabic", arabic.Text)
.AddField("English", english.Text)
.WithFooter(arabic.Number.ToString()))
.File(audio, Uri.EscapeDataString(ayah) + ".mp3")
.SendAsync();
}
}
}
public sealed class QuranResponse<T>
{
[JsonPropertyName("code")]
public int Code { get; set; }
[JsonPropertyName("status")]
public string Status { get; set; }
[JsonPropertyName("data")]
public T[] Data { get; set; }
}
public sealed class QuranAyah
{
[JsonPropertyName("number")]
public int Number { get; set; }
[JsonPropertyName("audio")]
public string Audio { get; set; }
[JsonPropertyName("name")]
public string Name { get; set; }
[JsonPropertyName("text")]
public string Text { get; set; }
}

View file

@ -1,20 +0,0 @@
#nullable disable
using System.Text.Json.Serialization;
namespace EllieBot.Modules.Searches.Common;
public class BibleVerses
{
public string Error { get; set; }
public BibleVerse[] Verses { get; set; }
}
public class BibleVerse
{
[JsonPropertyName("book_name")]
public string BookName { get; set; }
public int Chapter { get; set; }
public int Verse { get; set; }
public string Text { get; set; }
}