From bf272f17c4c95043a47e7b17f1f366726af41b82 Mon Sep 17 00:00:00 2001 From: Stijn De Clercq Date: Sun, 16 Jan 2022 19:55:07 +0100 Subject: [PATCH] Add xkcd command --- cogs/fun.py | 9 +++++++++ cogs/slash/db_slash.py | 4 ++-- cogs/slash/fun_slash.py | 29 +++++++++++++++++++++++++++++ data/embeds/xkcd.py | 28 ++++++++++++++++++++++++++++ files/help.json | 1 + 5 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 cogs/slash/fun_slash.py create mode 100644 data/embeds/xkcd.py diff --git a/cogs/fun.py b/cogs/fun.py index 11b0a21..f797803 100644 --- a/cogs/fun.py +++ b/cogs/fun.py @@ -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)) diff --git a/cogs/slash/db_slash.py b/cogs/slash/db_slash.py index 24bad49..1109b35 100644 --- a/cogs/slash/db_slash.py +++ b/cogs/slash/db_slash.py @@ -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)) diff --git a/cogs/slash/fun_slash.py b/cogs/slash/fun_slash.py new file mode 100644 index 0000000..bd4191a --- /dev/null +++ b/cogs/slash/fun_slash.py @@ -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)) diff --git a/data/embeds/xkcd.py b/data/embeds/xkcd.py new file mode 100644 index 0000000..6aaf672 --- /dev/null +++ b/data/embeds/xkcd.py @@ -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 diff --git a/files/help.json b/files/help.json index 545a3c4..a522c9b 100644 --- a/files/help.json +++ b/files/help.json @@ -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." } \ No newline at end of file