forked from TomHuynhSG/Smart-School-Chatbot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommands.py
More file actions
221 lines (150 loc) · 6.28 KB
/
Copy pathcommands.py
File metadata and controls
221 lines (150 loc) · 6.28 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
import discord
import random
from datetime import datetime
from dotenv import load_dotenv
import gsheet
import numpy as np
import os
load_dotenv()
BOT_NAME = os.getenv('BOT_NAME')
# Command: Send important links
def links(message, channel):
try:
embed = discord.Embed(title = "Here are some useful links!", color=0xd74742)
data = gsheet.get_metadata()
links = data[data['Channel'] == channel].iloc[:, 3:].replace('',np.nan).dropna(axis = 1).T.iloc[:,0].to_dict()
embed.set_image(url = 'https://i.imgur.com/ZmMo5ay.png')
for name, value in links.items():
embed.add_field(name = name, value = value, inline = False)
except Exception as err:
print(err)
return {'embed': embed}
# Command: Check-in
def hello(message, channel):
# Check if it is weekend
if datetime.now().weekday() in [5,6]:
return 'There is no class today!'
today = datetime.strftime(datetime.now().date(), '%Y-%m-%d')
class_status="Full_time"
try:
data = gsheet.get_metadata()
class_status = data[data['Channel'] == channel]['Class Status'].values[0]
spreadsheet_scoring_id = data[data['Channel'] == channel].iloc[:, 2].to_numpy()[0]
# print(spreadsheet_scoring_id)
# Get list of users and their Disord account
users = gsheet.get_users(spreadsheet_scoring_id)
# print("USERRRR:", users)
if users == {}:
return {'content': f'Cohort is inactive!'}
user = users[message.author.name]
# print("Current User Discord:", message.author.name)
# print("Their Real Name:", user)
except Exception as err:
print(err)
return f'Oh no, Student {message.author.name} not found! (Your discord name is not in the spreadsheet!)'
# Generate unique key
key = today +' | '+ user + ' | attendance'
# print("key:", key)
# Check if key already exists
if key in gsheet.get_keys(spreadsheet_scoring_id):
return f'Student {message.author.name} has already checked-in today!'
now = datetime.now().time()
# print("Class_status:", class_status)
if class_status=="Full_time":
late = datetime.strptime('10:15:00','%H:%M:%S').time()
else:
late = datetime.strptime('19:15:00','%H:%M:%S').time()
# Give point if on-time. minus point if late
if now > late:
score = -2
else:
score = 1
# Generate Attendance data row
body = {'values': [[today, user, score, 'attendance', key]]}
# Append data row to Student Scoring file
gsheet.add_point(body, spreadsheet_scoring_id)
return "Done"
# Command: Show missing students
def missing(message, channel):
try:
# Get channel_id from metadata
metadata = gsheet.get_metadata()
spreadsheet_scoring_id = metadata[metadata['Channel'] == channel].iloc[:, 2].to_numpy()[0]
cohort = metadata[metadata['Channel'] == channel]['Cohort'].values[0]
# Get list of users and their Disord account
users = gsheet.get_users(spreadsheet_scoring_id, cohort)
if users == {}:
return f'Cohort is inactive!'
# Today
today = datetime.strftime(datetime.now().date(), '%Y-%m-%d')
# Unique keys from log
log = gsheet.get_keys(spreadsheet_scoring_id)
count = 0
missing = []
for discord_name, user in users.items():
key = today +" | "+user +" | attendance"
if key not in log:
count += 1
missing.append(discord_name)
if count == 0:
message = "Everybody is here! Yayyy!"
else:
message = f"We are missing {count} student(s):\n"
for s in missing:
message += f"- {s}\n"
except Exception as err:
print(err)
return message
# Command: Give points to students
def give(message, channel):
# Verify authorized users
metadata = gsheet.get_metadata()
authorized_users = metadata[metadata['Channel'] == channel]['Instructors'].values[0]
spreadsheet_scoring_id = metadata[metadata['Channel'] == channel].iloc[:, 2].to_numpy()[0]
# print(authorized_users)
# print(message.author.name)
if message.author.name not in authorized_users.split(','):
return 'Authorization Error: Your are not allowed to give points!'
# Verify point
point, activity = message.content.split()[2:4]
# print("activity", activity)
# print(spreadsheet_scoring_id)
# Verify activity
valid_activities = gsheet.get_activity(spreadsheet_scoring_id)
# print("activities", valid_activities)
if activity not in valid_activities:
return 'Invalid Argument: Activity not found.'
# List of mentioned students, excluding bot
mentions = [u.name for u in message.mentions if u.name != BOT_NAME]
# Verfiy users
if len (mentions) == 0:
return 'Invalid Argument: No student specified.'
try:
users = gsheet.get_users(spreadsheet_scoring_id)
if users == {}:
return f'Cohort is inactive!'
students = []
for s in mentions:
students.append(users[s])
except Exception as err:
print(err)
return f'Student {s} not found!'
# Notes
notes = message.content.split()[4:]
for i, word in enumerate(notes):
# Exclude mentioned from notess
if word.startswith('<@'):
notes[i] = ''
notes = ' '.join(notes)
# Today
today = datetime.strftime(datetime.now().date(), '%Y-%m-%d')
# Generate data row
body = {'values': [[today, student, point, activity, notes] for student in students]}
# Append data row
gsheet.add_point(body, spreadsheet_scoring_id)
congrats = ['Good job', 'Spendid', 'You are amazing', 'Keep it up', 'Love you', 'Amazing skill','Super smart', 'Pretty awesome','You are the best','Absolutely amazing']
comforts = ["It's ok :((", "Sad sad...", "Try again next time!", "It happens even to the best of us!", "Oh no..."]
if int(point) > 0:
return f'{", ".join(mentions)} just earned **{point} point(s)** in **{activity}**! {random.choice(congrats)}!'
else:
return f'{", ".join(mentions)} just got points deduced **{point} point(s)** in **{activity}**! {random.choice(comforts)}!'