Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 68 additions & 57 deletions techsupport_bot/commands/xkcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import discord
import munch
from core import auxiliary, cogs
from discord.ext import commands
from discord import app_commands

if TYPE_CHECKING:
import bot
Expand All @@ -29,115 +29,126 @@ class XKCD(cogs.BaseCog):
Attributes:
MOST_RECENT_API_URL (str): The URL for the most recent comic
SPECIFIC_API_URL (str): The URL for a given number comic
xkcd (app_commands.Group): The group for the /xkcd commands

"""

MOST_RECENT_API_URL: str = "https://xkcd.com/info.0.json"
SPECIFIC_API_URL: str = "https://xkcd.com/%s/info.0.json"

@commands.group(
brief="xkcd extension parent",
description="Group for xkcd subcommands and retrieves numbered comics.",
invoke_without_subcommand=True,
xkcd: app_commands.Group = app_commands.Group(
name="xkcd", description="Command Group for the XKCD Extension"
)
async def xkcd(
self: Self, ctx: commands.Context, number: int | None = None
) -> None:
"""Entry point for the xkcd command. This either shows extension help
Or will display a comic if a number is entered

@xkcd.command(
name="latest",
description="Gets the latest XKCD comic.",
extras={
"module": "xkcd",
},
)
async def xkcd_latest(self: Self, interaction: discord.Interaction) -> None:
"""This calls the XKCD API and gets the latest comic

Args:
ctx (commands.Context): The context genereated by this command
number (int | None, optional): The comic number to lookup and display.
Defaults to None.
interaction (discord.Interaction): The interaction that called this command
"""
if number:
await self.numbered_comic(ctx, number)
await interaction.response.defer()
latest_comic_data = await self.api_call()
if latest_comic_data.status_code != 200:
embed = auxiliary.prepare_deny_embed(
message="I had trouble looking up XKCD's comics"
)
await interaction.followup.send(embed=embed)
return

embed = self.generate_embed(latest_comic_data)
if not embed:
embed = auxiliary.prepare_deny_embed(
message="I had trouble calling getting the correct XKCD info",
)

await interaction.followup.send(embed=embed)

@xkcd.command(
name="random",
brief="Gets a random XKCD comic",
description="Gets a random XKCD comic",
description="Gets a random XKCD comic.",
extras={
"module": "xkcd",
},
)
async def random_comic(self: Self, ctx: commands.Context) -> None:
"""Gets a completely random comic after looking up the latest comic and displays it
async def xkcd_random(self: Self, interaction: discord.Interaction) -> None:
"""This calls the XKCD API and gets a random comic

Args:
ctx (commands.Context): The context generated by running this command
interaction (discord.Interaction): The interaction that called this command
"""
most_recent_comic_data = await self.api_call()
if most_recent_comic_data.status_code != 200:
await auxiliary.send_deny_embed(
message="I had trouble looking up XKCD's comics", channel=ctx.channel
await interaction.response.defer()
latest_comic_data = await self.api_call()
if latest_comic_data.status_code != 200:
embed = auxiliary.prepare_deny_embed(
message="I had trouble looking up XKCD's comics"
)
await interaction.followup.send(embed=embed)
return

max_number = most_recent_comic_data.get("num")
max_number = latest_comic_data.get("num")
if not max_number:
await auxiliary.send_deny_embed(
message="I could not determine the max XKCD number", channel=ctx.channel
embed = auxiliary.prepare_deny_embed(
message="I could not determine the max XKCD number"
)
await interaction.followup.send(embed=embed)
return

comic_number = random.randint(1, max_number)

random_comic_data = await self.api_call(number=comic_number)
if random_comic_data.status_code != 200:
await auxiliary.send_deny_embed(
embed = auxiliary.prepare_deny_embed(
message=f"I had trouble calling a random comic (#{comic_number})",
channel=ctx.channel,
)
await interaction.followup.send(embed=embed)
return

embed = self.generate_embed(random_comic_data)
if not embed:
await auxiliary.send_deny_embed(
embed = auxiliary.prepare_deny_embed(
message="I had trouble calling getting the correct XKCD info",
channel=ctx.channel,
)
return

await ctx.send(embed=embed)
await interaction.followup.send(embed=embed)

@xkcd.command(
name="[number]",
brief="Gets a XKCD comic",
description="Gets a XKCD comic by number",
name="specific",
description="Gets an XKCD comic by number.",
extras={"module": "xkcd", "usage": "[comic_number]"},
)
async def shadow_number(self: Self, ctx: commands.Context, *, _: int) -> None:
"""Method to generate the help entry for the group parent and
prevent direct invocation.
This exists due to an oversight in the help menu generation.
This command should never be called
async def xkcd_specific(
self: Self, interaction: discord.Interaction, comic_number: int
) -> None:
"""This calls the XKCD API and gets a specific comic

Args:
ctx (commands.Context): The context generate by this command
interaction (discord.Interaction): The interaction that called this command
comic_number (int): The number of the comic to fetch from XKCD
"""
return
await interaction.response.defer()

async def numbered_comic(self: Self, ctx: commands.Context, number: int) -> None:
"""Gets the comic from XKCD by number

Args:
ctx (commands.Context): The context that called an XKCD command
number (int): The comic number to get
"""
comic_data = await self.api_call(number=number)
comic_data = await self.api_call(number=comic_number)
if comic_data.status_code != 200:
await auxiliary.send_deny_embed(
message="I had trouble looking up XKCD's comics", channel=ctx.channel
embed = auxiliary.prepare_deny_embed(
message=f"I had trouble calling the comic (#{comic_number})",
)
await interaction.followup.send(embed=embed)
return

embed = self.generate_embed(comic_data)
if not embed:
await auxiliary.send_deny_embed(
embed = auxiliary.prepare_deny_embed(
message="I had trouble calling getting the correct XKCD info",
channel=ctx.channel,
)
return

await ctx.send(embed=embed)
await interaction.followup.send(embed=embed)

async def api_call(self: Self, number: int = None) -> munch.Munch:
"""Makes an API call to xkcd to get the json for a given comic
Expand Down
Loading