-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatchers.js
100 lines (94 loc) · 4.56 KB
/
dispatchers.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
const { importModules, importNpmPlugins, contactBotAdminCheck } = require('./utils.js');
class Handlers {
constructor() {
this.commands = {};
this.keywords = [];
this.loadHandlers();
}
loadHandlers() {
this.commands = {};
this.keywords = [];
let handlers = {};
console.log("Loading handlers...");
const devMode = process.env.NODE_ENV === 'development';
if (devMode) {
console.log("Development mode enabled - handlers will be reloaded from disk");
}
Object.assign(handlers, importModules('./handlers/', devMode /* reloads the handlers from disk in development mode */));
Object.assign(handlers, importModules('./plugins/', true /* reloads the plugins from disk */));
Object.assign(handlers, importNpmPlugins());
for (const [filename, handler] of Object.entries(handlers)) {
if (Object.hasOwn(handler, 'commands')) {
Object.assign(this.commands, handler.commands);
}
if (Object.hasOwn(handler, 'keywords')) {
for (const keyword of handler.keywords) {
this.keywords.push(keyword);
}
}
this.commands["help"] = {
description: 'Mostra questo messaggio',
syntax: 'help [comando]',
handler: async (client, message, args, nconf) => {
let helpMessage = '';
if (args.length == 0) {
helpMessage = 'Comandi disponibili:\n';
for (const command of Object.keys(this.commands)) {
helpMessage += `• ${nconf.get("COMMAND_PREFIX")}${command}: ${this.commands[command].description}\n`;
};
helpMessage += `\nScrivi ${nconf.get("COMMAND_PREFIX")}help <comando> per maggiori informazioni su un comando specifico`;
}
else if (args.length == 1) {
let targetCommand = args[0];
if (targetCommand[0] == nconf.get("COMMAND_PREFIX")) {
targetCommand = targetCommand.slice(1);
}
if (this.commands[targetCommand]) {
helpMessage = `${nconf.get("COMMAND_PREFIX")}${targetCommand}: ${this.commands[targetCommand].description}\nSintassi: ${nconf.get("COMMAND_PREFIX")}${this.commands[targetCommand].syntax}`;
}
else {
helpMessage = `Comando non riconosciuto: ${nconf.get("COMMAND_PREFIX")}${targetCommand}. Scrivi ${nconf.get("COMMAND_PREFIX")}help per un elenco dei comandi disponibili`;
};
}
else {
helpMessage = `Scrivi ${nconf.get("COMMAND_PREFIX")}help per un elenco dei comandi disponibili`;
};
await message.reply(helpMessage);
}
};
this.commands["reload"] = {
description: 'Ricarica gli handlers',
syntax: 'reload',
handler:
async (client, message, args, nconf) => {
if (await message.getContact().then(contact => contactBotAdminCheck(contact, nconf))) {
await message.reply('Ricaricamento handlers...');
this.loadHandlers();
await message.reply('Handlers ricaricati');
}
else {
await message.reply('Non hai i permessi per eseguire questo comando');
}
}
};
}
console.log("Handlers loaded");
}
async commandDispatcher(client, message, command, args, nconf) {
if (this.commands[command]) {
this.commands[command].handler(client, message, args, nconf).catch((error) => { console.log(error) });
}
else {
await message.reply(`Comando non riconosciuto: ${nconf.get("COMMAND_PREFIX")}${command}. Scrivi ${nconf.get("COMMAND_PREFIX")}help per un elenco dei comandi disponibili`);
}
}
async keywordDispatcher(client, message, nconf) {
for (const keyword of this.keywords) {
const matches = message.body.match(keyword.regex);
if (matches) await keyword.handler(client, message, matches, nconf);
}
}
}
module.exports = {
Handlers: Handlers,
};