-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpixie_function.py
More file actions
167 lines (153 loc) · 5.44 KB
/
pixie_function.py
File metadata and controls
167 lines (153 loc) · 5.44 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
import datetime
import json
import re
import subprocess
import sys
import traceback
import os
root_dir = os.path.dirname(os.path.abspath(__file__))
def isLoaded(module, id):
botListModules = readData('main')
if botListModules[id][module]["last"] == "enabled":
return True
else:
return False
def isEnabled(module, id):
serverslist = readData('server', id, module)
if serverslist[module]["last"] == "enabled":
return True
else:
return False
def checkModule(module):
res = subprocess.run(['python3', '-m', 'py_compile', root_dir + '/modules/' + module + '.py'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
return res
def checkModuleConfig(module, id):
defaultlistmodule = getDefault()
serverlistmodule = readData('server', id)
if defaultlistmodule[module]['config'] == "None":
return True
else:
for configKey in serverlistmodule[module]['config']:
if serverlistmodule[module]['config'][configKey]['value'] == "None":
return False
return True
def readData(file, id = None, module = None):
if file == 'main':
with open(root_dir + '/data/modules.json') as e:
data = json.load(e)
elif file == 'list':
with open(root_dir + '/data/serverslist.json') as e:
data = json.load(e)
elif file == 'locales':
with open(root_dir + '/data/locales.json') as e:
data = json.load(e)
for lang in data:
with open(root_dir + '/data/locales/{}.json'.format(lang)) as e:
data[lang] = json.load(e)
elif file == 'server':
try:
with open(root_dir + '/data/servers/{}.json'.format(id)) as e:
data = json.load(e)
except:
with open(root_dir + '/data/default.json') as e:
data = json.load(e)
saveData('server', data, id)
else:
if module == None:
listmodules = readData('main')
for module in listmodules:
try:
data[module]
except KeyError:
defaultData = getDefault()
data[module] = defaultData[module]
else:
try:
data[module]
except KeyError:
defaultData = getDefault()
data[module] = defaultData[module]
saveData('server', data, id)
return data
def saveData(file, data, id = None):
if file == 'main':
with open(root_dir + '/data/modules.json', 'w') as e:
json.dump(data, e)
elif file == 'server':
if id == None:
return
with open(root_dir + '/data/servers/{}.json'.format(id), 'w') as e:
json.dump(data, e)
elif file == 'default':
with open(root_dir + '/data/default.json', 'w') as e:
json.dump(data, e)
def getDefault():
with open(root_dir + '/data/default.json') as e:
data = json.load(e)
return data
def getValidAction(module, configKey):
data = getDefault()
#actions = ''
if (data[module]['config'][configKey]['type'] == 'chantag' or data[module]['config'][configKey]['type'] == 'usertag' or data[module]['config'][configKey]['type'] == 'str'):
actions = '<set/clear>'
elif (data[module]['config'][configKey]['type'] == 'chantaglist' or data[module]['config'][configKey]['type'] == 'usertaglist'):
actions = '<add/remove/clear>'
return actions
def validateModuleName(moduleName):
listmodule = readData('main')
try:
listmodule[moduleName]
except KeyError:
return False
return True
def validateConfigKey(id, module, configKey = None):
defaultlistmodule = getDefault()
#serverlistmodule = readData('server', id)
try:
defaultlistmodule[module]['config'][configKey]
except KeyError:
return False
return True
def validateConfigKeyType(id, type, module, configKey, value):
defaultlistmodule = getDefault()
#serverlistmodule = readData('server', id)
try:
if type == 'bool':
bool(value)
elif type == 'str':
str(value)
elif type == 'chantag':
if re.search('^<#([0-9])+>$', value):
value = int(''.join(filter(str.isdigit, value)))
int(value)
elif type == 'usertag':
if re.search('^<@([0-9])+>$', value):
value = int(''.join(filter(str.isdigit, value)))
int(value)
elif type == 'int':
int(value)
except:
return False
return True
def validateValue(module, configKey, left):
defaultlistmodule = getDefault()
try:
validate = defaultlistmodule[module]['config'][configKey]['validate']
except:#no validation info
return True
else:
fighters = validate.split("/")
for right in fighters:
#Fight !
if left == right:
return True #left win !
return False #right win !
def removeConfig(action, id, module, configKey, value = None):
if action == 'clear':
defaultlistmodule = getDefault()
serverlistmodule = readData('server', id, module)
serverlistmodule[module]['config'][configKey]['value'] = defaultlistmodule[module]['config'][configKey]['value']
saveData('server', serverlistmodule, id)
elif action == 'remove':
return