-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_manager.py
73 lines (58 loc) · 2.01 KB
/
file_manager.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
import configparser
import os
import json
import glob
from pathlib import Path
script_dir = ''
def prefix_loc():
return script_dir + '/prefixes.json'
def config_loc():
return script_dir + '/config.ini'
def owners_loc():
return script_dir + '/owners.json'
def request_dir():
return script_dir + '/requests/'
def logs_dir():
return script_dir + '/logs/'
def make_config(path):
config = configparser.ConfigParser()
if not os.path.exists(path):
config['discord'] = {'token': '', 'default_prefix': '!'}
config['python'] = {'generate_logs': True, 'enable_curses': False, 'enable_eval': False}
config.write(open(path, 'w'))
print('Config generated. Please edit it with your token.')
quit()
def make_prefixes(path):
if not os.path.exists(path):
make_json(path, {})
def make_owners(path, bot):
if not os.path.exists(path):
ids = {"DISCORD_IDS": []}
discrim = str(bot.appinfo.owner).replace(str(bot.appinfo.owner.name) + "#", '')
ids["DISCORD_IDS"].append({"name": bot.appinfo.owner.name, "discrim": discrim, "id": bot.appinfo.owner.id})
make_json(path, ids)
def make_dir(path):
if not os.path.exists(path):
os.mkdir(path)
def make_json(path, data):
with open(path, 'w') as w:
json.dump(data, w, indent=4)
def delete_json(path, rq):
for file in glob.glob(path + '*' + str(rq) + '.json'):
if os.path.exists(file):
os.remove(file)
def delete_contents(path):
for file in glob.glob(path + '*'):
os.remove(file)
def get_json(key, path):
return [file for file in glob.glob(path + str(key) + '*.json')]
def get_json_userids(path):
"""very specific use but no better place to put it"""
arr = []
for file in glob.glob(path + '*.json'):
file_str = Path(file).stem
userid = file_str[0 : file_str.index('_')]
if int(userid) not in arr:
arr.append(int(userid))
return arr
# consider moving other writing operations here at the end of dev