Skip to content

Commit

Permalink
Defer voteythumbs request (also prettified latex, and deferred xkcd) (#…
Browse files Browse the repository at this point in the history
…126)

* voteythumbs defers reactions

* defer latex.py, cleaned up UI using embed

* defer xkcd.py

* black formatting

* Fixed formatting to satisfy black

---------

Co-authored-by: Isaac Beh <[email protected]>
  • Loading branch information
emadkhan713 and 49Indium authored Jun 25, 2023
1 parent 052b349 commit 370a113
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 15 deletions.
18 changes: 12 additions & 6 deletions uqcsbot/latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,20 @@ def __init__(self, bot: commands.Bot):
@app_commands.command(description="Renders the given LaTeX")
@app_commands.describe(input="LaTeX to render")
async def latex(self, interaction: discord.Interaction, input: str):
if len(input) == 0:
return
# since bot prohibits empty prompts, checking len==0 seems redundant
await interaction.response.defer(thinking=True)

url = f"https://latex.codecogs.com/png.image?%5Cdpi%7B200%7D%5Cbg%7B36393f%7D%5Cfg%7Bwhite%7D{quote(input)}"

await interaction.response.send_message(
f'LaTeX render for "{input}"\n{url}',
url = (
"https://latex.codecogs.com/png.image?"
"%5Cdpi%7B200%7D%5Cbg%7B36393f%7D%5Cfg%7Bwhite%7D"
f"{quote(input)}"
)
embed = discord.Embed(
colour=discord.Colour.blue(),
title=f'Latex render for "{input}"',
).set_image(url=f"{url}")

await interaction.edit_original_response(embed=embed)


async def setup(bot: commands.Bot):
Expand Down
8 changes: 6 additions & 2 deletions uqcsbot/voteythumbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,24 @@ async def voteythumbs_context(
self, interaction: discord.Interaction, message: discord.Message
):
"""Starts a 👍 👎 vote."""
await interaction.response.defer(ephemeral=True)

await self.common_react(message)
await interaction.response.send_message("Vote away!", ephemeral=True)
await interaction.edit_original_response(content="Vote away!")

@app_commands.command(name="voteythumbs")
@app_commands.describe(question="The question that shall be voted upon")
async def voteythumbs_command(
self, interaction: discord.Interaction, question: str
):
"""Starts a 👍 👎 vote."""
await interaction.response.send_message(question)
await interaction.response.defer()
message = await interaction.original_response()

await self.common_react(message)

await interaction.edit_original_response(content=question)


async def setup(bot: commands.Bot):
await bot.add_cog(VoteyThumbs(bot))
24 changes: 17 additions & 7 deletions uqcsbot/xkcd.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,12 @@ async def xkcd_command(
"""
Returns a random xkcd comic or the comic with the given number.
"""
await interaction.response.defer(thinking=True)

# If number is given, check if its a valid number
if number is not None and number <= 0:
await interaction.response.send_message(
"Invalid xkcd number (must be positive)"
await interaction.edit_original_response(
content="Invalid xkcd number (must be positive)"
)
return

Expand All @@ -50,10 +51,14 @@ async def xkcd_command(

# Check if the xkcd data failed to fetch
if xkcd_num == XKCD_FETCH_ERROR[0]:
await interaction.response.send_message("Failed to fetch xkcd page")
await interaction.edit_original_response(
content="Failed to fetch xkcd page"
)
return
elif xkcd_num == XKCD_PARSE_ERROR[0]:
await interaction.response.send_message("Failed to parse xkcd page data")
await interaction.edit_original_response(
content="Failed to parse xkcd page data"
)
return

# Create a custom embed for the xkcd comic
Expand All @@ -65,15 +70,16 @@ async def xkcd_command(
message.set_footer(text="xkcd.com")

# Send it!
await interaction.response.send_message(embed=message)
await interaction.edit_original_response(embed=message)

@staticmethod
def get_xkcd_data(url: str) -> (int, str, str, str):
"""
Returns the xkcd data from the given url.
:param url: The url to fetch the xkcd data from
:return: A tuple containing the xkcd number, title, description and image url
:return: A tuple containing the xkcd number, title,
description and image url
"""

# Get the xkcd page
Expand Down Expand Up @@ -101,7 +107,11 @@ def parse_xkcd_page(content: str) -> (int, str, str, str):
re.MULTILINE,
)
img_match = re.search(
r'(?<=Image URL \(for hotlinking\/embedding\): <a href= ")(.*?)(?=">)', data
(
r'(?<=Image URL \(for hotlinking\/embedding\): <a href= ")'
r'(.*?)(?=">)'
),
data,
)

# If any of the regexes failed, return an error
Expand Down

0 comments on commit 370a113

Please sign in to comment.