ryuko-ng/cogs/remind.py

70 lines
2.6 KiB
Python
Raw Normal View History

import discord
2018-12-30 22:53:55 +00:00
import asyncio
import time
from datetime import datetime
from discord.ext import commands
from discord.ext.commands import Cog
from helpers.robocronp import add_job, get_crontab
class Remind(Cog):
def __init__(self, bot):
self.bot = bot
@commands.cooldown(1, 60, type=commands.BucketType.user)
@commands.command()
async def remindlist(self, ctx):
"""Lists your reminders."""
ctab = get_crontab()
2018-12-30 23:13:04 +00:00
uid = str(ctx.author.id)
embed = discord.Embed(title=f"Active robocronp jobs")
for jobtimestamp in ctab["remind"]:
2018-12-30 23:13:04 +00:00
if uid not in ctab["remind"][jobtimestamp]:
continue
job_details = ctab["remind"][jobtimestamp][uid]
expiry_timestr = datetime.utcfromtimestamp(int(jobtimestamp))\
.strftime('%Y-%m-%d %H:%M:%S (UTC)')
embed.add_field(name=f"Reminder for {expiry_timestr}",
value=f"Added on: {job_details['added']}, "
f"Text: {job_details['text']}",
inline=False)
await ctx.send(embed=embed)
@commands.cooldown(1, 60, type=commands.BucketType.user)
2018-12-30 23:08:38 +00:00
@commands.command(aliases=["remindme"])
async def remind(self, ctx, when: str, *, text: str = "something"):
"""Reminds you about something."""
2019-01-01 23:15:53 +00:00
if ctx.guild:
await ctx.message.delete()
current_timestamp = time.time()
expiry_timestamp = self.bot.parse_time(when)
if current_timestamp + 5 > expiry_timestamp:
2018-12-30 22:53:55 +00:00
msg = await ctx.send(f"{ctx.author.mention}: Minimum "
"remind interval is 5 seconds.")
await asyncio.sleep(5)
await msg.delete()
return
expiry_datetime = datetime.utcfromtimestamp(expiry_timestamp)
duration_text = self.bot.get_relative_timestamp(time_to=expiry_datetime,
include_to=True,
humanized=True)
2019-01-07 08:49:19 +00:00
safe_text = await commands.clean_content().convert(ctx, str(text))
added_on = datetime.utcnow().strftime("%Y-%m-%d %H:%M:%S (UTC)")
add_job("remind",
ctx.author.id,
{"text": safe_text, "added": added_on},
expiry_timestamp)
2018-12-30 22:53:55 +00:00
msg = await ctx.send(f"{ctx.author.mention}: I'll remind you in "
f"DMs about `{safe_text}` in {duration_text}.")
await asyncio.sleep(5)
await msg.delete()
def setup(bot):
bot.add_cog(Remind(bot))