Skip to content

Commit

Permalink
add xkcd command
Browse files Browse the repository at this point in the history
  • Loading branch information
wizzdom committed Jan 15, 2025
1 parent bcf82c3 commit ea6d1c6
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/extensions/xkcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import arc
import hikari
import aiohttp
import random

xkcd = arc.GatewayPlugin(name="xkcd")


async def get_max_comics():
"""
Get the latest xkcd comic number.
"""
request_url = "https://xkcd.com/info.0.json"
async with aiohttp.ClientSession().get(request_url) as query:
if query.status != 200:
return
response = await query.json()
return response["num"]


@xkcd.include
@arc.slash_command("xkcd", "So tell me Frank!")
async def xkcd_command(
ctx: arc.GatewayContext,
num: arc.Option[
int | None, arc.IntParams("Optionally specify an xkcd number.")
] = None,
aiohttp_client: aiohttp.ClientSession = arc.inject(),
) -> None:
"""Send an xkcd!"""
if num is None:
latest_url = "https://xkcd.com/info.0.json"
async with aiohttp_client.get(latest_url) as query:
if query.status != 200:
await ctx.respond(
f"❌ Failed to query xkcd API. Status code: `{query.status}`",
flags=hikari.MessageFlag.EPHEMERAL,
)
return
response = await query.json()
max_comic = response["num"]
num = random.randint(1, max_comic)

base_url = "https://xkcd.com/"
page_url = f"{base_url}{num}/"
api_url = f"{page_url}info.0.json"

async with aiohttp_client.get(api_url) as response:
if response.status != 200:
await ctx.respond(
f"❌ Failed to query xkcd API. Status code: `{query.status}`",
flags=hikari.MessageFlag.EPHEMERAL,
)
return
data = await response.json()

embed = hikari.Embed(
title=data["title"],
description=data["transcript"],
url=page_url,
)
embed = embed.set_image(data["img"])
embed = embed.set_author(name=f"xkcd #{num}")

await ctx.respond(embed)


@arc.loader
def loader(client: arc.GatewayClient) -> None:
client.add_plugin(xkcd)

0 comments on commit ea6d1c6

Please sign in to comment.