-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |