-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfunctions.py
80 lines (68 loc) · 2.7 KB
/
functions.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
import datetime
import json
database = "database/database.json"
def update_user(user, data):
with open(database, 'r', encoding='utf-8') as file:
file_json = json.load(file)
file_json.get(user).update(data)
with open(database, 'w', encoding='utf-8') as file_wr:
json.dump(file_json, file_wr, indent=2)
return file_json[user]
def get_user(message):
with open(database, 'r', encoding='utf-8') as file:
file_json = json.load(file)
user_info = message.json['from']
id = str(user_info["id"])
return file_json[id]
def get_user_from_call(message):
with open(database, 'r', encoding='utf-8') as file:
file_json = json.load(file)
user_info = message.json['chat']
id = str(user_info["id"])
return file_json[id]
def get_add_user(message):
"""opens json file and check if user exist if
user does not exist add's and returns user info
if user already exist updates last visited and
returns user info"""
user_info = message.json['from']
id = str(user_info["id"])
with open(database, 'r', encoding='utf-8') as file:
file_json = json.load(file)
if user_info["is_bot"] == False:
if str(id) not in file_json.keys():
new_user_object = {
id: {
"user_id": id,
"user": user_info["first_name"],
"lang": " ",
"registered_date": datetime.datetime.now().isoformat(),
"is_new_user": True
}
}
file_json.update(new_user_object)
with open(database, 'w', encoding='utf-8') as file_wr:
json.dump(file_json, file_wr, indent=2)
return new_user_object[id]
else:
file_json[id]["is_new_user"] = False
file_json[id]["last_visited"] = datetime.datetime.now().isoformat()
with open(database, 'w', encoding='utf-8') as file_wr:
json.dump(file_json, file_wr, indent=2)
return file_json[id]
def set_lang(user_id, language):
"""sets user_id language and saves to json"""
with open(database, 'r', encoding='utf-8') as file:
file_json = json.load(file)
file_json[user_id]["lang"] = language
with open(database, 'w', encoding='utf-8') as file_wr:
json.dump(file_json, file_wr, indent=2)
return language
def first_name(user_id, name):
"""sets user_id first_name and saves to json"""
with open(database, 'r', encoding='utf-8') as file:
file_json = json.load(file)
file_json[user_id]["first_name"] = name
with open(database, 'w', encoding='utf-8') as file_wr:
json.dump(file_json, file_wr, indent=2)
return name