2 |
const path = require("path"); |
const path = require("path"); |
3 |
const MessageEmbed = require("./MessageEmbed"); |
const MessageEmbed = require("./MessageEmbed"); |
4 |
const SnippetManager = require("./SnippetManager"); |
const SnippetManager = require("./SnippetManager"); |
5 |
|
const Shield = require("./Shield"); |
6 |
const { escapeRegex } = require("./util"); |
const { escapeRegex } = require("./util"); |
7 |
|
|
8 |
class CommandManager { |
class CommandManager { |
9 |
constructor(cmdDir) { |
constructor(cmdDir, loadCommands = true, loadSnippets = true) { |
10 |
this.msg = null; |
this.msg = null; |
11 |
this.commandsDirectory = cmdDir; |
this.commandsDirectory = cmdDir; |
12 |
this.commands = []; |
this.commands = []; |
15 |
this.args = []; |
this.args = []; |
16 |
this.options = []; |
this.options = []; |
17 |
this.normalArgs = []; |
this.normalArgs = []; |
18 |
this.loadCommands(); |
|
19 |
this.snippetManager = new SnippetManager(); |
if (loadCommands) |
20 |
|
this.loadCommands(); |
21 |
|
|
22 |
|
if (loadSnippets) |
23 |
|
this.snippetManager = new SnippetManager(); |
24 |
|
|
25 |
|
this.shield = new Shield(); |
26 |
} |
} |
27 |
|
|
28 |
setMessage(msg) { |
setMessage(msg) { |
29 |
this.msg = msg; |
this.msg = msg; |
30 |
this.argv = msg.content.split(/ +/g); |
this.argv = msg.content.split(/ +/g); |
31 |
this.args = this.argv; |
this.args = this.argv; |
32 |
this.commandName = this.args.shift().trim().replace(new RegExp(`^${escapeRegex(app.config.get('prefix'))}`), ""); |
this.commandName = this.args.shift().trim().replace(new RegExp(`^${escapeRegex(app.config.props[msg.guild.id].prefix)}`), ""); |
33 |
} |
} |
34 |
|
|
35 |
loadCommands() { |
loadCommands() { |
51 |
} |
} |
52 |
|
|
53 |
snippet() { |
snippet() { |
54 |
return this.snippetManager.find(this.commandName); |
return this.snippetManager.find(this.msg.guild.id, this.commandName); |
55 |
} |
} |
56 |
|
|
57 |
hasValid() { |
hasValid() { |
59 |
} |
} |
60 |
|
|
61 |
valid() { |
valid() { |
62 |
return this.msg.content.startsWith(app.config.get('prefix')); |
return this.msg.content.startsWith(app.config.props[this.msg.guild.id].prefix); |
63 |
} |
} |
64 |
|
|
65 |
async notFound() { |
async notFound() { |
74 |
} |
} |
75 |
} |
} |
76 |
|
|
77 |
|
async notAllowed() { |
78 |
|
if (app.config.get('warn_notallowed')) { |
79 |
|
await app.msg.reply({ |
80 |
|
embeds: [ |
81 |
|
new MessageEmbed() |
82 |
|
.setColor('#f14a60') |
83 |
|
.setDescription(`:x: You don't have permission to run this command.`) |
84 |
|
] |
85 |
|
}); |
86 |
|
} |
87 |
|
} |
88 |
|
|
89 |
async exec() { |
async exec() { |
90 |
let cmd = this.commands[this.commandName]; |
let cmd = this.commands[this.commandName]; |
91 |
|
|
96 |
|
|
97 |
return await cmd.handle(this.msg, this); |
return await cmd.handle(this.msg, this); |
98 |
} |
} |
99 |
|
|
100 |
|
verify() { |
101 |
|
return this.shield.verify(this.msg, this); |
102 |
|
} |
103 |
} |
} |
104 |
|
|
105 |
module.exports = CommandManager; |
module.exports = CommandManager; |