-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAxonConfig.js
132 lines (118 loc) · 3.94 KB
/
AxonConfig.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
/**
* @typedef {import('../../AxonClient').default} AxonClient
* @typedef {{
* id?: String, prefix?: String, createdAt?: Date, updatedAt?: Date, bannedUsers?: Array<String>, bannedGuilds?: Array<String>
* }} AConfig
*/
/**
* Default AxonConfig 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 AxonConfig
*
* @prop {String} id
* @prop {String} prefix
*
* @prop {Date} createdAt
* @prop {Date} updatedAt
* @prop {Array<String>} [bannedUsers=[]] - Array of users that can't use bot commands
* @prop {Array<String>} [bannedGuilds=[]] - Array of guilds where bot commands cannot be used
*/
class AxonConfig {
/**
* Creates an instance of AxonConfig.
*
* @param {AxonClient} axon
* @param {AConfig} values DB values for the current Guild
*
* @memberof AxonConfig
*/
constructor(axon, values) {
this._axon = axon;
this.id = values.id || '1';
this.prefix = values.prefix || axon.settings.prefixes[0];
this.createdAt = values.createdAt || new Date();
this.updatedAt = values.updatedAt || new Date();
/**
* @type {Array<String>}
*/
this.bannedUsers = values.bannedUsers || [];
/**
* @type {Array<String>}
*/
this.bannedGuilds = values.bannedGuilds || [];
}
// **** CHECKERS (used IN the framework) **** //
/* Can be overridden/changed but need to exists / keep the same name */
//
/**
* Whether the user is blacklisted or not
* *used internally*
*
* @param {String} userID
* @returns {Boolean}
* @memberof AxonConfig
*/
isBlacklistedUser(userID) {
return this.bannedUsers.includes(userID);
}
/**
* Whether the guild is blacklisted or not
* *used internally*
*
* @param {String} guildID
* @returns {Boolean}
* @memberof AxonConfig
*/
isBlacklistedGuild(guildID) {
return this.bannedGuilds.includes(guildID);
}
//
// **** UPDATER - never used directly IN the framework **** //
/* Easily overridable. Can be changed as will (even methods name etc) */
//
/**
* Updates the state of a blacklisted user.
* true = add the user, false = remove the user.
* *not used internally*
*
* @param {String} userID - The guild ID
* @param {Boolean} [boolean=true] - Whether to add (true) the user or remove (false) it.
* @returns {Promise<AxonConfig|null>} Updated axonConfig / Error
* @memberof AxonConfig
*/
async updateBlacklistUser(userID, boolean = true) {
if (boolean) {
!this.isBlacklistedUser(userID) && this.bannedUsers.push(userID);
} else {
this.isBlacklistedUser(userID) && (this.bannedUsers = this.bannedUsers.filter(g => g === userID) );
}
return this._req('bannedGuilds', this.bannedUsers);
}
/**
* Updates the state of a blacklisted guild.
* true = add the guild, false = remove the guild.
* *not used internally*
*
* @param {String} guildID - The guild ID
* @param {Boolean} [boolean=true] - Whether to add (true) the guild or remove (false) it.
* @returns {Promise<AxonConfig|null>} Updated axonConfig / Error
* @memberof AxonConfig
*/
async updateBlacklistGuild(guildID, boolean = true) {
if (boolean) {
!this.isBlacklistedGuild(guildID) && this.bannedGuilds.push(guildID);
} else {
this.isBlacklistedGuild(guildID) && (this.bannedGuilds = this.bannedGuilds.filter(g => g === guildID) );
}
return this._req('bannedGuilds', this.bannedGuilds);
}
async _req(key, value) {
const newConf = await this._axon.DBProvider.updateAxon(key, value);
return newConf ? this : null;
}
}
export default AxonConfig;