forked from Glazelf/NinigiBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bot.js
66 lines (55 loc) · 2.36 KB
/
bot.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
const Discord = require('discord.js');
let commando = require('discord.js-commando');
const Enmap = require("enmap");
const fs = require("fs");
const path = require("path");
const client = new Discord.Client({
partials: ['CHANNEL', 'GUILD_MEMBER', 'MESSAGE', 'REACTION', 'USER']
});
const config = require("./config.json");
client.config = config;
// This loop reads the /events/ folder and attaches each event file to the appropriate event.
fs.readdir("./events/", (err, files) => {
if (err) return console.error(err);
files.forEach(file => {
// If the file is not a JS file, ignore it.
if (!file.endsWith(".js")) return;
// Load the event file itself
const event = require(`./events/${file}`);
// Get just the event name from the file name
let eventName = file.split(".")[0];
// Each event will be called with the client argument,
// followed by its "normal" arguments, like message, member, etc.
client.on(eventName, event.bind(null, client));
delete require.cache[require.resolve(`./events/${file}`)];
});
});
client.commands = new Enmap();
client.aliases = new Discord.Collection();
walk(`./commands/`);
client.login(config.token);
// This loop reads the /commands/ folder and attaches each command file to the appropriate command.
function walk(dir, callback) {
fs.readdir(dir, function (err, files) {
if (err) throw err;
files.forEach(function (file) {
let filepath = path.join(dir, file);
fs.stat(filepath, function (err, stats) {
if (stats.isDirectory()) {
walk(filepath, callback);
} else if (stats.isFile() && file.endsWith('.js')) {
let props = require(`./${filepath}`);
let commandName = file.split(".")[0];
console.log(`Loading Command: ${commandName} ✔ Success! `);
client.commands.set(commandName, props);
if (props.config.aliases) {
props.config.aliases.forEach(alias => {
if (client.aliases.get(alias)) return console.log("Warning: Two commands share an alias name!");
client.aliases.set(alias, commandName);
});
};
};
});
});
});
};