forked from blizzthewolf/SeaCogs
feat(bible): added bible translation to the output of bible random and bible passage
This commit is contained in:
parent
6be92f05db
commit
3813d3de02
2 changed files with 44 additions and 2 deletions
|
@ -15,6 +15,7 @@ from redbot.core.bot import Red
|
|||
from redbot.core.utils.chat_formatting import error
|
||||
|
||||
import bible.errors
|
||||
from bible.models import Version
|
||||
|
||||
|
||||
class Bible(commands.Cog):
|
||||
|
@ -46,6 +47,31 @@ class Bible(commands.Cog):
|
|||
return book["id"]
|
||||
raise ValueError(error(f"Book {book_name} not found."))
|
||||
|
||||
async def get_version(self, bible_id: str) -> Version:
|
||||
"""Retrieve the version of the Bible being used."""
|
||||
url = f"https://api.scripture.api.bible/v1/bibles/{bible_id}"
|
||||
headers = await self.bot.get_shared_api_tokens("api.bible")
|
||||
|
||||
async with self.session.get(url, headers=headers) as response:
|
||||
data = await response.json()
|
||||
self.logger.debug(
|
||||
"get_version executed with a response code of: %s",
|
||||
response.status,
|
||||
)
|
||||
if response.status == 401:
|
||||
raise bible.errors.Unauthorized()
|
||||
if response.status == 403:
|
||||
raise bible.errors.BibleAccessError()
|
||||
if response.status == 503:
|
||||
raise bible.errors.ServiceUnavailable()
|
||||
return Version(
|
||||
data["data"]["abbreviation"],
|
||||
data["data"]["language"],
|
||||
data["data"]["abbreviationLocal"],
|
||||
data["data"]["languageLocal"],
|
||||
data["data"]["copyright"],
|
||||
)
|
||||
|
||||
async def _get_passage(
|
||||
self,
|
||||
ctx: commands.Context,
|
||||
|
@ -180,6 +206,7 @@ class Bible(commands.Cog):
|
|||
return
|
||||
|
||||
try:
|
||||
version = await self.get_version(bible_id)
|
||||
if len(passage.split("-")) == 2:
|
||||
from_verse, to_verse = passage.replace(":", ".").split("-")
|
||||
if "." not in to_verse:
|
||||
|
@ -211,7 +238,9 @@ class Bible(commands.Cog):
|
|||
description=passage["content"].replace("¶ ", ""),
|
||||
color=await self.bot.get_embed_color(ctx.channel),
|
||||
)
|
||||
embed.set_footer(text=f"{ctx.prefix}bible passage - Powered by API.Bible")
|
||||
embed.set_footer(
|
||||
text=f"{ctx.prefix}bible passage - Powered by API.Bible - {version.abbreviationLocal}"
|
||||
)
|
||||
await ctx.send(embed=embed)
|
||||
else:
|
||||
await ctx.send(f"## {passage['reference']}\n{passage['content']}")
|
||||
|
@ -222,6 +251,7 @@ class Bible(commands.Cog):
|
|||
bible_id = await self.config.bible()
|
||||
|
||||
try:
|
||||
version = await self.get_version(bible_id)
|
||||
books = await self._get_books(bible_id)
|
||||
book = random.choice(books)
|
||||
|
||||
|
@ -248,7 +278,7 @@ class Bible(commands.Cog):
|
|||
description=passage["content"].replace("¶ ", ""),
|
||||
color=await self.bot.get_embed_color(ctx.channel),
|
||||
)
|
||||
embed.set_footer(text=f"{ctx.prefix}bible random - Powered by API.Bible")
|
||||
embed.set_footer(text=f"{ctx.prefix}bible random - Powered by API.Bible - {version.abbreviationLocal}")
|
||||
await ctx.send(embed=embed)
|
||||
else:
|
||||
await ctx.send(f"## {passage['reference']}\n{passage['content']}")
|
||||
|
|
12
bible/models.py
Normal file
12
bible/models.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
class Version:
|
||||
def __init__(
|
||||
self, abbreviation, language, abbreviationLocal, languageLocal, copyright
|
||||
):
|
||||
self.abbreviation = abbreviation
|
||||
self.language = language
|
||||
self.abbreviationLocal = abbreviationLocal
|
||||
self.languageLocal = languageLocal
|
||||
self.copyright = copyright
|
||||
|
||||
def __str__(self):
|
||||
return self.abbreviationLocal
|
Loading…
Reference in a new issue