forked from cswimr/SeaCogs
88 lines
3.9 KiB
Python
88 lines
3.9 KiB
Python
# _____ _
|
|
# / ____| (_)
|
|
# | (___ ___ __ _ _____ ___ _ __ ___ _ __ ___ ___ _ __
|
|
# \___ \ / _ \/ _` / __\ \ /\ / / | '_ ` _ \| '_ ` _ \ / _ \ '__|
|
|
# ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ |
|
|
# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_|
|
|
|
|
import aiohttp
|
|
import logging
|
|
|
|
from redbot.core import commands, Config
|
|
from redbot.core.bot import Red
|
|
|
|
class Bible(commands.Cog):
|
|
"""Retrieve Bible verses from the API.bible API."""
|
|
|
|
__author__ = "SeaswimmerTheFsh"
|
|
__version__ = "1.0.0"
|
|
|
|
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.group(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"])
|