From 4267a8f50d1d69ade38cfecd36b1e97f0db57824 Mon Sep 17 00:00:00 2001 From: wlinator Date: Sun, 22 Sep 2024 17:35:13 +0200 Subject: [PATCH] Add inspirobot command --- modules/misc/inspirotbot.py | 49 +++++++++++++++++++++++++++++++++++++ wrappers/inspirobot.py | 20 +++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 modules/misc/inspirotbot.py create mode 100644 wrappers/inspirobot.py diff --git a/modules/misc/inspirotbot.py b/modules/misc/inspirotbot.py new file mode 100644 index 0000000..92c6d0c --- /dev/null +++ b/modules/misc/inspirotbot.py @@ -0,0 +1,49 @@ +from discord import app_commands +from discord.ext import commands + +import lib.format +from lib.client import Luminara +from lib.exceptions import LumiException +from ui.embeds import Builder +from wrappers.inspirobot import InspiroBot + + +class Inspirobot(commands.Cog): + def __init__(self, bot: Luminara) -> None: + self.bot: Luminara = bot + self.inspirobot.usage = lib.format.generate_usage(self.inspirobot) + + @commands.hybrid_command( + name="inspirobot", + aliases=["inspiro", "ib"], + ) + @app_commands.describe(ephemeral="Whether to send the image ephemerally.") + @commands.is_nsfw() + async def inspirobot(self, ctx: commands.Context[Luminara], *, ephemeral: bool = False) -> None: + """ + Get a random AI-generated motivational image from Inspirobot. + + Parameters + ---------- + ctx : commands.Context[Luminara] + The context of the command. + ephemeral : bool, optional + Whether to send the image ephemerally. Defaults to False. + """ + async with ctx.typing(): + try: + image_url = await InspiroBot().get_image() + except Exception as e: + msg = f"Failed to get image URL from Inspirobot: {e}" + raise LumiException(msg) from e + + embed = Builder.create_embed( + Builder.SUCCESS, + author_text="InspiroBot (AI Generated)", + image_url=image_url, + ) + await ctx.send(embed=embed, ephemeral=ephemeral) + + +async def setup(bot: Luminara) -> None: + await bot.add_cog(Inspirobot(bot)) diff --git a/wrappers/inspirobot.py b/wrappers/inspirobot.py new file mode 100644 index 0000000..4142f29 --- /dev/null +++ b/wrappers/inspirobot.py @@ -0,0 +1,20 @@ +import httpx + + +class InspiroBot: + def __init__(self): + self.base_url = "https://inspirobot.me/api?generate=true" + + async def get_image(self) -> str: + """ + Get a random motivational image from Inspirobot. + + Returns + ------- + str + The URL of the image. + """ + async with httpx.AsyncClient() as client: + response = await client.get(self.base_url) + response.raise_for_status() + return response.text