From 267e048bc06ccc4734eb4e43e83ec9fddac6dee8 Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Thu, 1 Feb 2024 17:50:35 -0500 Subject: [PATCH] feat(bible): working on the verse command --- bible/bible.py | 68 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/bible/bible.py b/bible/bible.py index 90b4d65..c0f0ec9 100644 --- a/bible/bible.py +++ b/bible/bible.py @@ -5,9 +5,10 @@ # ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ | # |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_| +import aiohttp import logging -from redbot.core import commands +from redbot.core import commands, Config from redbot.core.bot import Red class Bible(commands.Cog): @@ -19,4 +20,69 @@ class Bible(commands.Cog): def __init__(self, bot: Red): super().__init__() self.bot = bot + self.session = aiohttp.ClientSession() + self.config = Config.get_conf(self, identifier=481923957134912, force_registration=True) self.logger = logging.getLogger("red.sea.bible") + self.config.register_global(bible="de4e12af7f28f599-01") + self.config.register_user(bible=None) + + async def translate_book_name(self, bible_id: str, book_name: str) -> str: + """Translate a book name to a book ID.""" + book_name = book_name.title() + books = await self._get_books(bible_id) + for book in books: + if book_name == book['abbreviation'] or book_name == book['name']: + return book['id'] + raise ValueError(f"Book {book_name} not found.") + + async def _get_passage(self, bible_id: str, passage_id: str) -> dict: + """Get a Bible passage from the API.bible API.""" + url = f"https://api.scripture.api.bible/v1/bibles/{bible_id}/passages/{passage_id}" + headers = self.bot.get_shared_api_tokens("api.bible") + async with self.session.get(url, headers=headers) as response: + data = await response.json() + return data["data"] + + async def _get_verse(self, bible_id: str, book_id: str, chapter: int, verse: int) -> dict: + """Get a Bible verse from the API.bible API.""" + passage_id = f"{book_id}.{chapter}.{verse}" + return await self._get_passage(bible_id, passage_id) + + async def _get_books(self, bible_id: str) -> dict: + """Get the books of the Bible from the API.bible API.""" + url = f"https://api.scripture.api.bible/v1/bibles/{bible_id}/books" + headers = self.bot.get_shared_api_tokens("api.bible") + async with self.session.get(url, headers=headers) as response: + data = await response.json() + return data["data"] + + async def _get_chapters(self, bible_id: str, book_id: str) -> dict: + """Get the chapters of a book of the Bible from the API.bible API.""" + url = f"https://api.scripture.api.bible/v1/bibles/{bible_id}/books/{book_id}/chapters" + headers = self.bot.get_shared_api_tokens("api.bible") + async with self.session.get(url, headers=headers) as response: + data = await response.json() + return data["data"] + + async def _get_verses(self, bible_id: str, book_id: str, chapter: int) -> dict: + """Get the verses of a chapter of a book of the Bible from the API.bible API.""" + url = f"https://api.scripture.api.bible/v1/bibles/{bible_id}/books/{book_id}/chapters/{chapter}/verses" + headers = self.bot.get_shared_api_tokens("api.bible") + async with self.session.get(url, headers=headers) as response: + data = await response.json() + return data["data"] + + @commands.command(autohelp=True) + async def bible(self, ctx: commands.Context): + """Core command for the Bible cog.""" + + @bible.command(name="verse") + async def bible_verse(self, ctx: commands.Context, book: str, chapter: int, verse: int): + """Get a Bible verse.""" + bible_id = await self.config.bible() + try: + book_id = await self.translate_book_name(bible_id, book) + except ValueError as e: + await ctx.send(str(e)) + passage = await self._get_verse(bible_id, book_id, chapter, verse) + await ctx.send(passage["content"])