discordbot/cogs/teto_image_cog.py

41 lines
2.0 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_url = response.headers.get('Location')
if image_url:
embed = discord.Embed(
title="Random Teto Image",
description=f"Website: {self.footer_url}",
color=discord.Color.red()
)
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("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))