sparkytron3000/extensions/anime.py

74 lines
2.6 KiB
Python
Raw Normal View History

2024-01-22 15:04:48 -08:00
import random
from discord.ext import commands
import discord
class Anime(commands.Cog):
2024-01-22 15:04:48 -08:00
def __init__(self, bot):
self.bot = bot
self.url = "https://api.waifu.im/search"
async def get_waifu(self, tags):
params = {
'included_tags': tags,
'height': '>=1000'
}
async with self.bot.http_session.get(self.url, params=params) as resp:
resp_data = await resp.json()
try:
2024-01-22 15:13:28 -08:00
image = random.choice(resp_data['images'])
return image['url']
2024-01-22 15:04:48 -08:00
except:
if resp_data['detail'] == "No image found matching the criteria given.":
2024-01-25 02:38:30 -08:00
self.bot.logger.info("No image found matching the criteria given.")
return "No image found matching the criteria given."
2024-01-22 15:04:48 -08:00
else:
2024-01-25 02:38:30 -08:00
self.bot.logger.exception("Something went wrong")
return "Something went wrong"
2024-01-22 15:04:48 -08:00
2024-01-22 15:37:26 -08:00
async def get_anime_from_img(self, img_url):
async with self.bot.http_session.get(f"https://api.trace.moe/search?anilistInfo&url={img_url}") as resp:
resp_data = await resp.json()
title = resp_data["result"][0]["anilist"]["title"]
2024-01-22 16:09:11 -08:00
video = resp_data["result"][0]["video"]
return title, video
2024-01-22 15:37:26 -08:00
@commands.command(aliases=["what_anime"])
async def whatanime(self, ctx):
if ctx.message.attachments:
file_url = ctx.message.attachments[0].url
else:
try:
file_url = ctx.message.content.split(" ", maxsplit=2)[1]
except:
await ctx.send("No image linked or attached.")
return
2024-01-22 16:18:12 -08:00
try:
titles, video = await self.get_anime_from_img(file_url)
except:
ctx.send("An error occurred. Try again by saving and uploading the image.")
2024-01-22 15:37:26 -08:00
message = ""
for key in titles:
message += f"{key}: {titles[key]}\n"
2024-01-22 16:09:11 -08:00
message += f"Scene: {video}"
2024-01-22 15:37:26 -08:00
await ctx.send(message)
2024-01-22 15:04:48 -08:00
@commands.command()
async def waifu(self, ctx, nsfw=""):
if nsfw.lower() == "nsfw":
tag = random.choice(["ero", "ass", "hentai", "milf", "oral", "paizuri", "ecchi"])
else:
tag = random.choice(["waifu", "maid", "uniform"])
image_url = await self.get_waifu(tag)
2024-01-22 15:04:48 -08:00
if ctx.channel.type == discord.ChannelType["private"]:
await ctx.send(image_url)
elif not ctx.channel.is_nsfw() and nsfw.lower() == "nsfw":
2024-01-22 15:04:48 -08:00
await ctx.send("Cannot post NSFW images in this channel.")
else:
await ctx.send(image_url)
2024-01-22 15:04:48 -08:00
async def setup(bot):
await bot.add_cog(Anime(bot))