/[sudobot]/trunk/src/CommandManager.js
ViewVC logotype

Annotation of /trunk/src/CommandManager.js

Parent Directory Parent Directory | Revision Log Revision Log


Revision 5 - (hide annotations)
Mon Jul 29 17:28:11 2024 UTC (8 months, 1 week ago) by rakin
File MIME type: text/javascript
File size: 2056 byte(s)
Added base commands
1 rakin 5 const fs = require("fs");
2     const path = require("path");
3     const MessageEmbed = require("./MessageEmbed");
4     const SnippetManager = require("./SnippetManager");
5     const { escapeRegex } = require("./util");
6    
7     class CommandManager {
8     constructor(cmdDir) {
9     this.msg = null;
10     this.commandsDirectory = cmdDir;
11     this.commands = [];
12     this.commandName = "";
13     this.argv = [];
14     this.args = [];
15     this.loadCommands();
16     this.snippetManager = new SnippetManager();
17     }
18    
19     setMessage(msg) {
20     this.msg = msg;
21     this.argv = msg.content.split(/ +/g);
22     this.args = this.argv;
23     this.commandName = this.args.shift().trim().replace(new RegExp(`^${escapeRegex(app.config.get('prefix'))}`), "");
24     }
25    
26     loadCommands() {
27     fs.readdir(this.commandsDirectory, (err, files) => {
28     if (err) {
29     return console.log('Unable to scan commands directory: ' + err);
30     }
31    
32     this.commandNames = files.map(file => file.replace('\.js', ''));
33    
34     for (let index in files) {
35     this.commands[this.commandNames[index]] = require(path.resolve(this.commandsDirectory, files[index]));
36     }
37     });
38     }
39    
40     has() {
41     return typeof this.commands[this.commandName] !== 'undefined';
42     }
43    
44     snippet() {
45     return this.snippetManager.find(this.commandName);
46     }
47    
48     hasValid() {
49     return this.has() && this.valid();
50     }
51    
52     valid() {
53     return this.msg.content.startsWith(app.config.get('prefix'));
54     }
55    
56     async notFound() {
57     if (app.config.get('debug')) {
58     await app.msg.reply({
59     embeds: [
60     new MessageEmbed()
61     .setColor('#f14a60')
62     .setDescription(`${escapeRegex(this.commandName)}: command not found`)
63     ]
64     });
65     }
66     }
67    
68     async exec() {
69     return await this.commands[this.commandName].handle(this.msg, this);
70     }
71     }
72    
73     module.exports = CommandManager;

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26