forked from greatbritdan/Mari0-Suggestions-Bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.py
279 lines (257 loc) · 8.76 KB
/
config.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import discord, copy
from discord.utils import get
import discord.ext.commands as CM
import json
with open('./data/values.json') as file:
VALUES = json.load(file)
# MANAGES CONFGERATION #
class ConfigManager():
def __setitem__(self, key, value):
setattr(self, key, value)
def __getitem__(self, key):
return getattr(self, key)
def __init__(self, client:CM.Bot, ctype):
self.client = client
self.type = ctype
self.values = {}
self.valid_values = []
self.default_values = {}
self.desc_values = {}
self.type_values = {}
self.restrict_values = {}
self.stackable_values = {}
list = None
if self.type == "guild":
self.logname = "msb-guild-logs"
list = VALUES
for val in list:
self.valid_values.append(val)
self.default_values[val] = list[val]["default"]
self.desc_values[val] = list[val]["help"]
if list[val]["type"].startswith("[]"):
self.stackable_values[val] = True
self.type_values[val] = list[val]["type"][2:]
else:
self.stackable_values[val] = False
self.type_values[val] = list[val]["type"]
if "restricted" in list[val]:
self.restrict_values[val] = list[val]["restricted"]
else:
self.restrict_values[val] = False
async def ready(self):
await self.values_msgupdate("load")
# load or save the json file
async def values_msgupdate(self, typ):
guild = get(self.client.guilds, id=879063875469860874)
channel = get(guild.channels, name=self.logname)
message = await channel.fetch_message(channel.last_message_id)
if typ == "load":
byte = await message.attachments[0].read()
txt = byte.decode("utf-8")
self.values = json.loads(txt)
elif typ == "save":
values = self.savevalues(self.values)
await message.delete()
with open("temp.json", "w") as f:
json.dump(values, f, indent=4)
await channel.send(file=discord.File("temp.json", "values.json"))
def tonumber(self, val):
try:
return int(val)
except ValueError:
return False
def savevalues(self, values):
valuescopy = copy.deepcopy(values)
for id in valuescopy:
valuesidcopy = copy.deepcopy(values[id])
for name in valuesidcopy:
if values[id][name] == self.default_values[name]:
values[id].pop(name)
if len(values[id]) == 0:
values.pop(id)
return values
def fix_model(self, obj:discord.Object=None, id=None): # if values or group is missing it'll fill out the list
if obj: id = str(obj.id)
if id not in self.values or self.values[id] == None:
self.values[id] = {}
for name in self.default_values:
if name not in self.values[id]:
self.values[id][name] = self.default_values[name]
# fix yucky order
vals = copy.deepcopy(self.values[id])
self.values[id] = {}
for name in self.default_values:
self.values[id][name] = vals[name]
def exists(self, name):
return (name in self.valid_values)
def get_help(self, name):
return self.desc_values[name]
def get_type(self, name):
return self.type_values[name]
def get_stackable(self, name):
return self.stackable_values[name]
def is_restricted(self, name):
return self.restrict_values[name]
async def remove_group(self, obj:discord.Object):
if str(obj.id) in self.values:
self.values.pop(str(obj.id))
await self.values_msgupdate("save")
return True
return False
def get_group(self, obj:discord.Object):
self.fix_model(obj)
return self.values[str(obj.id)]
def get_value(self, obj:discord.Object, name, guild:discord.Guild=None):
self.fix_model(obj)
val = self.values[str(obj.id)][name]
if self.get_stackable(name) and type(val) == list:
result = []
for nval in val:
result.append( self._get_value(name, nval, guild) )
else:
result = self._get_value(name, val, guild)
return result
def _get_value(self, name, val, guild:discord.Guild=None):
typ = self.get_type(name)
if typ == "channel/thread":
channel = get(guild.channels, id=val)
if channel:
return channel
else:
thread = get(guild.threads, id=val)
if thread:
return thread
elif typ == "channel":
return get(guild.channels, id=val)
elif typ == "thread":
return get(guild.threads, id=val)
elif typ == "role":
return get(guild.roles, id=val)
else:
return val
def can_set_value(self, obj:discord.Object, name, val):
return False
string_limit, number_limit, stackable_limit = 250, 5, 10
if self.type == "guild" and obj.member_count >= 40:
string_limit, number_limit, stackable_limit = 1000, 10, 25
vals = False
if self.get_stackable(name):
vals = [v.strip(" ") for v in val.split(",")]
if len(vals) > stackable_limit:
return "Stackable values like `{name}` can't have more than {limit} {type}s.".format(limit=stackable_limit, name=name, type=self.get_type(name))
def single_check(val):
if self.get_type(name) == "string":
if val != True and val != False and len(val) > string_limit:
return "String values like `{name}` can't have more than {limit} letters.".format(limit=string_limit, name=name)
if self.get_type(name) == "number":
if len(str(val)) > number_limit:
return "Number values like `{name}` can't have more than {limit} didgets.".format(limit=number_limit, name=name)
return False
if self.get_stackable(name):
for nval in vals:
err = single_check(nval)
if err: return err
return False
else:
return single_check(val)
async def set_value(self, obj:discord.Object, name, val, guild:discord.Guild=None, noupdate=False):
self.fix_model(obj)
err = self.can_set_value(obj, name, val)
if err:
return False, err
if self.get_stackable(name):
vals = [v.strip(" ") for v in val.split(",")]
result = []
for nval in vals:
result.append( self._set_value(obj, name, nval, guild) )
else:
result = self._set_value(obj, name, val, guild)
self.values[str(obj.id)][name] = result
if not noupdate:
await self.values_msgupdate("save")
return result, False
def _set_value(self, obj:discord.Object, name, val, guild:discord.Guild):
typ = self.get_type(name)
if (typ != "string") and type(val) == str:
val = val.lower()
if (val == "false" or val == "none"):
return False
if typ == "channel/thread":
newval = self.tonumber(val)
channel = get(guild.channels, id=newval) if newval else get(guild.channels, name=val)
if channel:
return channel.id
else:
thread = get(guild.threads, id=newval) if newval else get(guild.threads, name=val)
if thread:
return thread.id
if typ == "channel":
newval = self.tonumber(val)
channel = get(guild.channels, id=newval) if newval else get(guild.channels, name=val)
if channel:
return channel.id
elif typ == "thread":
newval = self.tonumber(val)
thread = get(guild.threads, id=newval) if newval else get(guild.threads, name=val)
if thread:
return thread.id
elif typ == "role":
newval = self.tonumber(val)
role = get(guild.roles, id=newval) if newval else get(guild.roles, name=val)
if role:
return role.id
elif typ == "boolean":
if val == "true":
return True
else:
return False
elif typ == "number":
newval = self.tonumber(val)
if newval:
return newval
else:
return val
async def reset_value(self, obj:discord.Object, name, noupdate=False):
self.fix_model(obj)
self.values[str(obj.id)][name] = self.default_values[name]
if not noupdate:
await self.values_msgupdate("save")
def raw_value(self, name, val):
if self.get_stackable(name) and type(val) == list:
result = []
for nval in val:
result.append(self._raw_value(nval))
return ",".join(result)
else:
return self._raw_value(val)
def _raw_value(self, val):
if isinstance(val, discord.TextChannel) or isinstance(val, discord.Thread) or isinstance(val, discord.Role):
return str(val.id)
return str(val)
def display_value(self, name, val):
if self.get_stackable(name) and type(val) == list:
result = []
for nval in val:
result.append(self._display_value(nval))
return ", ".join(result)
else:
return self._display_value(val)
def _display_value(self, val):
if isinstance(val, discord.TextChannel) or isinstance(val, discord.Thread) or isinstance(val, discord.Role):
return val.mention
return f"`{val}`"
# loop through each user/guild and return their object, using the guild parameter limits it to a spesific guild.
async def loopdata(self, guild:discord.Guild=None):
chomk = []
for id in self.values:
obj = None
if self.type == "guild":
obj = self.client.get_guild(int(id))
elif self.type == "user":
if guild:
obj = get(guild.members, id=int(id))
else:
obj = await self.client.fetch_user(int(id))
if obj:
chomk.append(obj)
return chomk