-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
61 lines (43 loc) · 1.63 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
61
from pymongo import MongoClient
from os import getenv
from utils import is_int
from dotenv import load_dotenv
load_dotenv()
client = MongoClient(getenv('MONGODB_URI'))
def get_db():
return client['bigcharles']
def get_collection():
db = client['bigcharles']
return db['patterns']
def get_total(guild_id):
return get_collection().count_documents({'guild_id': guild_id})
def get_delimiter(guild_id):
config = client['bigcharles']['configs'].find({'guild_id': guild_id})[0]
return config['delimiter']
def set_delimiter(args, guild_id, author_id):
if len(args) != 1:
return
configs = client['bigcharles']['configs']
configs.update_one({'guild_id': guild_id}, {
'$set': {'delimiter': args[0]}}, upsert=True)
def get_patterns(guild_id, **kwargs):
return get_collection().find({'guild_id': guild_id}, **kwargs)
def set_pattern(args, guild_id):
if len(args) < 2:
return
regex = args[0]
response = args[1]
chance = float(args[2]) if len(args) > 2 else 100
get_collection().update_one({'value': regex, 'guild_id': guild_id}, {
'$set': {'response': response, 'chance': chance}}, upsert=True)
def remove_pattern(args, guild_id):
if len(args) < 1:
return
regex = args[0]
patterns = get_collection()
result = patterns.delete_one({'value': regex, 'guild_id': guild_id})
if result.deleted_count == 0 and is_int(regex):
# tries to delete pattern at position <regex>
pattern = patterns.find_one({'guild_id': guild_id}, skip=int(regex)-1)
if pattern:
patterns.delete_one({'_id': pattern['_id']})