mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-12 02:08:27 -04:00
dev: refactored .bible and .quran, moved to their own folder and created ReligiousApiService for their logic
This commit is contained in:
@@ -0,0 +1,13 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace NadekoBot.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; }
|
||||
}
|
@@ -0,0 +1,7 @@
|
||||
namespace NadekoBot.Modules.Searches;
|
||||
|
||||
public class BibleVerses
|
||||
{
|
||||
public string? Error { get; set; }
|
||||
public BibleVerse[]? Verses { get; set; }
|
||||
}
|
20
src/NadekoBot/Modules/Searches/Religious/Common/QuranAyah.cs
Normal file
20
src/NadekoBot/Modules/Searches/Religious/Common/QuranAyah.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
#nullable disable
|
||||
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace NadekoBot.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; }
|
||||
}
|
@@ -0,0 +1,15 @@
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
namespace NadekoBot.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; }
|
||||
}
|
@@ -0,0 +1,63 @@
|
||||
using NadekoBot.Modules.Searches.Common;
|
||||
using OneOf;
|
||||
using OneOf.Types;
|
||||
using System.Net;
|
||||
using System.Net.Http.Json;
|
||||
|
||||
namespace NadekoBot.Modules.Searches;
|
||||
|
||||
public sealed class ReligiousApiService : INService
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
@@ -0,0 +1,60 @@
|
||||
namespace NadekoBot.Modules.Searches;
|
||||
|
||||
public partial class Searches
|
||||
{
|
||||
public partial class ReligiousCommands : NadekoModule<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();
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user