-
Notifications
You must be signed in to change notification settings - Fork 0
/
command-handler.ts
45 lines (35 loc) · 1.15 KB
/
command-handler.ts
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
import {Client} from "discord.js";
import getFiles from './get-files';
export default (client: Client) => {
const commands = {} as {
[key: string]: any
}
const suffix = ".ts"
const commandFiles = getFiles('./commands', suffix)
console.log(commandFiles);
for (const command of commandFiles) {
let commandFile = require(command)
if (commandFile.default) {
commandFile = commandFile.default;
}
const split = command.replace(/\\/g, '/').split('/');
const commandName = split[split.length - 1].replace(suffix, "");
commands[commandName.toLowerCase()] = commandFile;
}
console.log(commands);
client.on("messageCreate", (message) => {
if (message.author.bot || !message.content.startsWith("?")) {
return;
}
const args = message.content.slice(1).split(/ +/);
const commandName = args.shift()!.toLowerCase();
if (!commands[commandName]) {
return
}
try {
commands[commandName].callback(message, ...args)
} catch (error) {
console.error(error);
}
})
}