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
|
from redbot.core.utils.chat_formatting import error
|
||||||
|
|
||||||
import bible.errors
|
import bible.errors
|
||||||
|
from bible.models import Version
|
||||||
|
|
||||||
|
|
||||||
class Bible(commands.Cog):
|
class Bible(commands.Cog):
|
||||||
|
@ -46,6 +47,31 @@ class Bible(commands.Cog):
|
||||||
return book["id"]
|
return book["id"]
|
||||||
raise ValueError(error(f"Book {book_name} not found."))
|
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(
|
async def _get_passage(
|
||||||
self,
|
self,
|
||||||
ctx: commands.Context,
|
ctx: commands.Context,
|
||||||
|
@ -180,6 +206,7 @@ class Bible(commands.Cog):
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
version = await self.get_version(bible_id)
|
||||||
if len(passage.split("-")) == 2:
|
if len(passage.split("-")) == 2:
|
||||||
from_verse, to_verse = passage.replace(":", ".").split("-")
|
from_verse, to_verse = passage.replace(":", ".").split("-")
|
||||||
if "." not in to_verse:
|
if "." not in to_verse:
|
||||||
|
@ -211,7 +238,9 @@ class Bible(commands.Cog):
|
||||||
description=passage["content"].replace("¶ ", ""),
|
description=passage["content"].replace("¶ ", ""),
|
||||||
color=await self.bot.get_embed_color(ctx.channel),
|
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)
|
await ctx.send(embed=embed)
|
||||||
else:
|
else:
|
||||||
await ctx.send(f"## {passage['reference']}\n{passage['content']}")
|
await ctx.send(f"## {passage['reference']}\n{passage['content']}")
|
||||||
|
@ -222,6 +251,7 @@ class Bible(commands.Cog):
|
||||||
bible_id = await self.config.bible()
|
bible_id = await self.config.bible()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
version = await self.get_version(bible_id)
|
||||||
books = await self._get_books(bible_id)
|
books = await self._get_books(bible_id)
|
||||||
book = random.choice(books)
|
book = random.choice(books)
|
||||||
|
|
||||||
|
@ -248,7 +278,7 @@ class Bible(commands.Cog):
|
||||||
description=passage["content"].replace("¶ ", ""),
|
description=passage["content"].replace("¶ ", ""),
|
||||||
color=await self.bot.get_embed_color(ctx.channel),
|
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)
|
await ctx.send(embed=embed)
|
||||||
else:
|
else:
|
||||||
await ctx.send(f"## {passage['reference']}\n{passage['content']}")
|
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