-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
115 lines (87 loc) · 3.29 KB
/
utils.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
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
from datetime import datetime
import json
############################# Input/Output ###########################################
def parseInput(input: str) -> dict:
input = input.split(" ")
if " " in input:
command = input[0].lower()
args = input[1:]
return command, args
else:
return input[0], None
async def sendMessage(message, text):
await message.channel.send(text)
def toStrID(id:int) -> str:
return f"<@{id}>"
############################# Input/Output ###########################################
############################# DateTime ###########################################
def convertDateStringToDateTime(date: str) -> datetime:
return datetime.strptime(date, '%m/%d')
def convertDateTimeToDateString(date: datetime) -> str:
return date.strftime('%m/%d')
# converts a the current time to a int to compare to other times to tell amount of time difference
def convertDateTimeToInt() -> int:
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
ll = current_time.split(":")
time = 0
for i in range(3):
if i == 0:
time += int(ll[0]) * 60
elif i == 1:
time += int(ll[1])
else:
time += int(ll[2]) / 60
return time
############################# DateTime ###########################################
############################# Validation ###########################################
def isValidDiscordID(discordId: str) -> bool:
return (discordId[0] == "<" and discordId[1] == "@" and discordId[-1] == ">")
def isValidCommand(command: str) -> bool:
return (command[0] == "$")
def validateLengthOfArgs(args: list, length: int) -> bool:
return len(args) == length
def isValidDateString(date: str) -> bool:
try:
datetime.strptime(date, '%m/%d')
return True
except ValueError:
return False
def isValidDiscordIDSingle(args: list[str]) -> bool: # checks if args are a valid discord id, only one allowed. so arg has to be of len 1 and just a id
if validateLengthOfArgs(args,1) and isValidDiscordID(args[0]): # ensures there is only one argument, makes sure arg is an valid ID
return True
return False
############################# Validation ###########################################
############################# JSON ###########################################
# opens JSON
def openJSON() -> dict:
f = open('users.json','r+')
users = json.load(f)
f.close()
return users
#writes a dict to a JSON
def writeJSON(dic):
f = open('users.json','r+')
json.dump(dic, f)
f.close()
# checks if user is being tracked
def isTracked(id:str) -> bool:
jn = openJSON()
if id not in jn["activity"]["user"]:
return False
return True
#Assumes user is being tracked
def getLastMessageTime(id:str) -> int:
jn = openJSON()
return jn["activity"]["user"][id]["lastMsg"]
def updateLastMessageTime(id:str,time:int):
jn = openJSON()
jn["activity"]["user"][id]["lastMsg"] = time
writeJSON(jn)
# add person to activity tracker
def addActivity(id:str):
if not isTracked(id):
jn = openJSON()
jn["activity"]["user"][id] = {"points": 0, "lastMsg": 0}
writeJSON(jn)
############################# JSON ###########################################