-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreminder.py
70 lines (54 loc) · 2.01 KB
/
reminder.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from datetime import datetime, timedelta
from discord.ext import tasks
import discord
class Reminder:
def __init__(self) -> None:
self.reminder_date: datetime = None
self.index = 0
self.message = None
def callback(self):
if self.index >= len(self.intervals):
print('Done')
return
delta = timedelta(hours=self.intervals[self.index])
self.reminder_date = self.date_end - delta
days_left = delta.days
hours_left = delta.seconds // 3600
if self.reminder_date <= datetime.now():
print('Skip')
self.index += 1
self.callback()
return
self.message = f"Time Check: `{days_left} day(s) and {hours_left} hour(s)` left before the jam ends"
print(self.intervals[self.index])
# Start the reminder
if not self.task.is_running():
self.task.start()
else:
self.task.restart()
self.index += 1
def set_and_start(self, title: str, theme: str, date_end: datetime, intervals: list, channel: discord.TextChannel, role: discord.Role):
# Set the reminder
self.index = 0
self.title = title
self.theme = theme
self.date_end = date_end
self.intervals = sorted(intervals, reverse=True)
self.channel = channel
self.role = role
# Start the reminder
self.callback()
def time_left(self)->timedelta:
return self.date_end - datetime.now()
def force_stop(self):
if self.task.is_running():
self.task.cancel()
@tasks.loop(seconds=5)
async def task(self):
# For debugging only
print(self.reminder_date - datetime.now())
if datetime.now() >= self.reminder_date:
print('Reminder done')
await self.channel.send(f'{self.message} {self.role.mention}', allowed_mentions=discord.AllowedMentions(everyone=True))
self.task.cancel()
self.callback()