From 05c00667703fe622d882519bc1c6a9218133a59b Mon Sep 17 00:00:00 2001 From: Luke Robles <98352913+lrobles-iterable@users.noreply.github.com> Date: Wed, 26 Oct 2022 17:03:48 -0700 Subject: [PATCH] Convert trackdays to a slash command and allow you to pick a track --- app/cogs/actual_utils.py | 15 +++++++++++---- app/cogs/cheeky_functions.py | 30 ++++++++++++++++++++---------- app/meme_gen.py | 25 ++++++++----------------- app/trackdays.py | 9 +++++++-- 4 files changed, 46 insertions(+), 33 deletions(-) diff --git a/app/cogs/actual_utils.py b/app/cogs/actual_utils.py index 60c76467..f6612b8e 100644 --- a/app/cogs/actual_utils.py +++ b/app/cogs/actual_utils.py @@ -95,14 +95,21 @@ class ActualUtils(commands.Cog): @commands.has_role("Track day gamers") @commands.slash_command( - guild_ids=core_utils.my_guilds, + # guild_ids=core_utils.my_guilds, + guild_ids=None, name="trackdays", description="Query motorsportsreg.com for a list of trackdays going on at Buttonwillow and Thunderhill", ) - async def trackdays(self, ctx: commands.Context): + @option( + name="track", + description="Choose a track to see the trackdays at, or leave blank for all", + choices=["Buttonwillow", "Thunderhill", "Both"], + default="Both", + ) + async def trackdays(self, ctx: commands.Context, track): import trackdays - shid = await trackdays.get_msreg() + shid = await trackdays.get_msreg(track=track) for track, events in shid.items(): embed = discord.Embed( description=":checkered_flag: **Upcoming events at %s**:checkered_flag: " @@ -122,7 +129,7 @@ class ActualUtils(commands.Cog): embed.add_field( name="Event URL", value=track_day["event_url"], inline=False ) - await ctx.send(embed=embed) + await ctx.respond(embed=embed) @commands.slash_command( guild_ids=None, diff --git a/app/cogs/cheeky_functions.py b/app/cogs/cheeky_functions.py index 99489092..96389664 100644 --- a/app/cogs/cheeky_functions.py +++ b/app/cogs/cheeky_functions.py @@ -95,20 +95,30 @@ class Cheeky(commands.Cog): await ctx.reply(excuse.get_excuse()) - @commands.command(name="meme") - async def meme(self, ctx: commands.Context): + @commands.slash_command( + guild_ids=None, + name="meme", + description="Generate a dank classic meme. White bold text over an image", + ) + @option( + name="template", + description="Which meme template you want to use", + required=True, + ) + @option(name="top", description="The Top text to put on the meme", required=True) + @option( + name="bottom", description="The bottom text to put on the meme", required=True + ) + async def meme(self, ctx: commands.Context, template, top, bottom): import meme_gen - await ctx.message.delete() - try: - await ctx.send( - embed=core_utils.generate_embed( - embed_url=meme_gen.parse_message(ctx.message.content) - ) + await ctx.defer() + await ctx.send_followup( + embed=core_utils.generate_embed( + embed_url=meme_gen.parse_message(template, top, bottom) ) - except discord.errors.HTTPException: - await ctx.send(meme_gen.get_meme_help()) + ) @commands.command(name="define", aliases=["ud"]) async def define(self, ctx: commands.Context): diff --git a/app/meme_gen.py b/app/meme_gen.py index 2e67aa8e..18540388 100755 --- a/app/meme_gen.py +++ b/app/meme_gen.py @@ -13,10 +13,7 @@ supported_templates = sorted( ) -def parse_message(message): - if len(message.split()) <= 2: - return get_meme_help() - +def parse_message(template, top, bottom): escaped_chars = { "_": "__", " ": "_", @@ -28,16 +25,10 @@ def parse_message(message): "''": '"', } - # Unwrap message from discord - template = message.split()[1] - text = message.replace("!meme {}".format(template), "").lstrip().split(",") - upper = text[0].split(";")[0].lstrip() - lower = text[0].split(";")[1].lstrip() - - # Escape special characters in upper/lower text + # Escape special characters in top/bottom text for char, escaped in escaped_chars.items(): - upper = upper.replace(char, escaped) - lower = lower.replace(char, escaped) + top = top.replace(char, escaped) + bottom = bottom.replace(char, escaped) usesTemplate = True if template.startswith("http"): @@ -45,10 +36,10 @@ def parse_message(message): elif template not in supported_templates: return "Template not supported!" - return get_meme_url(template, upper, lower, usesTemplate) + return get_meme_url(template, top, bottom, usesTemplate) -def get_meme_url(template, upper, lower, usesTemplate): +def get_meme_url(template, top, bottom, usesTemplate): # TODO: Implement format as a parameter? base_URL = "https://memegen.link" custom_URL = "{}/custom".format(base_URL) @@ -56,10 +47,10 @@ def get_meme_url(template, upper, lower, usesTemplate): # Generate meme url meme = "{}/{}/{}.{}?alt={}".format( - custom_URL, upper, lower, default_format, template + custom_URL, top, bottom, default_format, template ) if usesTemplate: - meme = "{}/{}/{}/{}.{}".format(base_URL, template, upper, lower, default_format) + meme = "{}/{}/{}/{}.{}".format(base_URL, template, top, bottom, default_format) return meme diff --git a/app/trackdays.py b/app/trackdays.py index 30d8dd2f..fe42ca24 100755 --- a/app/trackdays.py +++ b/app/trackdays.py @@ -4,7 +4,7 @@ import httpx import xmltodict -async def get_msreg(): +async def get_msreg(track): base_url = "https://api.motorsportreg.com/rest/calendars/organization" orgs = { "speeddistrict": "2E22740B-E8C9-9FB9-21406A496429A28B", @@ -19,6 +19,12 @@ async def get_msreg(): } events = {} client = httpx.AsyncClient() + + if track == "Both": + tracks_we_care_about = ["buttonwillow", "thunderhill"] + else: + tracks_we_care_about = [track.lower()] + for org_name, org_id in orgs.items(): xml_blob = await client.get( "%s/%s?exclude_cancelled=true&postalcode=94549&radius=500" @@ -31,7 +37,6 @@ async def get_msreg(): try: for event in json_blob["event"]: - tracks_we_care_about = ["buttonwillow", "thunderhill"] if any(x in event["name"].lower() for x in tracks_we_care_about): event_object = { "event_name": event["name"],