-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbot_state.py
More file actions
67 lines (57 loc) · 1.71 KB
/
Copy pathbot_state.py
File metadata and controls
67 lines (57 loc) · 1.71 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
import json
from modules.db import get_connection
def get_sett(user_id):
default_settings = [1, 0, None, 0, 0, 1, 0, 0, 'None', 1]
conn = get_connection()
try:
cursor = conn.execute('SELECT settings FROM user_settings WHERE user_id = ?', (user_id,))
row = cursor.fetchone()
if row:
try:
return json.loads(row['settings'])
except json.JSONDecodeError:
return default_settings
else:
return default_settings
finally:
conn.close()
def set_sett(user_id, key, value):
mapping = {
'eeg': 0,
'state': 1,
'dflag': 3,
'tflag': 4,
'news': 5,
'forcelang': 8
}
current_settings = get_sett(user_id)
if isinstance(key, int):
if key < len(current_settings):
current_settings[key] = value
elif key in mapping:
idx = mapping[key]
if idx < len(current_settings):
current_settings[idx] = value
conn = get_connection()
try:
conn.execute('''
INSERT OR REPLACE INTO user_settings (user_id, settings)
VALUES (?, ?)
''', (user_id, json.dumps(current_settings)))
conn.commit()
finally:
conn.close()
def get_users():
conn = get_connection()
try:
cursor = conn.execute('SELECT user_id, settings FROM user_settings')
rows = cursor.fetchall()
return {str(row['user_id']): json.loads(row['settings']) for row in rows}
finally:
conn.close()
def new_data(user_id, link):
from modules.utils import usl
usl(str(user_id), link)
def get_link(user_id):
from modules.utils import get_usl
return get_usl(str(user_id))