-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgetTodaysQuestion.py
More file actions
97 lines (77 loc) · 3.41 KB
/
getTodaysQuestion.py
File metadata and controls
97 lines (77 loc) · 3.41 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
import random
from getQuestionFromLC import *
from lcUtils import *
from datetime import *
import os.path
import discord
import json
# Method to check if the date has changed, if it has, it will generate a new question and send it to the channel
async def dailyQuestion(client):
if checkNewDate():
embed = getEmbed()
question = getTodayQuestion()
url = "https://leetcode.com/problems/" + question["titleSlug"]
diff = question["difficulty"].lower()
title = question["title"]
embed.description = f"Today's {diff} question: {title} \n {url} \n\n if you're new, do `-help` to learn how to play"
embed.colour = discord.Colour.blue()
for file in os.listdir("servers"):
with open(f"servers/{file}", "r") as readFile:
server = json.load(readFile)
channelId = server['LOTDCHANNEL']
channelCtx = client.get_channel(channelId)
if channelCtx:
if server["LOTDPING"]:
await channelCtx.send(server["LOTDPING"])
await channelCtx.send(embed=embed)
for playerFile in os.listdir("players"): # resets all player's "HASSOLVEDTODAY property to false
with open(f"players/{playerFile}", "r") as readFile:
account = json.load(readFile)
account["HASSOLVEDTODAY"] = False
with open(f"players/{playerFile}", "w") as writeFile:
json.dump(account, writeFile)
def getTodayQuestion():
with open('./questions/todayQuestion.json', 'r') as readFile:
today = json.load(readFile)
with open('./questions/questionsList.json', 'r') as readFile:
questions_list = json.load(readFile)["QUESTIONS"]
today_id = today["LASTINDEX"] + 1
today_question = getQuestion(questions_list[today_id])
today["LASTINDEX"] += 1
today["TODAYDATE"] = str((datetime.utcnow() + timedelta(hours=10)).date())
today["QUESTIONID"] = questions_list[today_id]
today["QUESTIONNAME"] = today_question["title"]
today["QUESTIONSLUG"] = today_question["titleSlug"]
today["DIFFICULTY"] = today_question["difficulty"]
with open('./questions/todayQuestion.json', 'w') as writeFile:
json.dump(today, writeFile)
return today_question
def checkNewDate():
curr_date = (datetime.utcnow() + timedelta(hours=10)).date()
with open('questions/todayQuestion.json', 'r') as readFile:
today = json.load(readFile)
return str(curr_date) != today["TODAYDATE"]
# The following code is used to retrieve and generate a new questions list. This should rarely be run
def sortQuestions(questions, easy, medium, hard):
for i, q in enumerate(questions):
i += 1
if not q["paidOnly"]:
difficulty = q["difficulty"]
if difficulty == "Easy":
easy.append(i)
elif difficulty == "Medium":
medium.append(i)
else:
hard.append(i)
def makeQuestionsList():
questions = getAllQuestions()
easy = []
medium = []
hard = []
sortQuestions(questions, easy, medium, hard)
questionsList = easy + medium
random.shuffle(questionsList)
questionsList = {"QUESTIONS": questionsList}
with open('./questions/questionsList.json', 'w') as writeFile:
json.dump(questionsList, writeFile)
# makeQuestionsList() # only run this to regenerate the questions list. Try to avoid doing this!