feat(bible): working on the verse command

This commit is contained in:
Seaswimmer 2024-02-01 17:50:35 -05:00
parent 4c98d00641
commit 267e048bc0
Signed by untrusted user: cswimr
GPG key ID: B8953EC01E5C4063

View file

@ -5,9 +5,10 @@
# ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ | # ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ |
# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_| # |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_|
import aiohttp
import logging import logging
from redbot.core import commands from redbot.core import commands, Config
from redbot.core.bot import Red from redbot.core.bot import Red
class Bible(commands.Cog): class Bible(commands.Cog):
@ -19,4 +20,69 @@ class Bible(commands.Cog):
def __init__(self, bot: Red): def __init__(self, bot: Red):
super().__init__() super().__init__()
self.bot = bot 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.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"])