forked from Khaaz/AxonCore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandOptions.js
163 lines (147 loc) · 5.44 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
import Command from './Command';
import Module from '../Module';
import AxonError from '../../Errors/AxonError';
/**
* CommandOptions.
* Holds options for a command and all necessary checkers.
*
* @author KhaaZ
*
* @class CommandOptions
*
* @prop {Number} [argsMin=0] - Minimum arguments required to execute the command
* @prop {Boolean} [invalidUsageMessage=true] - Whether to trigger the help command on invalid usage (not enough arguments)
* @prop {Boolean} [sendPermissionMessage=false] - Whether to trigger an error message on invalid permission (bot / user / custom etc)
* @prop {Function | String} [invalidPermissionMessage=null] - What the invalid permission message should be
* @prop {Number} [invalidPermissionMessageTimeout=9000] - What the invalid permission message deletion timeout should be
* @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 betweeneach 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 {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;
}
if (typeof base.invalidPermissionMessage === 'string') {
// eslint-disable-next-line no-unused-vars
this.invalidPermissionMessage = (channel, member) => base.invalidPermissionMessage;
} else if (typeof base.invalidPermissionMessage === 'function') {
this.invalidPermissionMessage = base.invalidPermissionMessage;
} else {
this.invalidPermissionMessage = null;
}
this.argsMin = base.argsMin || 0;
this.invalidUsageMessage = base.invalidUsageMessage !== false;
// invalid permissions
this.invalidPermissionMessage = !!base.invalidPermissionMessage;
this.sendPermissionMessage = !!base.sendPermissionMessage;
this.invalidPermissionMessageTimeout = base.invalidPermissionMessageTimeout !== undefined ? base.invalidPermissionMessageTimeout : 9000; // eslint-disable-line no-magic-numbers
this.deleteCommand = !!base.deleteCommand;
this.guildOnly = base.guildOnly !== false;
this.hidden = !!base.hidden;
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 we should send an invalid usage message or not (help command)
*
* @param {Array} args
* @returns {Boolean}
*
* @memberof CommandOptions
*/
shouldSendInvalidUsageMessage(args) {
return (args.length < this.argsMin && this.invalidUsage && !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
*
* @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' } );
}
}
export default CommandOptions;