Migrate remindme, closes #100
This commit is contained in:
parent
93381d7da0
commit
1594ae32ab
1 changed files with 63 additions and 79 deletions
|
@ -5,26 +5,24 @@ from datetime import datetime, timedelta
|
|||
from typing import List, Optional
|
||||
|
||||
from bson import ObjectId
|
||||
from discord import Embed
|
||||
from discord.ext.commands import Bot
|
||||
from discord.ext.tasks import loop
|
||||
from discord_slash import SlashContext, cog_ext
|
||||
from discord_slash.utils.manage_commands import create_option
|
||||
from discord_slash.utils.manage_components import (
|
||||
create_actionrow,
|
||||
create_select,
|
||||
create_select_option,
|
||||
wait_for_component,
|
||||
from dis_snek import InteractionContext, Snake
|
||||
from dis_snek.ext.tasks.task import Task
|
||||
from dis_snek.ext.tasks.triggers import IntervalTrigger
|
||||
from dis_snek.models.discord.components import ActionRow, Select, SelectOption
|
||||
from dis_snek.models.discord.embed import Embed, EmbedField
|
||||
from dis_snek.models.snek.application_commands import (
|
||||
OptionTypes,
|
||||
slash_command,
|
||||
slash_option,
|
||||
)
|
||||
|
||||
from jarvis.db.models import Reminder
|
||||
from jarvis.utils import build_embed
|
||||
from jarvis.utils.cachecog import CacheCog
|
||||
from jarvis.utils.field import Field
|
||||
|
||||
valid = re.compile(r"[\w\s\-\\/.!@#$%^*()+=<>,\u0080-\U000E0FFF]*")
|
||||
invites = re.compile(
|
||||
r"(?:https?://)?(?:www.)?(?:discord.(?:gg|io|me|li)|discord(?:app)?.com/invite)/([^\s/]+?)(?=\b)",
|
||||
r"(?:https?://)?(?:www.)?(?:discord.(?:gg|io|me|li)|discord(?:app)?.com/invite)/([^\s/]+?)(?=\b)", # noqa: E501
|
||||
flags=re.IGNORECASE,
|
||||
)
|
||||
|
||||
|
@ -32,49 +30,41 @@ invites = re.compile(
|
|||
class RemindmeCog(CacheCog):
|
||||
"""J.A.R.V.I.S. Remind Me Cog."""
|
||||
|
||||
def __init__(self, bot: Bot):
|
||||
def __init__(self, bot: Snake):
|
||||
super().__init__(bot)
|
||||
self._remind.start()
|
||||
|
||||
@cog_ext.cog_slash(
|
||||
name="remindme",
|
||||
description="Set a reminder",
|
||||
options=[
|
||||
create_option(
|
||||
name="message",
|
||||
description="What to remind you of",
|
||||
option_type=3,
|
||||
required=True,
|
||||
),
|
||||
create_option(
|
||||
name="weeks",
|
||||
description="Number of weeks?",
|
||||
option_type=4,
|
||||
required=False,
|
||||
),
|
||||
create_option(
|
||||
name="days",
|
||||
description="Number of days?",
|
||||
option_type=4,
|
||||
required=False,
|
||||
),
|
||||
create_option(
|
||||
name="hours",
|
||||
description="Number of hours?",
|
||||
option_type=4,
|
||||
required=False,
|
||||
),
|
||||
create_option(
|
||||
name="minutes",
|
||||
description="Number of minutes?",
|
||||
option_type=4,
|
||||
required=False,
|
||||
),
|
||||
],
|
||||
@slash_command(name="remindme", description="Set a reminder")
|
||||
@slash_option(
|
||||
name="message",
|
||||
description="What to remind you of?",
|
||||
option_type=OptionTypes.STRING,
|
||||
required=True,
|
||||
)
|
||||
@slash_option(
|
||||
name="weeks",
|
||||
description="Number of weeks?",
|
||||
option_type=OptionTypes.INTEGER,
|
||||
required=False,
|
||||
)
|
||||
@slash_option(
|
||||
name="days", description="Number of days?", option_type=OptionTypes.INTEGER, required=False
|
||||
)
|
||||
@slash_option(
|
||||
name="hours",
|
||||
description="Number of hours?",
|
||||
option_type=OptionTypes.INTEGER,
|
||||
required=False,
|
||||
)
|
||||
@slash_option(
|
||||
name="minutes",
|
||||
description="Number of minutes?",
|
||||
option_type=OptionTypes.INTEGER,
|
||||
required=False,
|
||||
)
|
||||
async def _remindme(
|
||||
self,
|
||||
ctx: SlashContext,
|
||||
ctx: InteractionContext,
|
||||
message: Optional[str] = None,
|
||||
weeks: Optional[int] = 0,
|
||||
days: Optional[int] = 0,
|
||||
|
@ -148,8 +138,8 @@ class RemindmeCog(CacheCog):
|
|||
title="Reminder Set",
|
||||
description=f"{ctx.author.mention} set a reminder",
|
||||
fields=[
|
||||
Field(name="Message", value=message),
|
||||
Field(
|
||||
EmbedField(name="Message", value=message),
|
||||
EmbedField(
|
||||
name="When",
|
||||
value=remind_at.strftime("%Y-%m-%d %H:%M UTC"),
|
||||
inline=False,
|
||||
|
@ -158,19 +148,21 @@ class RemindmeCog(CacheCog):
|
|||
)
|
||||
|
||||
embed.set_author(
|
||||
name=ctx.author.name + "#" + ctx.author.discriminator,
|
||||
name=ctx.author.username + "#" + ctx.author.discriminator,
|
||||
icon_url=ctx.author.avatar_url,
|
||||
)
|
||||
embed.set_thumbnail(url=ctx.author.avatar_url)
|
||||
|
||||
await ctx.send(embed=embed)
|
||||
|
||||
async def get_reminders_embed(self, ctx: SlashContext, reminders: List[Reminder]) -> Embed:
|
||||
async def get_reminders_embed(
|
||||
self, ctx: InteractionContext, reminders: List[Reminder]
|
||||
) -> Embed:
|
||||
"""Build embed for paginator."""
|
||||
fields = []
|
||||
for reminder in reminders:
|
||||
fields.append(
|
||||
Field(
|
||||
EmbedField(
|
||||
name=reminder.remind_at.strftime("%Y-%m-%d %H:%M UTC"),
|
||||
value=f"{reminder.message}\n\u200b",
|
||||
inline=False,
|
||||
|
@ -184,19 +176,15 @@ class RemindmeCog(CacheCog):
|
|||
)
|
||||
|
||||
embed.set_author(
|
||||
name=ctx.author.name + "#" + ctx.author.discriminator,
|
||||
name=ctx.author.username + "#" + ctx.author.discriminator,
|
||||
icon_url=ctx.author.avatar_url,
|
||||
)
|
||||
embed.set_thumbnail(url=ctx.author.avatar_url)
|
||||
|
||||
return embed
|
||||
|
||||
@cog_ext.cog_subcommand(
|
||||
base="reminders",
|
||||
name="list",
|
||||
description="List reminders for a user",
|
||||
)
|
||||
async def _list(self, ctx: SlashContext) -> None:
|
||||
@slash_command(name="reminders", sub_cmd_name="list", sub_cmd_description="List reminders")
|
||||
async def _list(self, ctx: InteractionContext) -> None:
|
||||
exists = self.check_cache(ctx)
|
||||
if exists:
|
||||
await ctx.defer(hidden=True)
|
||||
|
@ -214,12 +202,8 @@ class RemindmeCog(CacheCog):
|
|||
|
||||
await ctx.send(embed=embed)
|
||||
|
||||
@cog_ext.cog_subcommand(
|
||||
base="reminders",
|
||||
name="delete",
|
||||
description="Delete a reminder",
|
||||
)
|
||||
async def _delete(self, ctx: SlashContext) -> None:
|
||||
@slash_command(name="reminders", sub_cmd_name="delete", sub_cmd_description="Delete a reminder")
|
||||
async def _delete(self, ctx: InteractionContext) -> None:
|
||||
reminders = Reminder.objects(user=ctx.author.id, active=True)
|
||||
if not reminders:
|
||||
await ctx.send("You have no reminders set", hidden=True)
|
||||
|
@ -227,14 +211,14 @@ class RemindmeCog(CacheCog):
|
|||
|
||||
options = []
|
||||
for reminder in reminders:
|
||||
option = create_select_option(
|
||||
option = SelectOption(
|
||||
label=reminder.remind_at.strftime("%Y-%m-%d %H:%M UTC"),
|
||||
value=str(reminder.id),
|
||||
emoji="⏰",
|
||||
)
|
||||
options.append(option)
|
||||
|
||||
select = create_select(
|
||||
select = Select(
|
||||
options=options,
|
||||
custom_id="to_delete",
|
||||
placeholder="Select reminders to delete",
|
||||
|
@ -242,7 +226,7 @@ class RemindmeCog(CacheCog):
|
|||
max_values=len(reminders),
|
||||
)
|
||||
|
||||
components = [create_actionrow(select)]
|
||||
components = [ActionRow(select)]
|
||||
embed = await self.get_reminders_embed(ctx, reminders)
|
||||
message = await ctx.send(
|
||||
content=f"You have {len(reminders)} reminder(s) set:",
|
||||
|
@ -251,13 +235,13 @@ class RemindmeCog(CacheCog):
|
|||
)
|
||||
|
||||
try:
|
||||
context = await wait_for_component(
|
||||
self.bot,
|
||||
used_component = await self.bot.wait_for_component(
|
||||
check=lambda x: ctx.author.id == x.author_id,
|
||||
messages=message,
|
||||
components=components,
|
||||
timeout=60 * 5,
|
||||
)
|
||||
for to_delete in context.selected_options:
|
||||
for to_delete in used_component.context.values:
|
||||
_ = Reminder.objects(user=ctx.author.id, id=ObjectId(to_delete)).delete()
|
||||
|
||||
for row in components:
|
||||
|
@ -265,9 +249,9 @@ class RemindmeCog(CacheCog):
|
|||
component["disabled"] = True
|
||||
|
||||
fields = []
|
||||
for reminder in filter(lambda x: str(x.id) in context.selected_options, reminders):
|
||||
for reminder in filter(lambda x: str(x.id) in used_component.context.values, reminders):
|
||||
fields.append(
|
||||
Field(
|
||||
EmbedField(
|
||||
name=reminder.remind_at.strftime("%Y-%m-%d %H:%M UTC"),
|
||||
value=reminder.message,
|
||||
inline=False,
|
||||
|
@ -285,8 +269,8 @@ class RemindmeCog(CacheCog):
|
|||
)
|
||||
embed.set_thumbnail(url=ctx.author.avatar_url)
|
||||
|
||||
await context.edit_origin(
|
||||
content=f"Deleted {len(context.selected_options)} reminder(s)",
|
||||
await used_component.context.edit_origin(
|
||||
content=f"Deleted {len(used_component.context.values)} reminder(s)",
|
||||
components=components,
|
||||
embed=embed,
|
||||
)
|
||||
|
@ -296,7 +280,7 @@ class RemindmeCog(CacheCog):
|
|||
component["disabled"] = True
|
||||
await message.edit(components=components)
|
||||
|
||||
@loop(seconds=15)
|
||||
@Task.create(trigger=IntervalTrigger(seconds=15))
|
||||
async def _remind(self) -> None:
|
||||
reminders = Reminder.objects(remind_at__lte=datetime.utcnow() + timedelta(seconds=30))
|
||||
for reminder in reminders:
|
||||
|
@ -326,6 +310,6 @@ class RemindmeCog(CacheCog):
|
|||
reminder.delete()
|
||||
|
||||
|
||||
def setup(bot: Bot) -> None:
|
||||
def setup(bot: Snake) -> None:
|
||||
"""Add RemindmeCog to J.A.R.V.I.S."""
|
||||
bot.add_cog(RemindmeCog(bot))
|
||||
|
|
Loading…
Add table
Reference in a new issue