This repository has been archived by the owner on Nov 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommand.js
186 lines (169 loc) · 5.9 KB
/
command.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
const discordjs = require('discord.js');
const consts = require('./const');
/**
* Abstract Class Command for creating command classes.
* @abstract
* @class Command
*/
class Command {
///// PUBLIC ABSTRACT /////
/**
* Create instance of Command.
*/
constructor() {
this.help;
this.description;
this.exec;
if (!Array.isArray(this.invokes)) {
throw Error('Command invokes() needs to return an array, also if there is only one invoke!');
}
if (this.permission < 0) {
throw Error('Permission level must be larger then or even as 0');
}
}
/**
* Returns invoke of the command.
* First invoke will be displayed in help command and be interpreted
* as main invoke.
* @public
* @abstract
* @returns {string[]} command invokes
*/
get invokes() {
throw Error('get invoke() must be implemented!');
}
/**
* Returns a short description text of the command.
* @public
* @abstract
* @returns {string} description
*/
get description() {
throw Error('get description() must be implemented!');
}
/**
* Returns a detailed help text how to use this command.
* @public
* @abstract
* @returns {string} help text
*/
get help() {
throw Error('get help() must be implemented!');
}
/**
* Returns the minimum permission level required for this command
* as a number larger then or even as 0.
* @public
* @abstract
* @returns {number} permission level
*/
get permission() {
return 0;
}
/**
* Function which will be executed on command execution.
* This function will get passed an object with following properties:
* - channel: TextChannel where command was sent to
* - member: Member object of sender of the command
* - guild: Guild where the command was sent to ('undefined' if command was send to DM channel)
* - message: Message object of the command message
* - args: The arguments passed to the command as string array
* - cmdhandler: Commandhandler instance
* @public
* @abstract
* @param {Object} commandArguments
* @param {Object} commandArguments.channel TextChannel where command was sent to
* @param {Object} [commandArguments.member] Member object of sender of the command
* @param {Object} [commandArguments.guild] Guild where the command was sent to ('undefined' if command was send to DM channel)
* @param {Object} commandArguments.message Message object of the command message
* @param {Object} commandArguments.args The arguments passed to the command as string array
* @param {Object} commandArguments.cmdhandler CommandHander instance
*/
exec(commandArguments) {
throw Error('exec() must be implemented!');
}
/**
* Function which will be executed when the command failed.
* This function will get passed an error object and the commandArguments
* which were passed to the command before.
* @public
* @abstract
* @param {error} error
* @param {Object} commandArguments [→ see exec]{@link Command#exec}
* @param {Object} commandArguments
* @param {Object} commandArguments.channel TextChannel where command was sent to
* @param {Object} [commandArguments.member] Member object of sender of the command
* @param {Object} [commandArguments.guild] Guild where the command was sent to ('undefined' if command was send to DM channel)
* @param {Object} commandArguments.message Message object of the command message
* @param {Object} commandArguments.args The arguments passed to the command as string array+
* @param {Object} commandArguments.cmdhandler CommandHander instance
*/
error(error, commandArguments) {
if (commandArguments && commandArguments.channel) {
let emb = new discordjs.RichEmbed()
.setColor(consts.COLORS.ERROR)
.setTitle('Command Error')
.setDescription('```\n' + error + '\n```');
return commandArguments.channel.send('', emb).catch((err) => {
throw err;
});
}
}
///// PUBLIC /////
/**
* Returns the main invoke which will be used in help message,
* for example. This is the fist invoke of the invokes list.
* @public
* @returns {string} mainInvoke
*/
get mainInvoke() {
return this.invokes[0];
}
/**
* Returns group of command, if set.
* @public
* @returns {string} group
*/
get group() {
return this._group;
}
/**
* Sends an assembled help message embed into a discord
* chat.
* @param {Object} channel discord.js channel object
*/
sendHelp(channel) {
let emb = new discordjs.RichEmbed()
.setColor(consts.COLORS.DEFAULT)
.setTitle(this.mainInvoke + ' - Command Help')
.addField('Description', this.description.length > 0 ? this.description : 'no description set')
.addField('Help', this.help.length > 0 ? this.help : '*no help set*')
.addField('Aliases', this.invokes.map((i) => `- \`${i}\``).join('\n'))
.addField('Group', this.group, true)
.addField('Permission', this.permission, true);
return channel.send('', emb);
}
///// PRIVATES /////
/**
* Set discord client instance.
* @private
*/
_setClient(client) {
if (!client) {
throw Error('discord.js client instance is undefined');
}
this.client = client;
return this;
}
/**
* Set group of command.
* @private
*/
_setGroup(group) {
if (group) {
this._group = group;
}
return this;
}
};
module.exports = Command;