mirror of
https://github.com/wlinator/luminara.git
synced 2024-10-02 18:03:12 +00:00
Add inspirobot command
This commit is contained in:
parent
9e87b273d2
commit
4267a8f50d
2 changed files with 69 additions and 0 deletions
49
modules/misc/inspirotbot.py
Normal file
49
modules/misc/inspirotbot.py
Normal file
|
@ -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))
|
20
wrappers/inspirobot.py
Normal file
20
wrappers/inspirobot.py
Normal file
|
@ -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
|
Loading…
Reference in a new issue