1
0
Fork 0

Switch reminder to slash commands

This commit is contained in:
Ethanell 2021-02-04 10:52:13 +01:00
parent 35a5fec694
commit ebaf45e84a

View file

@ -4,10 +4,12 @@ from discord.ext import commands
from discord import Embed from discord import Embed
from discord.ext.commands import BadArgument from discord.ext.commands import BadArgument
from discord.ext import tasks from discord.ext import tasks
from discord_slash import SlashContext, cog_ext, SlashCommandOptionType
from discord_slash.utils import manage_commands
from administrator.check import is_enabled from administrator.check import is_enabled
from administrator.logger import logger from administrator.logger import logger
from administrator import db from administrator import db, slash
from administrator.utils import time_pars, seconds_to_time_string from administrator.utils import time_pars, seconds_to_time_string
extension_name = "reminders" extension_name = "reminders"
@ -17,58 +19,49 @@ logger = logger.getChild(extension_name)
class Reminders(commands.Cog, name="Reminder"): class Reminders(commands.Cog, name="Reminder"):
def __init__(self, bot: commands.Bot): def __init__(self, bot: commands.Bot):
self.bot = bot self.bot = bot
slash.get_cog_commands(self)
def description(self): def description(self):
return "Create and manage reminders" return "Create and manage reminders"
@commands.group("reminder", pass_context=True) @cog_ext.cog_subcommand(base="reminder", name="add", description="Add a reminder to your reminders list", options=[
manage_commands.create_option("message", "The message", SlashCommandOptionType.STRING, True),
manage_commands.create_option("time", "When, ?D?H?M?S", SlashCommandOptionType.STRING, True)
])
@is_enabled() @is_enabled()
async def reminder(self, ctx: commands.Context): async def reminder_add(self, ctx: SlashContext, message: str, time: str):
if ctx.invoked_subcommand is None:
await ctx.invoke(self.reminder_help)
@reminder.group("help", pass_context=True)
async def reminder_help(self, ctx: commands.Context):
embed = Embed(title="Reminder help")
embed.add_field(name="reminder add <message> <time>", value="Add a reminder to your reminders list\n"
"Time: ?D?H?M?S", inline=False)
embed.add_field(name="reminder list", value="Show your tasks list", inline=False)
embed.add_field(name="reminder remove [N°]", value="Show your tasks list with if no id given\n"
"Remove the task withe the matching id", inline=False)
await ctx.send(embed=embed)
@reminder.group("add", pass_context=True)
async def reminder_add(self, ctx: commands.Context, message: str, time: str):
time = time_pars(time) time = time_pars(time)
now = datetime.now() now = datetime.now()
s = db.Session() s = db.Session()
s.add(db.Task(message, ctx.author.id, ctx.channel.id, now + time, ctx.message.created_at)) s.add(db.Task(message, ctx.author.id, ctx.channel.id, now + time, datetime.now()))
s.commit() s.commit()
s.close() s.close()
await ctx.send(f"""Remind you in {seconds_to_time_string(time.total_seconds())} !""") await ctx.send(content=f"""Remind you in {seconds_to_time_string(time.total_seconds())} !""")
@reminder.group("list", pass_context=True) @cog_ext.cog_subcommand(base="reminder", name="list", description="Show your tasks list")
async def reminder_list(self, ctx: commands.Context): @is_enabled()
async def reminder_list(self, ctx: SlashContext):
embed = Embed(title="Tasks list") embed = Embed(title="Tasks list")
s = db.Session() s = db.Session()
for t in s.query(db.Task).filter(db.Task.user == ctx.author.id).all(): for t in s.query(db.Task).filter(db.Task.user == ctx.author.id).all():
embed.add_field(name=f"{t.id} | {t.date.strftime('%d/%m/%Y %H:%M')}", value=f"{t.message}", inline=False) embed.add_field(name=f"{t.id} | {t.date.strftime('%d/%m/%Y %H:%M')}", value=f"{t.message}", inline=False)
s.close() s.close()
await ctx.send(embed=embed) await ctx.send(embeds=[embed])
@reminder.group("remove", pass_context=True) @cog_ext.cog_subcommand(base="reminder", name="remove", description="Remove the task withe the matching id",
async def reminder_remove(self, ctx: commands.Context, n: int = None): options=[
if n is None: manage_commands.create_option("id", "The reminder id",
await ctx.invoke(self.reminder_list) SlashCommandOptionType.INTEGER, True)])
else: @is_enabled()
async def reminder_remove(self, ctx: SlashContext, n: int):
s = db.Session() s = db.Session()
t = s.query(db.Task).filter(db.Task.id == n).first() t = s.query(db.Task).filter(db.Task.id == n).first()
if t and t.user == ctx.author.id: if t and t.user == ctx.author.id:
s.delete(t) s.delete(t)
s.commit() s.commit()
s.close() s.close()
await ctx.message.add_reaction("\U0001f44d") await ctx.send(content="\U0001f44d")
else: else:
s.close() s.close()
raise BadArgument() raise BadArgument()