45 lines
2.3 KiB
Python
45 lines
2.3 KiB
Python
import discord
|
|
from discord.ext import commands
|
|
import aiohttp
|
|
|
|
class TetoImageCog(commands.Cog):
|
|
def __init__(self, bot):
|
|
self.bot = bot
|
|
self.teto_url = "https://slipstreamm.dev/teto"
|
|
self.footer_url = "https://slipstreamm.dev/teto-web"
|
|
|
|
@commands.hybrid_command(name='tetoimage', description='Get a random image of Kasane Teto.')
|
|
async def get_teto_image(self, ctx):
|
|
"""Gets a random image of Kasane Teto."""
|
|
async with ctx.typing():
|
|
try:
|
|
async with aiohttp.ClientSession() as session:
|
|
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'}
|
|
async with session.get(self.teto_url, headers=headers, allow_redirects=False) as response:
|
|
if response.status == 302:
|
|
image_path = response.headers.get('Location')
|
|
if image_path:
|
|
image_url = f"https://slipstreamm.dev{image_path}"
|
|
embed = discord.Embed(
|
|
title="Random Teto Image",
|
|
description=f"Website: {self.footer_url}",
|
|
color=discord.Color.red()
|
|
)
|
|
if image_url.startswith("http") or image_url.startswith("https"):
|
|
embed.set_image(url=image_url)
|
|
embed.set_footer(text="Random Teto Image Website", icon_url=None)
|
|
await ctx.send(embed=embed)
|
|
else:
|
|
await ctx.send("The retrieved URL was not valid.")
|
|
else:
|
|
await ctx.send("Could not get the image URL from the redirect.")
|
|
else:
|
|
await ctx.send(f"Unexpected response status: {response.status}")
|
|
except aiohttp.ClientConnectorError as e:
|
|
await ctx.send(f"Could not connect to the image service: {e}")
|
|
except Exception as e:
|
|
await ctx.send(f"An error occurred: {e}")
|
|
|
|
async def setup(bot):
|
|
await bot.add_cog(TetoImageCog(bot))
|