File size: 1,672 Bytes
4694180
 
 
 
 
 
 
 
 
 
 
 
 
 
51e1c40
4694180
89ad488
4694180
51e1c40
6cef7ec
4694180
51e1c40
6cef7ec
4694180
 
 
51e1c40
 
 
 
 
 
4694180
 
89ad488
4694180
 
89ad488
51e1c40
4694180
89ad488
4694180
 
51e1c40
4694180
 
89ad488
51e1c40
 
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
from threading import RLock

from Powers.database import MongoDB

INSERTION_LOCK = RLock()


class AFK(MongoDB):
    """Class to store afk users"""
    db_name = "afk"

    def __init__(self) -> None:
        super().__init__(self.db_name)

    def insert_afk(self, chat_id, user_id, time, reason, media_type, media=None):
        with INSERTION_LOCK:
            if curr := self.check_afk(chat_id=chat_id, user_id=user_id):
                if reason:
                    self.update({"chat_id": chat_id, "user_id": user_id}, {
                        "reason": reason, "time": time})
                if media:
                    self.update({"chat_id": chat_id, "user_id": user_id}, {
                        'media': media, 'media_type': media_type, "time": time})
            else:
                self.insert_one(
                    {
                        "chat_id": chat_id,
                        "user_id": user_id,
                        "reason": reason,
                        "time": time,
                        "media": media,
                        "media_type": media_type
                    }
                )
            return True

    def check_afk(self, chat_id, user_id):
        return bool(curr := self.find_one({"chat_id": chat_id, "user_id": user_id}))

    def get_afk(self, chat_id, user_id):
        if curr := self.find_one({"chat_id": chat_id, "user_id": user_id}):
            return curr
        return

    def delete_afk(self, chat_id, user_id):
        with INSERTION_LOCK:
            if curr := self.check_afk(chat_id, user_id):
                self.delete_one({"chat_id": chat_id, "user_id": user_id})
            return