Add XKCD command back in

pull/142/head
stijndcl 2022-10-18 22:23:55 +02:00
parent 1c249adb46
commit 65785fef8f
4 changed files with 62 additions and 0 deletions

View File

@ -9,6 +9,7 @@ from database.crud.dad_jokes import get_random_dad_joke
from database.crud.memes import get_all_memes, get_meme_by_name from database.crud.memes import get_all_memes, get_meme_by_name
from didier import Didier from didier import Didier
from didier.data.apis.imgflip import generate_meme from didier.data.apis.imgflip import generate_meme
from didier.data.apis.xkcd import fetch_xkcd_post
from didier.exceptions.no_match import expect from didier.exceptions.no_match import expect
from didier.menus.memes import MemeSource from didier.menus.memes import MemeSource
from didier.utils.discord import constants from didier.utils.discord import constants
@ -132,6 +133,18 @@ class Fun(commands.Cog):
"""Autocompletion for the 'template'-parameter""" """Autocompletion for the 'template'-parameter"""
return self.client.database_caches.memes.get_autocomplete_suggestions(current) return self.client.database_caches.memes.get_autocomplete_suggestions(current)
@commands.hybrid_command(name="xkcd")
@app_commands.rename(comic_id="id")
async def xkcd(self, ctx: commands.Context, comic_id: Optional[int] = None):
"""Fetch comic `#id` from xkcd
If no argument to `id` is passed, this fetches today's comic instead.
"""
async with ctx.typing():
post = await fetch_xkcd_post(self.client.http_session, num=comic_id)
await ctx.reply(embed=post.to_embed(), mention_author=False, ephemeral=False)
async def setup(client: Didier): async def setup(client: Didier):
"""Load the cog""" """Load the cog"""

View File

@ -0,0 +1,16 @@
from typing import Optional
from aiohttp import ClientSession
from didier.data.embeds.xkcd import XKCDPost
from didier.utils.http.requests import ensure_get
__all__ = ["fetch_xkcd_post"]
async def fetch_xkcd_post(http_session: ClientSession, *, num: Optional[int] = None) -> XKCDPost:
"""Fetch a post from xkcd.com"""
url = "https://xkcd.com" + (f"/{num}" if num is not None else "") + "/info.0.json"
async with ensure_get(http_session, url) as response:
return XKCDPost.parse_obj(response)

View File

@ -0,0 +1,28 @@
import discord
from overrides import overrides
from didier.data.embeds.base import EmbedPydantic
from didier.utils.discord.colours import xkcd_blue
__all__ = ["XKCDPost"]
class XKCDPost(EmbedPydantic):
"""A post from xkcd.com"""
num: int
img: str
safe_title: str
day: int
month: int
year: int
@overrides
def to_embed(self, **kwargs) -> discord.Embed:
embed = discord.Embed(colour=xkcd_blue(), title=self.safe_title)
embed.set_author(name=f"XKCD #{self.num}")
embed.set_image(url=self.img)
embed.set_footer(text=f"Published {self.day:02d}/{self.month:02d}/{self.year}")
return embed

View File

@ -9,6 +9,7 @@ __all__ = [
"google_blue", "google_blue",
"steam_blue", "steam_blue",
"urban_dictionary_green", "urban_dictionary_green",
"xkcd_blue",
] ]
@ -46,3 +47,7 @@ def steam_blue() -> discord.Colour:
def urban_dictionary_green() -> discord.Colour: def urban_dictionary_green() -> discord.Colour:
return discord.Colour.from_rgb(220, 255, 0) return discord.Colour.from_rgb(220, 255, 0)
def xkcd_blue() -> discord.Colour:
return discord.Colour.from_rgb(150, 168, 200)