-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathCommandOptions.js
221 lines (203 loc) · 7.75 KB
/
CommandOptions.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
import Command from './Command';
import Module from '../Module';
import AxonError from '../../Errors/AxonError';
/**
* @typedef {import('../../Langs/MessageManager').default} MessageManager
* @typedef {import('../Models/GuildConfig').default} GuildConfig
*/
/**
* CommandOptions.
* Holds options for a command and all necessary checkers.
*
* @author KhaaZ
*
* @class CommandOptions
*
* @prop {(channel: Channel, member: Member) => String | String} [invalidPermissionMessage=null] - What the invalid permission message should be
* @prop {Boolean} [sendPermissionMessage=false] - Whether to trigger an error message on invalid permission (bot / user / custom etc)
* @prop {Number} [invalidPermissionMessageTimeout=9000] - What the invalid permission message deletion timeout should be
* @prop {Number} [argsMin=0] - Minimum arguments required to execute the command
* @prop {String} [invalidUsageMessage=null] - What the invalid usage message should be
* @prop {Boolean} [sendUsageMessage=true] - Whether to trigger the help command on invalid usage (not enough arguments)
* @prop {Boolean} [deleteCommand=false] - Whether to delete the command input after trigger
* @prop {Boolean} [guildOnly=true] - Whether to allow executing this command outside of guilds
* @prop {Boolean} [hidden=false] - Whether to hide this command from help command (general / subcommands)
* @prop {Number} [cooldown=3000] - Cooldown between each usage of this command for a specific user (in ms)
*/
class CommandOptions {
/**
* Creates an instance of CommandOptions.
*
* @param {Command} command - The base command
* @param {Object} [override={}] - - The specific options for this command (format - CommandOptions)
* @param {(channel: Channel, member: Member) => String | String} override.invalidPermissionMessage - What the invalid permission message should be
* @param {Boolean} override.sendPermissionMessage - Whether to trigger an error message on invalid permission (bot / user / custom etc)
* @param {Number} override.invalidPermissionMessageTimeout - What the invalid permission message deletion timeout should be
* @param {Number} override.argsMin - Minimum arguments required to execute the command
* @param {String} override.invalidUsageMessage - What the invalid usage message should be
* @param {Boolean} override.sendUsageMessage - Whether to trigger the help command on invalid usage (not enough arguments)
* @param {Boolean} override.deleteCommand - Whether to delete the command input after trigger
* @param {Boolean} override.guildOnly - Whether to allow executing this command outside of guilds
* @param {Boolean} override.hidden - Whether to hide this command from help command (general / subcommands)
* @param {Number} override.cooldown - Cooldown between each usage of this command for a specific user (in ms)
* @param {Boolean} [useModuleDefault=false] - Whether to use or not the module's base options before applying override permissions
* @memberof CommandOptions
*/
constructor(command, override = {}, useModuleDefault = false) {
let isModule = false;
if (!(command instanceof Command) ) {
if (!(command instanceof Module) ) {
throw new AxonError('First argument needs to be the Command.', 'CommandOptions');
} else {
isModule = true;
}
}
this._command = command;
let base;
if (useModuleDefault && !isModule && command.module.options) {
base = Object.assign( {}, command.module.options);
for (const key in override) {
base[key] = override[key];
}
} else {
base = override;
}
// invalid permissions
if (typeof base.invalidPermissionMessage === 'string') {
/**
* @type {() => String}
*/
this.invalidPermissionMessage = () => base.invalidPermissionMessage;
} else if (typeof base.invalidPermissionMessage === 'function') {
this.invalidPermissionMessage = base.invalidPermissionMessage;
} else {
this.invalidPermissionMessage = null;
}
/**
* @type {Boolean}
*/
this.sendPermissionMessage = !!base.sendPermissionMessage;
/**
* @type {Number}
*/
this.invalidPermissionMessageTimeout = base.invalidPermissionMessageTimeout !== undefined ? base.invalidPermissionMessageTimeout : 9000; // eslint-disable-line no-magic-numbers
/**
* @type {Number}
*/
this.argsMin = base.argsMin || 0;
/**
* @type {String}
*/
this.invalidUsageMessage = base.invalidUsageMessage !== undefined ? base.invalidUsageMessage : null;
/**
* @type {Boolean}
*/
this.sendUsageMessage = base.sendUsageMessage !== false;
/**
* @type {Boolean}
*/
this.deleteCommand = !!base.deleteCommand;
/**
* @type {Boolean}
*/
this.guildOnly = base.guildOnly !== false;
/**
* @type {Boolean}
*/
this.hidden = !!base.hidden;
/**
* @type {Number}
*/
this.cooldown = (base.cooldown === 0 || base.cooldown === null) ? 0 : (base.cooldown || 3000); // eslint-disable-line no-magic-numbers
}
/**
* Returns the MessageManager instance
*
* @readonly
* @type {MessageManager}
* @memberof CommandOptions
*/
get l() {
return this._command.l;
}
/**
* Whether the command is guild only or not
*
* @returns {Boolean}
* @memberof CommandOptions
*/
isGuildOnly() {
return this.guildOnly;
}
/**
* Whether the command is hidden or not
*
* @returns {Boolean}
* @memberof CommandOptions
*/
isHidden() {
return this.hidden;
}
/**
* Whether args for this command are correct or not (enough args).
*
* @param {Array<String>} args
* @returns {Boolean}
* @memberof CommandOptions
*/
hasCorrectArgs(args) {
return (args.length >= this.argsMin);
}
/**
* Whether we should send an invalid usage message or not (help command)
*
* @param {Array<String>} args
* @returns {Boolean}
* @memberof CommandOptions
*/
shouldSendInvalidUsageMessage(args) {
return (args.length < this.argsMin && this.sendUsageMessage && !this.hidden);
}
/**
* Whether we should send the invalid permission message or not
*
* @param {GuildConfig} guildConfig
* @returns {Boolean}
* @memberof CommandOptions
*/
shouldSendInvalidPermissionMessage(guildConfig) {
return (!guildConfig.isModOnly() && this.sendPermissionMessage);
}
/**
* Whether we should delete the command or not
*
* @returns {Boolean}
* @memberof CommandOptions
*/
shouldDeleteCommand() {
return this.deleteCommand;
}
/**
* Get the invalid permission message
*
* @param {Channel} channel - The guild channel
* @param {Member} member - The guild member
* @param {String} permission - The missing permission
* @returns {String}
*/
getInvalidPermissionMessage(channel, member, permission) {
const message = this.invalidPermissionMessage
? this.invalidPermissionMessage(channel, member)
: this.l.getMessage('ERR_CALLER_PERM');
return this.l.parser.parse(message, { permissions: permission || 'Custom' } );
}
/**
* Get the invalid usage message
*
* @returns {String}
*/
getInvalidUsageMessage() {
return this.invalidUsageMessage;
}
}
export default CommandOptions;