-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
394 lines (341 loc) · 14.5 KB
/
bot.py
File metadata and controls
394 lines (341 loc) · 14.5 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import os
import discord
import manage
from discord.ext import commands
from beautifultable import BeautifulTable
import datetime
import json
import mongo
import admin
import datetime
from datetime import timedelta
import asyncio
import time
from threading import Thread
# load env variables
from dotenv import load_dotenv
load_dotenv()
# get token from env variables
token = os.environ.get("bot_token")
threadDict = dict()
# set timezone
os.environ['TZ'] = 'America/Toronto'
time.tzset()
client = commands.Bot(command_prefix='.')
m = manage.Manage()
@client.event
async def on_ready():
# When the bot has everything it needs, it is ready
loadReminders()
timeUntilNine = calculateTimeForDaily()
print('Bot is ready.')
await dailyRemind(timeUntilNine)
def calculateTimeForDaily():
tommorow = datetime.datetime.now()
morningNine = datetime.datetime(year=tommorow.year, month=tommorow.month, day=tommorow.day, hour=9, minute=0, second=0)
return (morningNine - datetime.datetime.now()).seconds
@client.command(brief='sends message to user\'s dm after the specified time interval')
async def remind(ctx, message, sleepTime):
await asyncio.sleep(float(sleepTime)*60*60)
author = ctx.message.author
if author.dm_channel == None:
dm = await author.create_dm()
await dm.send(message)
else:
await author.dm_channel.send(message)
async def dailyRemind(timeUntilNine):
await asyncio.sleep(timeUntilNine)
while True:
data = admin.getTasks('ALL')
for course in data:
for task in course['tasks']:
if datetime.datetime.strptime(task['dueDate'], '%d/%m/%y %H:%M') >= datetime.datetime.now():
await notify(course['courseCode'], task['desc'], task['dueDate'])
await asyncio.sleep(24*60*60)
def loadReminders():
data = admin.getTasks('ALL')
for course in data:
for task in course['tasks']:
if datetime.datetime.strptime(task['dueDate'], '%d/%m/%y %H:%M') >= datetime.datetime.now():
threadDict[course['courseCode']] = {task['desc']: asyncio.Task(reminder(task['dueDate'], course['courseCode'], task['desc']))}
print('done loading')
@client.command(brief='deletes all the expired tasks')
async def deleteOldTasks(ctx, courseCode):
if not isAdmin(ctx.message.author):
await ctx.send("You do not have admin permissions")
return
data = admin.getTasks('ALL')
for course in data:
for task in course['tasks']:
if datetime.datetime.strptime(task['dueDate'], '%d/%m/%y %H:%M') < datetime.datetime.now():
admin.removeTask(course['courseCode'], task['desc'])
await ctx.send('Deleted all expired tasks!')
@client.event
async def on_raw_reaction_add(raw):
#the message that the user reacted to
courseCode=None
if raw.message_id == 750764988448112662:
if str(raw.emoji) == '🍎':
courseCode ='MGTA01'
elif str(raw.emoji) =='🍌':
courseCode ='VPMA93'
if courseCode!=None:
try:
admin.subscribe(raw.member,courseCode)
await client.get_channel(raw.channel_id).send(raw.member.mention+" successfully enrolled in "+courseCode)
except Exception as e:
await client.get_channel(raw.channel_id).send("FAILED! could not subscribe. Error: "+str(e))
@client.event
async def on_raw_reaction_remove(raw):
#the message that the user reacted to
courseCode=None
if raw.message_id == 750764988448112662:
if str(raw.emoji) == '🍎':
courseCode ='MGTA01'
elif str(raw.emoji) =='🍌':
courseCode ='VPMA93'
if courseCode!=None:
try:
user =client.get_user(raw.user_id)
admin.unsubscribe(user.name,courseCode)
await client.get_channel(raw.channel_id).send(user.mention+" successfully unenrolled from "+courseCode)
except Exception as e:
await client.get_channel(raw.channel_id).send("FAILED! could not unsubscribe. Error: "+str(e))
async def reminder(dt, courseCode,description):
newDate = datetime.datetime.strptime(dt, '%d/%m/%y %H:%M')
date1 = datetime.datetime.utcfromtimestamp(newDate.timestamp()) - datetime.datetime.utcnow()
timeUntilRemind = date1.total_seconds()
await asyncio.sleep(timeUntilRemind)
await notify(courseCode,description,dt)
def readFile():
with open("tasks.json") as f:
data = json.load(f)
return data
# def getTasksForDay():
# data = readFile()
# dataList = list()
# for course in data.keys():
# for task in data[course]:
# if datetime.datetime.now() < datetime.datetime.strptime(task["dueDate"], "%d/%m/%y %H:%M"):
# date = datetime.datetime.strptime(task["dueDate"], "%d/%m/%y %H:%M")
# # table.rows.append([course, task['description'], date.strftime("%d/%m/%y"), date.strftime("%H:%M"), task["status"]])
# d = {'course': course, 'desc': task['description'], 'due': date.strftime("%d/%m/%y") + ' ' + date.strftime("%H:%M"), 'status': task['status']}
# dataList.append(d)
# return dataList
def getAll():
data = readFile()
dataList = list()
for course in data.keys():
for task in data[course]:
date = datetime.datetime.strptime(
task["dueDate"], "%d/%m/%y %H:%M")
d = {'course': course, 'desc': task['description'], 'due': date.strftime(
"%d/%m/%y") + ' ' + date.strftime("%H:%M"), 'status': task['status']}
dataList.append(d)
return dataList
@client.command(brief="Adds a task to Mongodb")
async def add(ctx, course, description, dueDate):
user = ctx.message.author
print(dueDate)
try:
datetime.datetime.strptime(dueDate, "%d/%m/%y %H:%M")
except ValueError:
await ctx.send("FAILED! Date is not in the correct format.")
return
print(user)
try:
mongo.addMongo(user.name, course, description, dueDate)
await ctx.send("Successfully added for " + user.mention + " in " + course+" Description: "+ description+" due on "+dueDate)
except Exception as e:
print("FAILED! could not delete task. Error: "+str(e))
@client.command(brief="Removes task from Mongodb")
async def remove(ctx, course, description):
user = ctx.message.author
try:
mongo.removeMongo(user.name,course, description)
await ctx.send("Successfully deleted task \""+ course+" "+description+"\" for "+user.mention)
except Exception as e:
await ctx.send("FAILED! could not delete task. Error: "+str(e))
@client.command(brief="Edits the dueDate of a task")
async def edit(ctx,course,description, newDueDate):
user = ctx.message.author
try:
mongo.editMongo(user.name,course,description,newDueDate)
await ctx.send("Successfully edited due date of \""+ course+" "+description+"\" for "+user.mention+" to \""+newDueDate+"\"")
except Exception as e:
await ctx.send("FAILED! could not edit task. Error: "+str(e))
@client.command(brief="Sets status of task (complete, in progress, not complete)")
async def setStatus(ctx,course,description,status):
user = ctx.message.author
try:
mongo.setStatusMongo(user.name,course,description,status)
await ctx.send("Successfully set status of \""+ course+" "+description+"\" for "+user.mention+" to \""+status+"\"")
except Exception as e:
await ctx.send("FAILED! could not set status for task. Error: "+str(e))
def createEmbed(course, description, due, status):
if status == "complete":
c=discord.Colour.green()
elif status == "in progress":
c=discord.Colour.orange()
else:
c=discord.Colour.red()
embed=discord.Embed(
title=course,
description=description,
colour=c
)
embed.add_field(name='Due', value=due, inline=False)
return embed
@ client.command(brief='Displays all tasks')
async def all(ctx):
data=mongo.getDataFromMongo(ctx.message.author.name)
embedList=list()
for user in data:
for course in user["courses"]:
for task in course["tasks"]:
embed=createEmbed(
course['courseCode'], task['desc'], task['dueDate'], task['status'])
embedList.append(embed)
for embed in embedList:
await ctx.send(embed=embed)
@ client.command(brief='displays all tasks due today')
async def today(ctx):
data=mongo.getDataFromMongo(ctx.message.author.name)
embedList=list()
for user in data:
for course in user["courses"]:
for task in course["tasks"]:
if datetime.datetime.strptime(task['dueDate'], '%d/%m/%y %H:%M') >= datetime.datetime.now():
embed=createEmbed(
course['courseCode'], task['desc'], task['dueDate'], task['status'])
embedList.append(embed)
for embed in embedList:
await ctx.send(embed=embed)
# admin stuff
@client.command(brief="Creates a new course")
async def createCourse(ctx, courseCode):
if not isAdmin(ctx.message.author):
await ctx.send("You do not have admin permissions")
return
try:
admin.createCourse(courseCode)
await ctx.send("Successfully added course "+courseCode)
except Exception as e:
await ctx.send("FAILED! could not add course. Error: "+str(e))
@client.command(brief="Adds a new task to a course")
async def addTask(ctx, courseCode, description, dueDate):
if not isAdmin(ctx.message.author):
await ctx.send("You do not have admin permissions")
return
try:
datetime.datetime.strptime(dueDate, "%d/%m/%y %H:%M")
except ValueError:
await ctx.send("FAILED! Date is not in the correct format.")
return
try:
admin.addTask(courseCode,description,dueDate)
await ctx.send("Successfully added \""+description+"\" due on \""+dueDate+"\" for "+courseCode)
threadDict[courseCode] = {description: asyncio.Task(reminder(dueDate, courseCode,description))}
except Exception as e:
await ctx.send("FAILED! could not add task. Error: "+str(e))
@client.command(brief="Removes a task from a course")
async def removeTask(ctx, courseCode, description):
if not isAdmin(ctx.message.author):
await ctx.send("You do not have admin permissions")
return
try:
admin.removeTask(courseCode,description)
threadDict.pop(courseCode).pop(description).cancel()
await ctx.send("Successfully removed \""+description+"\" for "+courseCode)
except Exception as e:
await ctx.send("FAILED! could not remove task. Error: "+str(e))
@client.command(brief="Edits a task for a course")
async def editTask(ctx, courseCode, description, newDueDate):
user = ctx.message.author
if not isAdmin(user):
await ctx.send("You do not have admin permissions")
return
try:
admin.editTask(courseCode,description,newDueDate)
await ctx.send("Successfully edited due date of \""+ courseCode+" "+description+"\" for "+user.mention+" to \""+newDueDate+"\"")
threadDict.pop(courseCode).pop(description).cancel()
threadDict[courseCode] = {description: asyncio.Task(reminder(newDueDate, courseCode,description))}
except Exception as e:
await ctx.send("FAILED! could not remove task. Error: "+str(e))
@client.command(brief="Enrols user to a course")
async def enroll(ctx, courseCode):
user = ctx.message.author
try:
admin.subscribe(user,courseCode)
await ctx.send(user.mention+" successfully enrolled in "+courseCode)
except Exception as e:
await ctx.send("FAILED! could not subscribe. Error: "+str(e))
@client.command(brief="Unenrols user froms a course")
async def unenroll(ctx, courseCode):
user = ctx.message.author
try:
admin.unsubscribe(user,courseCode)
await ctx.send(user.mention+" successfully unenrolled from "+courseCode)
except Exception as e:
await ctx.send("FAILED! could not unsubscribe. Error: "+e)
@client.command(brief="Shows subsribers for a course (ALL for all courses)")
async def showSubscribers(ctx, courseCode):
if not isAdmin(ctx.message.author):
await ctx.send("You do not have admin permissions")
return
try:
await ctx.send(admin.showSubscribers(courseCode))
except Exception as e:
await ctx.send("FAILED! could not show subscribers. Error: "+str(e))
@client.command(brief="Shows tasks for a course (ALL for all courses)")
async def showTasks(ctx, courseCode):
if not isAdmin(ctx.message.author):
await ctx.send("You do not have admin permissions")
return
try:
data=admin.getTasks(courseCode)
embedList=list()
for course in data:
for task in course["tasks"]:
embed=createEmbed(
course['courseCode'], task['desc'], task['dueDate'], task['status'])
embedList.append(embed)
for embed in embedList:
await ctx.send(embed=embed)
except Exception as e:
await ctx.send("FAILED! could not show tasks. Error: "+str(e))
@client.command()
async def getStatus(ctx, courseCode ,description):
user = ctx.message.author
await ctx.send(mongo.getStatusMongo(user,courseCode,description))
async def notify(courseCode, description, dueDate):
subs = admin.getSubs(courseCode)
for sub in subs:
user = client.get_user(sub["id"])
status = mongo.getStatusMongo(user,courseCode,description)
print(status)
embed=createEmbed(courseCode, description, dueDate,status)
if user.dm_channel == None:
dm = await user.create_dm()
await dm.send(embed=embed)
else:
await user.dm_channel.send(embed=embed)
@client.command()
async def courses(ctx):
await ctx.send(admin.getCourses())
@client.command()
async def time(ctx):
await ctx.send(datetime.datetime.strftime(datetime.datetime.now(), "%d/%m/%y %H:%M"))
def isAdmin(user):
return client.get_channel(748928662614573090).permissions_for(user).administrator
# embed = discord.Embed(
# title = 'MGEA05',
# description = 'Exam',
# colour = discord.Colour.blue()
# )
# embed.add_field(name='Due', value='23/12/21 23:59', inline=False)
# # embed.add_field(name='Status', value='COMPLETE', inline=False) USE REACTIONS INSTEAD
# await ctx.send(embed=embed)
# | Course | Description | Due Date | Due Time | Status |
# | MGEA05 | Exam | 23/12/21 | 23:59 | COMPLETE |
client.run(token)