-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabase.py
92 lines (64 loc) · 2.07 KB
/
database.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
83
84
85
86
87
88
89
90
91
92
import pymongo
from config import MongoDB_URI, database, collection
client = pymongo.MongoClient(MongoDB_URI,connect=False)
db = client[database]
def scrape(data):
userid = data.chat.id
chattype = data.chat.type
mycollection = db[collection['user']]
if chattype == 'group' or chattype == "supergroup":
mycollection = db[collection['group']]
title = data.chat.title
firstseen = data.date
result = mycollection.find_one({'userid': userid})
try:
result['userid']
userexist = True
except:
userexist = False
username = data.from_user.username
firstname = data.from_user.first_name
lastname = data.from_user.last_name
dc = data.from_user.dc_id
scraped = {}
scraped['userid'] = userid
scraped['chattype'] = chattype
if chattype == 'group' or chattype == "supergroup":
scraped['title'] = title
else:
scraped['username'] = username
scraped['firstname'] = firstname
scraped['lastname'] = lastname
scraped['is-banned'] = False
scraped['dc'] = dc
scraped['mode'] = "botapi"
scraped['firstseen'] = firstseen
if (userexist == False):
mycollection.insert_one(scraped)
def find_mode(userid):
mycollection = db[collection['user']]
if userid < 0:
mycollection = db[collection['group']]
cursor = mycollection.find({'userid': userid})
mode = "botapi"
for i in cursor:
mode = i['mode']
return mode
def set_mode(userid, mode):
mycollection = db[collection['user']]
if userid < 0:
mycollection = db[collection['group']]
filter = {"userid": userid}
values = {"$set": {"mode": mode}}
mycollection.update_one(filter, values)
def user_exist(chatid, chattype):
mycollection = db[collection['user']]
if chattype == 'group' or chattype == "supergroup":
mycollection = db[collection['group']]
result = mycollection.find_one({'userid': chatid})
try:
result['userid']
userexist = True
except:
userexist = False
return userexist