-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
80 lines (64 loc) · 1.98 KB
/
main.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
const Discord = require("discord.js"); // const Discord to communicate with node modules
const { prefix, token } = require("./config.json"); // token and prefix from config file
const client = new Discord.Client(); // bot instance - Clyde
const fs = require("fs"); // file system
client.commands = new Discord.Collection(); // collection of commands
const commandFiles = fs
.readdirSync("./commands/")
.filter((file) => file.endsWith(".js")); // read .js files in commands folder
/**
* read all command files in folder and add to collection
*/
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
/**
* run Clyde bot
*/
client.once("ready", () => {
console.log("Clyde is online.");
});
/**
* respond to message command
*/
client.on("message", (message) => {
/**
* check if message is a command. if not, do nothing.
*/
if (!message.content.startsWith(prefix)) {
return;
}
/**
* check if message author is Clyde itself. if so, do nothing.
*/
if (message.author.bot) {
return;
}
/**
* decipher command and store it in command const
*/
const args = message.content.slice(prefix.length).split(/ +/);
const commandName = args.shift().toLowerCase();
/**
* commands
*/
if (!client.commands.has(commandName)) return; // if user entered command does not exist in collection, do nothing.
const command = client.commands.get(commandName);
if (command.args && !args.length) {
let reply = `No arguments were provided!`;
if (command.usage) {
reply += `\nExpected command usage is: \`${prefix}${command.name} ${command.usage}\``;
}
return message.channel.send(reply);
}
try {
command.execute(message, args);
} catch (error) {
console.error(error);
message.reply(
`An error has occurred while trying to execute that command! Please ensure proper comman usage using \`${prefix}help ${command.name}\``
);
}
});
client.login(token);