Skip to content

Commit

Permalink
Fumo fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuro-Rui committed Jul 12, 2024
1 parent fc1b934 commit 60cca6b
Showing 1 changed file with 16 additions and 25 deletions.
41 changes: 16 additions & 25 deletions cogs/fumo.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import json
import random
from datetime import datetime
from typing import Literal
from typing import Literal, Optional

import aiohttp
import discord
Expand All @@ -23,14 +23,12 @@ def display_emoji(self) -> discord.PartialEmoji:
return discord.PartialEmoji(name="Cirno", id=935836292653146123)

async def fetch_fumos(self) -> None:
async with self.bot.session.get(
"https://raw.githubusercontent.com/Kuro-Rui/Kuro-Cogs/main/fumo/data/fumos.json"
) as response:
async with self.bot.session.get("https://kuro-rui.github.io/api/fumo/all.json") as resp:
try:
response.raise_for_status()
resp.raise_for_status()
except aiohttp.ClientResponseError as exc_info:
self._log.exception("Failed to fetch Fumos", exc_info=exc_info)
self.fumos = json.loads(await response.text())
self.fumos = json.loads(await resp.text())
self._log.info("Successfully fetched %d Fumos.", sum(len(l) for l in self.fumos.values()))

async def cog_load(self) -> None:
Expand All @@ -47,62 +45,55 @@ async def random(self, ctx: commands.Context):
async def image(self, ctx: commands.Context):
"""Get a random Fumo image"""

await self.summon_fumo(ctx, "Image")
await self.summon_fumo(ctx, "image")

@commands.command()
async def gif(self, ctx: commands.Context):
"""Get a random Fumo GIF"""

await self.summon_fumo(ctx, "GIF")
await self.summon_fumo(ctx, "gif")

@commands.command()
async def video(self, ctx: commands.Context):
"""Get a random Fumo video"""

await self.summon_fumo(ctx, "Video")
await self.summon_fumo(ctx, "video")

@commands.check(lambda ctx: datetime.today().weekday() == 4)
@commands.command()
async def friday(self, ctx: commands.Context):
"""Get a random Fumo Friday video"""

await self.summon_fumo(ctx, "FUMO FRIDAY")
await self.summon_fumo(ctx, "friday")

async def get_fumos(
self, content_type: Literal["Image", "GIF", "Video", "FUMO FRIDAY"] = None
self, content_type: Optional[Literal["friday", "gif", "image", "video"]] = None
) -> list[str]:
if not self.fumos:
await self.fetch_fumos()
if not content_type:
fumos = self.fumos["Image"] + self.fumos["GIF"] + self.fumos["Video"]
fumos = self.fumos["image"] + self.fumos["gif"] + self.fumos["video"]
if self.is_friday():
fumos.extend(self.fumos["FUMO FRIDAY"])
fumos.extend(self.fumos["friday"])
return fumos
fumos = self.fumos[content_type]
if content_type == "Video" and self.is_friday():
fumos.extend(self.fumos["FUMO FRIDAY"])
if content_type == "video" and self.is_friday():
fumos.extend(self.fumos["friday"])
return fumos

async def summon_fumo(
self,
ctx: commands.Context,
content_type: Literal["Image", "GIF", "Video", "FUMO FRIDAY"] = None,
content_type: Optional[Literal["friday", "gif", "image", "video"]] = None,
) -> None:
all_fumos = await self.get_fumos(content_type)
url = random.choice(all_fumos)
title = f"Here's a Random Fumo! ᗜˬᗜ"
if content_type:
title = f"Here's a Random Fumo {content_type}! ᗜˬᗜ"
if content_type == "FUMO FRIDAY":
if content_type == "friday":
title = "Happy Fumo Friday! ᗜˬᗜ"
types = {
"jpg": "Image",
"png": "Image",
"gif": "GIF",
"mp4": "Video",
"mov": "Video",
}
if types[url[-3:]] != "Video":
if url[-3:] not in ("mp4", "mov"):
embed = discord.Embed(color=ctx.embed_color, title=title)
embed.set_image(url=url)
await ctx.send(embed=embed)
Expand Down

0 comments on commit 60cca6b

Please sign in to comment.