Add xkcd command

pull/103/head
Stijn De Clercq 2022-01-16 19:55:07 +01:00
parent 3c5221f32e
commit bf272f17c4
5 changed files with 69 additions and 2 deletions

View File

@ -1,3 +1,4 @@
from data.embeds.xkcd import XKCDEmbed
from data.menus import paginatedLeaderboard
from decorators import help
import discord
@ -137,6 +138,14 @@ class Fun(commands.Cog):
r = requests.get("https://official-joke-api.appspot.com/jokes/programming/random").json()
await ctx.send(r[0]["setup"] + "\n" + r[0]["punchline"])
@commands.command(name="xkcd")
@help.Category(category=Category.Fun)
async def xkcd(self, ctx, n: int = None):
"""
Send an xkcd comic
"""
return await ctx.reply(embed=XKCDEmbed(n).create())
def setup(client):
client.add_cog(Fun(client))

View File

@ -8,7 +8,7 @@ from functions.timeFormatters import fromString
from startup.didier import Didier
class Slash(commands.Cog):
class DBSlash(commands.Cog):
def __init__(self, client: Didier):
self.client: Didier = client
@ -84,4 +84,4 @@ class Slash(commands.Cog):
def setup(client: Didier):
client.add_cog(Slash(client))
client.add_cog(DBSlash(client))

View File

@ -0,0 +1,29 @@
from discord.ext import commands
from dislash import SlashInteraction, slash_command, Option, OptionType
from data.embeds.xkcd import XKCDEmbed
from startup.didier import Didier
class FunSlash(commands.Cog):
def __init__(self, client: Didier):
self.client: Didier = client
@slash_command(
name="xkcd",
description="Zoek xkcd comics",
options=[
Option(
"num",
description="Nummer van de comic (default de comic van vandaag).",
type=OptionType.INTEGER,
required=False
)
]
)
async def _xkcd_slash(self, interaction: SlashInteraction, num: int = None):
return await interaction.reply(embed=XKCDEmbed(num).create())
def setup(client: Didier):
client.add_cog(FunSlash(client))

View File

@ -0,0 +1,28 @@
import discord
from requests import get
from functions.stringFormatters import leading_zero
class XKCDEmbed:
n: int
def __init__(self, n: int = None):
self.n = n
def create(self) -> discord.Embed:
endpoint = "https://xkcd.com/info.0.json" if self.n is None else f"https://xkcd.com/{self.n}/info.0.json"
response = get(endpoint)
if response.status_code != 200:
embed = discord.Embed(colour=discord.Colour.red())
embed.set_author(name="xkcd")
embed.description = f"Er ging iets mis (status {response.status_code})."
return embed
data = response.json()
embed = discord.Embed(colour=discord.Colour.from_rgb(150, 168, 200), title=data["safe_title"])
embed.set_author(name=f"xkcd #{data['num']}")
embed.set_image(url=data["img"])
embed.set_footer(text=f"{leading_zero(data['day'])}/{leading_zero(data['month'])}/{data['year']}")
return embed

View File

@ -122,6 +122,7 @@
"unload": "Verwijdert [Cog].",
"unload all": "Verwijdert alle cogs.",
"whois": "Toont de info van [@User]",
"xkcd": "Zoek [xkcd](https://xkcd.com/) comics.",
"xp": "Bekijk je huidige aantal berichten en xp.",
"yes/no": "Didier helpt je met keuzes maken."
}