Create reminder task & command, fix Les aliases

pull/4/head
Stijn De Clercq 2020-10-23 21:34:10 +02:00
parent caf595010b
commit 250c2d40c7
6 changed files with 133 additions and 4 deletions

52
cogs/remind.py 100644
View File

@ -0,0 +1,52 @@
import discord
from discord.ext import commands
from decorators import help
from enums.help_categories import Category
from functions.database import remind
class Remind(commands.Cog):
def __init__(self, client):
self.client = client
# Don't allow any commands to work when locked
def cog_check(self, ctx):
return not self.client.locked
@commands.group(name="Remind", aliases=["Remindme"], usage="[Categorie]", case_insensitive=True, invoke_without_command=True)
async def remind(self, ctx):
"""
Command group to remind the user of a certain thing every day.
:param ctx: Discord Context
"""
categories = ["Les", "Nightly"]
embed = discord.Embed(colour=discord.Colour.blue())
embed.set_author(name="Remind Categorieën")
embed.description = "\n".join(sorted(categories))
await ctx.send(embed=embed)
@remind.command(name="Nightly")
async def nightly(self, ctx):
"""
Command to get a daily Nightly reminder
"""
if remind.switchReminder(ctx.author.id, "nightly"):
await ctx.send("Vanaf nu wordt je er dagelijks aan herinnerd om Didier Nightly te doen.")
else:
await ctx.send("Je zal er niet langer aan herinnerd worden om Didier Nightly te doen.")
@remind.command(name="Les", aliases=["Class", "Classes", "Sched", "Schedule"])
async def les(self, ctx):
"""
Command to get a daily reminder with an embed of your schedule
"""
if remind.switchReminder(ctx.author.id, "les"):
await ctx.send("Vanaf nu krijg je dagelijks je lessenrooster toegestuurd.")
else:
await ctx.send("Je zal je lessenrooster niet langer toegestuurd krijgen.")
def setup(client):
client.add_cog(Remind(client))

View File

@ -41,7 +41,7 @@ class School(commands.Cog):
embed.set_footer(text="Omwille van de coronamaatregelen is er een beperkter aanbod, en kan je enkel nog eten afhalen. Ter plaatse eten is niet meer mogelijk.")
await ctx.send(embed=embed)
@commands.command(name="Les", aliases=["Sched", "Schedule", "Class"], usage="[Jaargang]* [Dag]*")
@commands.command(name="Les", aliases=["Class", "Classes", "Sched", "Schedule"], usage="[Jaargang]* [Dag]*")
@commands.check(checks.allowedChannels)
@help.Category(category=Category.School)
async def les(self, ctx, *day):

View File

@ -1,9 +1,11 @@
from data import constants
from data.remind import Reminders
from discord.ext import commands, tasks
from enums.numbers import Numbers
from functions import timeFormatters
from functions.database import currency, poke, prison, birthdays, stats
import json
from random import random
import requests
import time
@ -181,8 +183,22 @@ class Tasks(commands.Cog):
# Don't do it multiple times a day if bot dc's, ...
with open("files/lastTasks.json", "r") as fp:
lastTasks = json.load(fp)
if int(self.getCurrentHour()) == 0 and int(time.time()) - int(lastTasks["remind"]) > 10000:
pass
if int(self.getCurrentHour()) == 21 and int(time.time()) - int(lastTasks["remind"]) > 10000:
reminders = Reminders()
for category in reminders.categories:
for user in category["users"]:
userInstance = await self.client.fetch_user(user)
# User can't be fetched for whatever reason, ignore instead of crashing
if userInstance is None:
continue
# Check if a special embed has to be attached for this reminder
if "embed" not in category:
await userInstance.send(random.choice(category["messages"]))
else:
await userInstance.send(random.choice(category["messages"]), embed=category["embed"])
# with open("files/lastTasks.json", "w") as fp:
# lastTasks["remind"] = round(time.time())

16
data/remind.py 100644
View File

@ -0,0 +1,16 @@
from functions.database import remind
class Reminders:
def __init__(self):
rows = remind.getAllRows()
self._nightlyUsers = [int(user[0]) for user in rows if user[1]]
self._nightlyMessages = ["Dagelijkse herinnering om Didier Nightly te doen.", "Vrees niet, Nightly-streak-liefhebber! 't Zenne kik, Didier, me ne reminder!"]
self.nightly = {"users": self._nightlyUsers, "messages": self._nightlyMessages}
self._les = [int(user[0]) for user in rows if user[2]]
self._lesMessages = ["Lessenrooster voor vandaag:"]
self.les = {"users": self._les, "messages": self._lesMessages, "embed": None}
self.categories = [self.nightly, self.les]

View File

@ -86,7 +86,9 @@
"releases": "Lijst van de games die in de volgende 30 dagen uitkomen.\nGeef een getal op om volgende/vorige pagina's te bekijken (standaard pagina 1).",
"releases info": "Geeft gedetailleerde informatie over de game met de opgegeven id.",
"reload": "Verwijdert [Cog] & voegt hem daarna terug toe.",
"remind": "Krijg een dagelijkse herinnering om Didier Nightly te doen.\nMeer categorieën en usages komen later:tm:.",
"remind": "Krijg een lijst van categorieën waaraan je dagelijks herinnerd kan worden.\nOm deze herinneringen niet langer te krijgen, kan je opnieuw Didier Remind [Categorie] doen.",
"remind les": "Krijg dagelijks het lessenrooster voor die dag toegestuurd.",
"remind nightly": "Krijg dagelijks een herinnering om Didier Nightly te doen.",
"reverse": "Returnt tekst achterstevoren.",
"rhyme": "Stuurt de 15 beste suggesties voor woorden die rijmen op [Woord].",
"rob": "Probeer Didier Dinks te stelen van [Persoon].",

View File

@ -0,0 +1,43 @@
from functions.database import utils
def getAllRows():
connection = utils.connect()
cursor = connection.cursor()
cursor.execute("SELECT * FROM remind")
return cursor.fetchall()
def getOrAddUser(userid):
connection = utils.connect()
cursor = connection.cursor()
cursor.execute("SELECT * FROM remind WHERE userid = %s", (int(userid),))
res = cursor.fetchall()
if not res:
cursor.execute("INSERT INTO remind(userid) VALUES %s", (int(userid),))
connection.commit()
return getOrAddUser(userid)
return res[0]
def switchReminder(userid, column):
connection = utils.connect()
cursor = connection.cursor()
columns = ["id", "nightly", "les"]
res = getOrAddUser(userid)
# Switch the column value
to = not (res[columns.index(column)])
cursor.execute("UPDATE remind SET %s = %s WHERE userid = %s", (column, to, int(userid),))
connection.commit()
return to