Update remindme to use modals

This commit is contained in:
Zeva Rose 2022-02-21 01:26:41 -07:00
parent b12b109ad5
commit 6349321e5c

View file

@ -5,11 +5,12 @@ from datetime import datetime, timedelta
from typing import List from typing import List
from bson import ObjectId 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.client.utils.misc_utils import get
from dis_snek.models.discord.channel import GuildChannel from dis_snek.models.discord.channel import GuildChannel
from dis_snek.models.discord.components import ActionRow, Select, SelectOption from dis_snek.models.discord.components import ActionRow, Select, SelectOption
from dis_snek.models.discord.embed import Embed, EmbedField 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 ( from dis_snek.models.snek.application_commands import (
OptionTypes, OptionTypes,
SlashCommandChoice, SlashCommandChoice,
@ -20,35 +21,19 @@ from jarvis_core.db import q
from jarvis_core.db.models import Reminder from jarvis_core.db.models import Reminder
from jarvis.utils import build_embed from jarvis.utils import build_embed
from jarvis.utils.cachecog import CacheCog
valid = re.compile(r"[\w\s\-\\/.!@#$%^*()+=<>,\u0080-\U000E0FFF]*") valid = re.compile(r"[\w\s\-\\/.!@#$%^*()+=<>:,\u0080-\U000E0FFF]*")
time_pattern = re.compile(r"(\d+\.?\d?[s|m|h|d|w]{1})\s?") time_pattern = re.compile(r"(\d+\.?\d?[s|m|h|d|w]{1})\s?", flags=re.IGNORECASE)
invites = re.compile( invites = re.compile(
r"(?:https?://)?(?:www.)?(?:discord.(?:gg|io|me|li)|discord(?:app)?.com/invite)/([^\s/]+?)(?=\b)", # noqa: E501 r"(?:https?://)?(?:www.)?(?:discord.(?:gg|io|me|li)|discord(?:app)?.com/invite)/([^\s/]+?)(?=\b)", # noqa: E501
flags=re.IGNORECASE, flags=re.IGNORECASE,
) )
class RemindmeCog(CacheCog): class RemindmeCog(Scale):
"""J.A.R.V.I.S. Remind Me Cog.""" """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_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( @slash_option(
name="private", name="private",
description="Send as DM?", description="Send as DM?",
@ -62,13 +47,35 @@ class RemindmeCog(CacheCog):
async def _remindme( async def _remindme(
self, self,
ctx: InteractionContext, ctx: InteractionContext,
message: str,
delay: str,
private: str = "n", private: str = "n",
) -> None: ) -> None:
private = private == "y" private = private == "y"
if len(message) > 100: modal = Modal(
await ctx.send("Reminder cannot be > 100 characters.", ephemeral=True) 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 return
elif invites.search(message): elif invites.search(message):
await ctx.send( await ctx.send(
@ -83,7 +90,7 @@ class RemindmeCog(CacheCog):
units = {"w": "weeks", "d": "days", "h": "hours", "m": "minutes", "s": "seconds"} units = {"w": "weeks", "d": "days", "h": "hours", "m": "minutes", "s": "seconds"}
delta = {"weeks": 0, "days": 0, "hours": 0, "minutes": 0, "seconds": 0} 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: for t in times:
delta[units[t[-1]]] += float(t[:-1]) delta[units[t[-1]]] += float(t[:-1])
else: else:
@ -96,7 +103,7 @@ class RemindmeCog(CacheCog):
await ctx.send("At least one time period is required", ephemeral=True) await ctx.send("At least one time period is required", ephemeral=True)
return 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: if reminders >= 5:
await ctx.send( await ctx.send(
"You already have 5 (or more) active reminders. " "You already have 5 (or more) active reminders. "
@ -108,7 +115,7 @@ class RemindmeCog(CacheCog):
remind_at = datetime.now() + timedelta(**delta) remind_at = datetime.now() + timedelta(**delta)
r = Reminder( r = Reminder(
user=ctx.author_id, user=ctx.author.id,
channel=ctx.channel.id, channel=ctx.channel.id,
guild=ctx.guild.id, guild=ctx.guild.id,
message=message, message=message,
@ -138,7 +145,7 @@ class RemindmeCog(CacheCog):
) )
embed.set_thumbnail(url=ctx.author.display_avatar.url) 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( async def get_reminders_embed(
self, ctx: InteractionContext, reminders: List[Reminder] self, ctx: InteractionContext, reminders: List[Reminder]