Skip to content

Commit

Permalink
add xkcd command (#65)
Browse files Browse the repository at this point in the history
Co-authored-by: nova <[email protected]>
  • Loading branch information
wizzdom and novanai authored Jan 15, 2025
1 parent bcf82c3 commit e47172c
Showing 1 changed file with 65 additions and 0 deletions.
65 changes: 65 additions & 0 deletions src/extensions/xkcd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import random

import aiohttp
import arc
import hikari

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


@xkcd.include
@arc.slash_command("xkcd", "Wisdom from xkcd!")
async def xkcd_command(
ctx: arc.GatewayContext,
num: arc.Option[
int | None, arc.IntParams("Optionally specify an xkcd number.")
] = None,
transcript: arc.Option[bool, arc.BoolParams("Show the transcript?")] = False,
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)

page_url = f"https://xkcd.com/{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: `{response.status}`",
flags=hikari.MessageFlag.EPHEMERAL,
)
return

data = await response.json()

# sometimes transcript is an empty string
transcript_text = t if (t := data["transcript"]) else "No transcript available."

embed = hikari.Embed(
title=data["title"],
description=transcript_text if transcript else None,
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 e47172c

Please sign in to comment.