make excuse use beautiful soup
All checks were successful
Build and push / changes (push) Successful in 3s
Build and push / Lint-Python (push) Successful in 3s
Build and push / Build-and-Push-Docker (push) Successful in 20s
Build and push / sync-argocd-app (push) Successful in 2s
Build and push / post-status-to-discord (push) Successful in 2s

This commit is contained in:
Luke R 2025-02-26 15:11:13 -08:00
parent b2aa137f44
commit 928b1cad07
2 changed files with 23 additions and 8 deletions

View File

@ -161,11 +161,11 @@ class Cheeky(commands.Cog):
await ctx.respond(embed=dice.roll(number_of_die, sides, number_to_add))
@commands.command(name="excuse")
@commands.slash_command(name="excuse")
async def excuse(self, ctx: commands.Context):
import excuse
await ctx.reply(excuse.get_excuse())
await ctx.respond(excuse.get_excuse())
async def get_all_meme_templates(ctx: discord.AutocompleteContext):
"""

View File

@ -3,11 +3,26 @@ import requests
def get_excuse():
url = requests.get("http://www.devexcuses.com")
soup = BeautifulSoup(url.content, features="html.parser")
"""
Fetches a random developer excuse from devexcuses.com
return "```{}```".format(
str(soup.find("p", {"class": "excuse"}).contents[0])
.split(">")[1]
.split("</a")[0]
Returns:
str: A formatted excuse string wrapped in code block
"""
url = "http://www.devexcuses.com"
response = requests.get(url, headers={"Cache-Control": "no-cache"})
response.raise_for_status() # Raise exception for bad responses
soup = BeautifulSoup(response.content, features="html.parser")
excuse_element = soup.find("p", {"class": "excuse"})
if not excuse_element or not excuse_element.contents:
return "```Failed to get an excuse. Maybe that's your excuse?```"
# Extract the excuse text more reliably using the <a> tag inside the <p>
excuse_link = excuse_element.find("a")
excuse_text = (
excuse_link.text.strip() if excuse_link else excuse_element.text.strip()
)
return f"```{excuse_text}```"