-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
68 lines (60 loc) · 2.05 KB
/
index.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
const { Client, Intents, Collection } = require('discord.js');
const bot = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const fs = require('fs');
bot.commands = new Collection();
const mysql = require('mysql');
const settings = require("./settings.js");
var con = mysql.createConnection(settings.sql);
function handleDisconnect() {
con = mysql.createConnection(settings.sql);
con.connect(function (err) {
if (err) {
console.log('error when connecting to db:', err);
setTimeout(handleDisconnect, 2000);
}
});
con.on('error', function (err) {
console.log('db error', err);
if (err.code === 'PROTOCOL_CONNECTION_LOST') {
handleDisconnect();
} else {
throw err;
}
});
}
handleDisconnect();
bot.login(settings.bot.token)
bot.on("ready", function () {
fs.readdir('./Events/', (error, f) => {
if (error) { return console.error(error); }
f.forEach((f) => {
let events = require(`./Events/${f}`);
let event = f.split('.')[0];
bot.on(event, events.bind(null, bot, con));
console.log("Loaded " + f + " event.");
});
})
fs.readdir('./Scripts/', (error, f) => {
if (error) { return console.error(error); }
console.log("Loaded " + f + " script.");
f.forEach((f) => {
let events = require(`./Scripts/${f}`);
events(bot, con);
});
})
fs.readdir("./Commands/", (error, f) => {
if (error) { return console.error(error); }
let commands = f.filter(file => file.endsWith(".js"));
if (commands.length <= 0) { return console.log("No command found."); }
commands.forEach((f) => {
let command = require(`./Commands/${f}`);
console.log("Loaded " + f + " command.");
bot.commands.set(command.help.name, command);
});
});
})
if (!Array.prototype.last) {
Array.prototype.last = function () {
return this[this.length - 1];
};
};