From 3813d3de02801edfdcf3ba4a62548111cc16afde Mon Sep 17 00:00:00 2001 From: SeaswimmerTheFsh Date: Fri, 2 Feb 2024 12:43:09 -0500 Subject: [PATCH] feat(bible): added bible translation to the output of bible random and bible passage --- bible/bible.py | 34 ++++++++++++++++++++++++++++++++-- bible/models.py | 12 ++++++++++++ 2 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 bible/models.py diff --git a/bible/bible.py b/bible/bible.py index dc6b640..6670ddb 100644 --- a/bible/bible.py +++ b/bible/bible.py @@ -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']}") diff --git a/bible/models.py b/bible/models.py new file mode 100644 index 0000000..ba837ec --- /dev/null +++ b/bible/models.py @@ -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