This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
afk_sql.py
82 lines (59 loc) · 1.85 KB
/
afk_sql.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
from sqlalchemy import Column, UnicodeText, Boolean, Integer, DateTime
from sql import BASE, SESSION
from datetime import datetime
class AFK(BASE):
__tablename__ = "afk_users"
user_id = Column(Integer, primary_key=True)
is_afk = Column(Boolean)
reason = Column(UnicodeText, nullable=True)
since = Column(DateTime)
def __init__(self, user_id, since, reason=None, is_afk=True):
self.user_id = user_id
self.reason = reason if len(reason) != 0 else None
self.is_afk = is_afk
self.since = since
AFK.__table__.create(checkfirst=True)
AFK_USERS = {}
def is_afk(user_id):
return user_id in AFK_USERS
def check_afk_status(user_id):
if user_id in AFK_USERS:
return True, AFK_USERS[user_id][0], AFK_USERS[user_id][1]
return False, None, None
def set_afk(user_id, reason=None):
try:
curr = SESSION.query(AFK).get(user_id)
if not curr:
curr = AFK(user_id, datetime.utcnow(), reason, True)
else:
curr.is_afk = True
curr.reason = reason
if not curr.since:
curr.since = datetime.utcnow()
AFK_USERS[user_id] = [reason, curr.since]
SESSION.add(curr)
SESSION.commit()
except:
SESSION.rollback()
raise
def rm_afk(user_id):
curr = SESSION.query(AFK).get(user_id)
if curr:
if user_id in AFK_USERS:
del AFK_USERS[user_id]
SESSION.delete(curr)
SESSION.commit()
return True
SESSION.close()
return False
def num_afk():
return len(AFK_USERS)
def __load_afk_users():
global AFK_USERS
try:
all_afk = SESSION.query(AFK).all()
AFK_USERS = {user.user_id: [user.reason, user.since]
for user in all_afk if user.is_afk}
finally:
SESSION.close()
__load_afk_users()