-
Notifications
You must be signed in to change notification settings - Fork 17
/
GuildConfig.js
347 lines (312 loc) · 11.6 KB
/
GuildConfig.js
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/**
* @typedef {import('../../AxonClient').default} AxonClient
* @typedef {import('../Module').default} Module
* @typedef {import('../Command/Command').default} Command
* @typedef {import('../Event/Listener').default} Listener
* @typedef {{
* guildID?: String, prefixes?: Array<String>, createdAt?: Date, updatedAt?: Date, modules?: Array<String>
* commands: Array<String>, eventListeners: Array<String>, ignoredUsers: Array<String>, ignoredRoles: Array<String>,
* ignoredChannels: Array<String>, modOnly: Boolean, modRoles: Array<String>, modUsers: Array<String>
* }} GConfig
*/
/**
* Default GuildConfig data structure used in AxonCore.
* This class can be extended and changed as you want.
* All methods flagged with "is used internally" can be overridden but need to keep the same name.
*
* @author KhaaZ
*
* @class GuildConfig
*
* @prop {String} guildID
* @prop {Array<String>} prefixes
* @prop {Array<String>} modules - Guild disabled modules: Array of modules labels
* @prop {Array<String>} commands - Guild disabled commands: Array of commands labels
* @prop {Array<String>} listeners - Guild disabled listeners: Array of listeners labels
* @prop {Date} createdAt - Creation of the guild Config
* @prop {Date} updatedAt - Last update of the guild Config
* @prop {Array<String>} ignoredUsers - Users that cannot use commands in this guild: Users ids
* @prop {Array<String>} ignoredRoles - Roles that cannot use commands in this guild: Roles ids
* @prop {Array<String>} ignoredChannels - Channels where commands cannot be used in this guild: Channels ids
* @prop {Boolean} modOnly - Whether the guild accept commands from only mods+ or everyone
* @prop {Array<String>} modRoles - Roles able to execute mod commands: Roles ids
* @prop {Array<String>} modUsers - Users able to execute mod commands: Users ids
*/
class GuildConfig {
/**
* Creates an instance of GuildConfig.
*
* @param {AxonClient} axon
* @param {GConfig} values - DB values for the current guild
* @memberof GuildConfig
*/
constructor(axon, values) {
this._axon = axon;
this.guildID = values.guildID || '';
this.prefixes = values.prefixes || [];
this.createdAt = values.createdAt || new Date();
this.updatedAt = values.updatedAt || new Date();
/* Disabled modules / commands / events */
this.modules = values.modules || [];
this.commands = values.commands || [];
this.listeners = values.eventListeners || [];
this.ignoredUsers = values.ignoredUsers || [];
this.ignoredRoles = values.ignoredRoles || [];
this.ignoredChannels = values.ignoredChannels || [];
this.modOnly = values.modOnly || false;
this.modRoles = values.modRoles || [];
this.modUsers = values.modUsers || [];
}
//
// **** CHECKER - used IN the framework **** //
/* Can be overridden/changed but need to exists / keep the same name */
//
/**
* Get guild prefixes for this guild.
*
* @returns {Array<String>}
* @memberof GuildConfig
*/
getPrefixes() {
return (this.prefixes.length > 0) ? this.prefixes : (this._axon.settings.prefixes || [] );
}
/**
* Check if the user/role/channel is ignored on the specified guild.
* *used internally*
*
* @param {Message} msg
* @returns {Boolean} True if either one of the three is ignored
* @memberof GuildConfig
*/
isIgnored(msg) {
return this.isUserIgnored(this._axon.library.message.getAuthorID(msg) )
|| this.isRoleIgnored(this._axon.library.message.getMember(msg) )
|| this.isChannelIgnored(this._axon.library.message.getChannelID(msg) );
}
/**
* Check if the user/role/channel is ignored on the specified guild.
*
* @param {String} userID
* @returns {Boolean} True if the user is one of the ignored users
*
* @memberof GuildConfig
*/
isUserIgnored(userID) {
return this.ignoredUsers.includes(userID); // User is ignored
}
/**
* Check if the user/role/channel is ignored on the specified guild.
*
* @param {Member} member
* @returns {Boolean} True if the member has one of the ignored roles
* @memberof GuildConfig
*/
isRoleIgnored(member) {
const roles = this._axon.library.member.getRoles(member);
if (!roles || !(Array.isArray(roles) ) ) {
return false;
}
return !!this.ignoredRoles.find(r => roles.includes(r) ); // Role is ignored
}
/**
* Check if the user/role/channel is ignored on the specified guild.
*
* @param {String} channelID
* @returns {Boolean} True if the channel is one of the ignored channels
* @memberof GuildConfig
*/
isChannelIgnored(channelID) {
return this.ignoredChannels.includes(channelID); // Channel is ignored
}
/**
* Check if the module is disabled on the specified guild.
* *used internally*
*
* @param {Module} module - The module object
* @returns {Boolean} Whether the module is disabled or not
* @memberof GuildConfig
*/
isModuleDisabled(module) {
return this.modules.includes(module.label);
}
/**
* Check if the command is disabled on the specified guild.
* *used internally*
*
* @param {Command} command - The command object
* @returns {Boolean} Whether the command is disabled or not
* @memberof GuildConfig
*/
isCommandDisabled(command) {
return this.commands.includes(command.label);
}
/**
* Check if the listener is disabled on the specified guild.
* *used internally*
*
* @param {Listener} listener - The listener object
* @returns {Boolean} Whether the listener is disabled or not
* @memberof GuildConfig
*/
isListenerDisabled(listener) {
return this.listeners.includes(listener.label);
}
/**
* Whether the guild is set up to mod only or not.
* *used internally*
*
* @returns {Boolean}
* @memberof GuildConfig
*/
isModOnly() {
return this.modOnly;
}
/**
* Whether the role ID is in the guild mod roles.
* *used internally*
*
* @param {String} roleID
* @returns {Boolean}
* @memberof GuildConfig
*/
isModRole(roleID) {
return this.modRoles.includes(roleID);
}
/**
* Whether the user ID is in the guild mod users.
* *used internally*
*
* @param {String} userID
* @returns {Boolean}
* @memberof GuildConfig
*/
isModUser(userID) {
return this.modUsers.includes(userID);
}
//
// **** UPDATER - never used directly IN the framework **** //
/* Easily overridable. Can be changed as will (even methods name etc) */
//
/**
* Update the guild config in the cache and DB.
* *not used internally*
*
* @param {GConfig} guildConfig - Guild schema Object
* @returns {Promise<GuildConfig>} Updated guildSchema
* @memberof GuildConfig
*/
async update(guildConfig) {
if (guildConfig.guildID !== this.guildID) {
return Promise.resolve(null);
}
const update = {};
for (const key in guildConfig) {
this[key] = guildConfig[key];
if (!key.startsWith('_') ) {
update[key] = guildConfig[key];
}
}
this.updatedAt = new Date();
update.updatedAt = new Date();
const newConf = await this._axon.DBProvider.saveGuild(this.guildID, update);
return newConf ? this : null;
}
/**
* Register prefixes for this guild.
* *not used internally*
*
* @param {Array<String>} prefixArr - The array of prefix
* @returns {Promise<GuildConfig|null>} Updated guildConfig / error
* @memberof GuildConfig
*/
updatePrefixes(prefixArr) {
this.prefixes = prefixArr;
return this._req('prefixes', this.prefixes);
}
/**
* Updates the state of a module.
* true = disable the module, false = enable the module
* *not used internally*
*
* @param {String} label - The module label
* @param {Boolean} [boolean=true] - Whether to enable (true) the module or disable (false) it.
* @returns {Promise<GuildConfig|null>} Updated guildConfig / Error
* @memberof GuildConfig
*/
updateStateModule(label, boolean = true) {
boolean
? this.modules.includes(label) && (this.modules = this.modules.filter(m => m !== label) )
: !this.modules.includes(label) && this.modules.push(label);
return this._req('modules', this.modules);
}
/**
* Updates the state of a command.
* true = disable the command, false = enable the command.
* *not used internally*
*
* @param {String} label - The command label
* @param {Boolean} [boolean=true] - Whether to enable (true) the command or disable (false) it.
* @returns {Promise<GuildConfig|null>} Updated guildConfig / Error
* @memberof GuildConfig
*/
updateStateCommand(label, boolean = true) {
boolean
? this.commands.includes(label) && (this.commands = this.commands.filter(c => c !== label) )
: !this.commands.includes(label) && this.commands.push(label);
return this._req('commands', this.commands);
}
/**
* Updates the state of a listener.
* true = disable the listener, false = enable the listener.
* *not used internally*
*
* @param {String} label - The listener label
* @param {Boolean} [boolean=true] - Whether to enable (true) the listener or disable (false) it.
* @returns {Promise<GuildConfig|null>} Updated guildConfig / Error
* @memberof GuildConfig
*/
updateStateListener(label, boolean = true) {
boolean
? this.listeners.includes(label) && (this.listeners = this.listeners.filter(e => e !== label) )
: !this.listeners.includes(label) && this.listeners.push(label);
return this._req('eventListeners', this.listeners);
}
/**
* Updates the state of a mod role.
* true = add the role, false = remove the role.
* *not used internally*
*
* @param {String} roleID - The role ID
* @param {Boolean} [boolean=true] - Whether to add (true) the role or remove (false) it.
* @returns {Promise<GuildConfig|null>} Updated guildConfig / Error
* @memberof GuildConfig
*/
updateStateModRole(roleID, boolean = true) {
boolean
? !this.modRoles.includes(roleID) && this.modRoles.push(roleID)
: this.modRoles.includes(roleID) && (this.modRoles = this.modRoles.filter(c => c !== roleID) );
return this._req('modRoles', this.modRoles);
}
/**
* Updates the state of a mod user.
* true = add the user, false = remove the user.
* *not used internally*
*
* @param {String} userID - The user ID
* @param {Boolean} [boolean=true] - Whether to add (true) the user or remove (false) it.
* @returns {Promise<GuildConfig|null>} Updated guildConfig / Error
* @memberof GuildConfig
*/
updateStateModUser(userID, boolean = true) {
boolean
? !this.modUsers.includes(userID) && this.modUsers.push(userID)
: this.modUsers.includes(userID) && (this.modUsers = this.modUsers.filter(c => c !== userID) );
return this._req('modUsers', this.modUsers);
}
async _req(key, value) {
this.updatedAt = new Date();
const newConf = await this._axon.DBProvider.updateGuild(key, this.guildID, value);
return newConf ? this : null;
}
}
export default GuildConfig;