From 6349321e5cc54c2235ddac420f60bcdce5fe40de Mon Sep 17 00:00:00 2001 From: Zevaryx Date: Mon, 21 Feb 2022 01:26:41 -0700 Subject: [PATCH] Update remindme to use modals --- jarvis/cogs/remindme.py | 63 +++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 28 deletions(-) diff --git a/jarvis/cogs/remindme.py b/jarvis/cogs/remindme.py index e7a90b9..ab4aae6 100644 --- a/jarvis/cogs/remindme.py +++ b/jarvis/cogs/remindme.py @@ -5,11 +5,12 @@ from datetime import datetime, timedelta from typing import List from bson import ObjectId -from dis_snek import InteractionContext, Snake +from dis_snek import InteractionContext, Scale, Snake from dis_snek.client.utils.misc_utils import get from dis_snek.models.discord.channel import GuildChannel from dis_snek.models.discord.components import ActionRow, Select, SelectOption from dis_snek.models.discord.embed import Embed, EmbedField +from dis_snek.models.discord.modal import InputText, Modal, TextStyles from dis_snek.models.snek.application_commands import ( OptionTypes, SlashCommandChoice, @@ -20,35 +21,19 @@ from jarvis_core.db import q from jarvis_core.db.models import Reminder from jarvis.utils import build_embed -from jarvis.utils.cachecog import CacheCog -valid = re.compile(r"[\w\s\-\\/.!@#$%^*()+=<>,\u0080-\U000E0FFF]*") -time_pattern = re.compile(r"(\d+\.?\d?[s|m|h|d|w]{1})\s?") +valid = re.compile(r"[\w\s\-\\/.!@#$%^*()+=<>:,\u0080-\U000E0FFF]*") +time_pattern = re.compile(r"(\d+\.?\d?[s|m|h|d|w]{1})\s?", flags=re.IGNORECASE) invites = re.compile( r"(?:https?://)?(?:www.)?(?:discord.(?:gg|io|me|li)|discord(?:app)?.com/invite)/([^\s/]+?)(?=\b)", # noqa: E501 flags=re.IGNORECASE, ) -class RemindmeCog(CacheCog): +class RemindmeCog(Scale): """J.A.R.V.I.S. Remind Me Cog.""" - def __init__(self, bot: Snake): - super().__init__(bot) - @slash_command(name="remindme", description="Set a reminder") - @slash_option( - name="message", - description="What to remind you of?", - opt_type=OptionTypes.STRING, - required=True, - ) - @slash_option( - name="delay", - description="How long? (i.e. 1w 3d 7h 5m 20s)", - opt_type=OptionTypes.STRING, - required=False, - ) @slash_option( name="private", description="Send as DM?", @@ -62,13 +47,35 @@ class RemindmeCog(CacheCog): async def _remindme( self, ctx: InteractionContext, - message: str, - delay: str, private: str = "n", ) -> None: private = private == "y" - if len(message) > 100: - await ctx.send("Reminder cannot be > 100 characters.", ephemeral=True) + modal = Modal( + title="Set your reminder!", + components=[ + InputText( + label="What to remind you?", + placeholder="Reminder", + style=TextStyles.PARAGRAPH, + custom_id="message", + ), + InputText( + label="When to remind you?", + placeholder="1h 30m", + style=TextStyles.SHORT, + custom_id="delay", + ), + ], + ) + await ctx.send_modal(modal) + try: + response = await self.bot.wait_for_modal(modal, author=ctx.author.id, timeout=60 * 5) + message = response.responses.get("message") + delay = response.responses.get("delay") + except asyncio.TimeoutError: + return + if len(message) > 500: + await ctx.send("Reminder cannot be > 500 characters.", ephemeral=True) return elif invites.search(message): await ctx.send( @@ -83,7 +90,7 @@ class RemindmeCog(CacheCog): units = {"w": "weeks", "d": "days", "h": "hours", "m": "minutes", "s": "seconds"} delta = {"weeks": 0, "days": 0, "hours": 0, "minutes": 0, "seconds": 0} - if times := time_pattern.findall(delay, flags=re.I): + if times := time_pattern.findall(delay): for t in times: delta[units[t[-1]]] += float(t[:-1]) else: @@ -96,7 +103,7 @@ class RemindmeCog(CacheCog): await ctx.send("At least one time period is required", ephemeral=True) return - reminders = len(await Reminder.find(q(user=ctx.author.id, active=True))) + reminders = len([x async for x in Reminder.find(q(user=ctx.author.id, active=True))]) if reminders >= 5: await ctx.send( "You already have 5 (or more) active reminders. " @@ -108,7 +115,7 @@ class RemindmeCog(CacheCog): remind_at = datetime.now() + timedelta(**delta) r = Reminder( - user=ctx.author_id, + user=ctx.author.id, channel=ctx.channel.id, guild=ctx.guild.id, message=message, @@ -138,7 +145,7 @@ class RemindmeCog(CacheCog): ) embed.set_thumbnail(url=ctx.author.display_avatar.url) - await ctx.send(embed=embed, ephemeral=private) + await response.send(embed=embed, ephemeral=private) async def get_reminders_embed( self, ctx: InteractionContext, reminders: List[Reminder]