async def free_tutor(ctx) -> None: # make sure it is a tutor using the command discordID = f'{ctx.message.author.name}#{ctx.message.author.discriminator}' if discordID in currentTutoringDict: eastern = timezone('US/Eastern') # get current time now = datetime.now(eastern) currentTime = f'{now.hour}:{now.minute}:{now.second}' # get the students id student = currentTutoringDict[discordID][0] # get current day day = {0: 'Monday', 1: 'Tuesday', 2: 'Wednesday', 3: 'Thursday', 4: 'Friday', 5: 'Saturday', 6: 'Sunday'}[now.weekday()] # gather info to insert into db insertVals = [discordID, currentTutoringDict[discordID][1], currentTime, day, student, currentTutoringDict[discordID][2], now] # inser it con = access.Connection(db_host, db_user, db_pw, db_name, False) con.insert("tutoringlogs", 'discordID, startTime, endTime, DAY, studentDiscordID, classTutored, fullDate', insertVals) con.close_connection() # tell student and tutor the session is over bot.loop.create_task(sendDM(student, f'Hello {memberDict[student].mention}, your tutoring session has ended.')) bot.loop.create_task(sendDM(discordID, f'Your tutoring session has ended.')) # remove the session from the dictionary del currentTutoringDict[discordID]
async def add_me(ctx, fName, lName): # get the info to insert insertVals = [ctx.message.author, fName, lName] con = access.Connection(db_host, db_user, db_pw, db_name, False) # insert it con.insert("Student", 'discordID, firstName, lastName', insertVals) con.close_connection(); # tell them they are added await ctx.message.author.create_dm() await ctx.message.author.dm_channel.send( f'Hello {ctx.message.author.mention}, you have been added!' )
def getAllTutors(): load_dotenv() db_host = os.environ.get('DB_HOST') db_user = os.environ.get('DB_USER') db_pw = os.environ.get('DB_PW') db_name = os.environ.get('DB_NAME') con = access.Connection(db_host, db_user, db_pw, db_name, False) ret = [] tutors = con.select("discordID", "tutor", "") con.close_connection() for entry in tutors: ret.append(entry[0]) return ret
def getCurrentTutors(): load_dotenv() db_host = os.environ.get('DB_HOST') db_user = os.environ.get('DB_USER') db_pw = os.environ.get('DB_PW') db_name = os.environ.get('DB_NAME') con = access.Connection(db_host, db_user, db_pw, db_name, False) # datetime object containing current date and time eastern = timezone('US/Eastern') now = datetime.now(eastern) print(now) currentTime = f'{now.hour}:{now.minute}:{now.second}' day = {0: 'Monday', 1: 'Tuesday', 2: 'Wednesday', 3: 'Thursday', 4: 'Friday', 5: 'Saturday', 6: 'Sunday'}[now.weekday()] ret = [] tutors = con.select("discordID", "tutoringtimeslots", f"startTime <= '{currentTime}' and endTime >= '{currentTime}' and day = '{day}'") con.close_connection() for entry in tutors: ret.append(entry[0]) return ret
def send_logs(receivers): load_dotenv() db_host = os.environ.get('DB_HOST') db_user = os.environ.get('DB_USER') db_pw = os.environ.get('DB_PW') db_name = os.environ.get('DB_NAME') con = access.Connection(db_host, db_user, db_pw, db_name, False) from datetime import datetime, timedelta eastern = timezone('US/Eastern') # get current time dt = datetime.now(eastern) start = dt - timedelta(days=dt.weekday()) print(start) sql = f''' SELECT discordID as "tutorID", startTime, endTime, DAY, studentDiscordID, classTutored FROM tutoringlogs WHERE fullDate >= '{start}' ''' sql_all_logs = ''' SELECT discordID as "tutorID", startTime, endTime, DAY, studentDiscordID, classTutored FROM tutoringlogs ''' logs = con.raw_select(sql) import csv log_file = start.strftime('%B%d').lower() + "logs.csv" print(log_file) with open(log_file, mode='w+', newline='') as log_file: fieldnames = [ 'tutorID', 'startTime', 'endTime', 'day', 'studentDiscordID', 'classTutored' ] log_writer = csv.writer(log_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) log_writer.writerow(fieldnames) for row in logs: log_writer.writerow(row) classes_tutored = dict() total_hours = 0 hours_per_day = { 'Monday': 0, 'Tuesday': 0, 'Wednesday': 0, 'Thursday': 0, 'Friday': 0 } sessions_per_day = { 'Monday': 0, 'Tuesday': 0, 'Wednesday': 0, 'Thursday': 0, 'Friday': 0 } weekday_to_weekname = { 0: 'Monday', 1: 'Tuesday', 2: 'Wednesday', 3: 'Thursday', 4: 'Friday' } html_table = ''' <table> <tr class="header"> <td>Tutor ID</td> <td>Student ID</td> <td>Start Time</td> <td>End Time</td> <td>Class Tutored</td> </tr> ''' for row in logs: tutor_id, start_time, end_time, day, student_id, class_tutored = row if class_tutored in classes_tutored.keys(): classes_tutored[class_tutored] += 1 else: classes_tutored[class_tutored] = 1 html_table += f''' <tr> <td>{tutor_id}</td> <td>{student_id}</td> <td>{start_time}</td> <td>{end_time}</td> <td>{class_tutored}</td> </tr> ''' dt = datetime.now(eastern) start_time = dt + start_time end_time = dt + end_time if (end_time - start_time).total_seconds() > 0: total_hours += (end_time - start_time).total_seconds() / 3600 hours_per_day[day] += (end_time - start_time).total_seconds() / 3600 sessions_per_day[day] += 1 x = list(hours_per_day.keys()) y = list(hours_per_day.values()) most_tutored_class = max(classes_tutored, key=classes_tutored.get) print(x) print(y) plt.xlabel('Day', fontsize=16) plt.ylabel('Hours', fontsize=16) plt.title('Tutoring hours per day') plt.bar(x, y, width=0.3) plt.savefig('graph.png', bbox_inches='tight') html_table += '</table>' print(total_hours) print(hours_per_day) busiest_day = 'Monday' busiest_day_hours = 0 busiest_day_session = 'Monday' busiest_day_session_num = 0 for key in hours_per_day.keys(): if hours_per_day[key] > busiest_day_hours: busiest_day_hours = hours_per_day[key] busiest_day = key if sessions_per_day[key] > busiest_day_session_num: busiest_day_session_num = sessions_per_day[key] busiest_day_session = key print(busiest_day) import sys sender_email = os.environ.get('BOT_EMAIL') receiver_email = "*****@*****.**" password = os.environ.get("BOT_PASSWORD") message = MIMEMultipart("alternative") message["From"] = sender_email message["To"] = ', '.join(receivers) message["Subject"] = start.strftime('%B %d') + " tutoring logs" # Create the plain-text and HTML version of your message text = """\ Tutoring bot logs """ html = """\ <html> <body> <h4>Logs for the week of """ + start.strftime('%B %d') + """.</h4> <p> Total hours this week: """ + str(int(round(total_hours))) + """</p> <p>Busiest day (max hours): """ + str(busiest_day) + """</p> <p>Busiest day (most sessions): """ + str(busiest_day_session) + """</p> <p>Most tutored class: """ + most_tutored_class + """</p> <p> <img src="cid:0"> </p> <p> A complete csv file of the logs has been attached to this email. </p> <footer> <h3>EKU CSC tutoring bot</h3> </footer> </body> </html> """ # Turn these into plain/html MIMEText objects part1 = MIMEText(text, "plain") part2 = MIMEText(html, "html") with open(log_file.name, "rb") as attachment: part = MIMEBase('application', 'octet-stream') part.set_payload(attachment.read()) encoders.encode_base64(part) part.add_header( "Content-Disposition", f"attachment; filename= {log_file.name}", ) message.attach(part) with open('graph.png', 'rb') as attachment: img = MIMEImage(attachment.read()) img.add_header('Content-Disposition', "attachment", filename='graph.png') img.add_header('Content-ID', '<0>') message.attach(img) text = message.as_string() # Add HTML/plain-text parts to MIMEMultipart message # The email client will try to render the last part first message.attach(part1) message.attach(part2) # Create secure connection with server and send email context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", 465, context=context) as server: server.login(sender_email, password) server.sendmail(sender_email, receivers, message.as_string())