2024-02-01 17:14:06 -05:00
|
|
|
# _____ _
|
|
|
|
# / ____| (_)
|
|
|
|
# | (___ ___ __ _ _____ ___ _ __ ___ _ __ ___ ___ _ __
|
|
|
|
# \___ \ / _ \/ _` / __\ \ /\ / / | '_ ` _ \| '_ ` _ \ / _ \ '__|
|
|
|
|
# ____) | __/ (_| \__ \\ V V /| | | | | | | | | | | | __/ |
|
|
|
|
# |_____/ \___|\__,_|___/ \_/\_/ |_|_| |_| |_|_| |_| |_|\___|_|
|
|
|
|
|
2024-02-01 17:50:35 -05:00
|
|
|
import aiohttp
|
2024-02-01 18:05:23 -05:00
|
|
|
import json
|
2024-02-01 17:14:06 -05:00
|
|
|
import logging
|
|
|
|
|
2024-02-01 18:20:48 -05:00
|
|
|
from discord import Embed
|
2024-02-01 17:50:35 -05:00
|
|
|
from redbot.core import commands, Config
|
2024-02-01 17:14:06 -05:00
|
|
|
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
|
2024-02-01 17:50:35 -05:00
|
|
|
self.session = aiohttp.ClientSession()
|
|
|
|
self.config = Config.get_conf(self, identifier=481923957134912, force_registration=True)
|
2024-02-01 17:14:06 -05:00
|
|
|
self.logger = logging.getLogger("red.sea.bible")
|
2024-02-01 17:50:35 -05:00
|
|
|
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']:
|
2024-02-01 18:23:00 -05:00
|
|
|
return book['id']
|
2024-02-01 17:50:35 -05:00
|
|
|
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."""
|
2024-02-01 18:07:55 -05:00
|
|
|
url = f"https://api.scripture.api.bible/v1/bibles/{bible_id}/passages/{passage_id}?content-type=text&include-notes=false&include-titles=false&include-chapter-numbers=false&include-verse-numbers=true&include-verse-spans=false&use-org-id=false"
|
2024-02-01 17:52:45 -05:00
|
|
|
headers = await self.bot.get_shared_api_tokens("api.bible")
|
2024-02-01 18:01:26 -05:00
|
|
|
async with self.session.get(url, headers=headers) as response:
|
2024-02-01 17:50:35 -05:00
|
|
|
data = await response.json()
|
2024-02-01 18:17:30 -05:00
|
|
|
self.logger.debug(passage_id)
|
2024-02-01 18:05:23 -05:00
|
|
|
self.logger.debug(json.dumps(data))
|
2024-02-01 17:50:35 -05:00
|
|
|
return data["data"]
|
|
|
|
|
|
|
|
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"
|
2024-02-01 17:52:45 -05:00
|
|
|
headers = await self.bot.get_shared_api_tokens("api.bible")
|
2024-02-01 17:50:35 -05:00
|
|
|
async with self.session.get(url, headers=headers) as response:
|
|
|
|
data = await response.json()
|
|
|
|
return data["data"]
|
|
|
|
|
2024-02-01 17:51:15 -05:00
|
|
|
@commands.group(autohelp=True)
|
2024-02-01 17:50:35 -05:00
|
|
|
async def bible(self, ctx: commands.Context):
|
|
|
|
"""Core command for the Bible cog."""
|
|
|
|
|
|
|
|
@bible.command(name="verse")
|
2024-02-01 18:23:00 -05:00
|
|
|
async def bible_verse(self, ctx: commands.Context, book: str, chapter: int, verse: int) -> str:
|
2024-02-01 17:50:35 -05:00
|
|
|
"""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))
|
2024-02-01 17:59:19 -05:00
|
|
|
return
|
2024-02-01 18:00:22 -05:00
|
|
|
passage = await self._get_passage(bible_id, f"{book_id}.{chapter}.{verse}")
|
2024-02-01 17:50:35 -05:00
|
|
|
await ctx.send(passage["content"])
|
2024-02-01 18:14:42 -05:00
|
|
|
|
|
|
|
@bible.command(name="passage")
|
|
|
|
async def bible_passage(self, ctx: commands.Context, book: str, passage: str):
|
|
|
|
"""Get a Bible passage.
|
|
|
|
|
|
|
|
Example usage:
|
|
|
|
`[p]bible passage John 3:16-3:17`"""
|
|
|
|
bible_id = await self.config.bible()
|
2024-02-01 18:20:48 -05:00
|
|
|
|
2024-02-01 18:14:42 -05:00
|
|
|
try:
|
2024-02-01 18:23:00 -05:00
|
|
|
book_id = await self.translate_book_name(bible_id, book)
|
2024-02-01 18:14:42 -05:00
|
|
|
except ValueError as e:
|
|
|
|
await ctx.send(str(e))
|
|
|
|
return
|
2024-02-01 18:20:48 -05:00
|
|
|
|
2024-02-01 18:14:42 -05:00
|
|
|
from_verse, to_verse = passage.replace(":", ".").split("-")
|
2024-02-01 18:20:48 -05:00
|
|
|
if '.' not in to_verse:
|
|
|
|
to_verse = f"{from_verse.split('.')[0]}.{to_verse}"
|
|
|
|
|
2024-02-01 18:14:42 -05:00
|
|
|
passage = await self._get_passage(bible_id, f"{book_id}.{from_verse}-{book_id}.{to_verse}")
|
2024-02-01 18:20:48 -05:00
|
|
|
|
2024-02-01 18:23:00 -05:00
|
|
|
embed = Embed(title=f"{passage['reference']}", description=passage["content"].replace('¶ ', ''), color=await self.bot.get_embed_color(ctx.channel))
|
2024-02-01 18:20:48 -05:00
|
|
|
await ctx.send(embed=embed)
|