-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
60 lines (52 loc) · 1.8 KB
/
db.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
import motor.motor_asyncio
from typing import Dict
class KekDB(object):
def __init__(self):
"""
Connect to MongoDB
"""
self.client = motor.motor_asyncio.AsyncIOMotorClient()
self.db = self.client['svh']
self.quotes = self.db['quotes']
self.quote_log = self.db['qlog']
self.history_log = self.db['history']
def close(self):
"""
Close connection to MongoDB
"""
self.client.close()
def get_quotes_sync(self, options: dict = {}) -> list:
"""
[blocking] Get all quotes from db or some of them filtered by options
"""
cursor = self.quotes.find(options)
return cursor.to_list()
def log_quote(self, user_id: str, quote_id: str, timestamp: int):
"""
Logs quote usage without caring about result
:param user_id: user to whom quote applied
:param quote_id: id of quote applied (see self.quotes collection)
:param timestamp: when the quote was sent
"""
self.quote_log.insert_one({
'user': user_id,
'quote': quote_id,
'time': timestamp
})
def log_history(self, chat_id: int, message: dict, text: str, sticker: dict, timestamp: int):
"""
Logs chat messages anonymously without caring about result
Used to collect training data (to make bot more kekable)
:param chat_id: chat where message sent
:param message: raw message object
:param text: message text
:param sticker: raw sticker object
:param timestamp: telegram time
"""
self.history_log.insert_one({
'chat': chat_id,
'message': message,
'text': text,
'sticker': sticker,
'time': timestamp
})