-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
59 lines (47 loc) · 1.8 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
const fs = require('fs');
const winston = require('winston');
const Discord = require('discord.js');
const logger = require('./util/logger.js');
const scheduler = require('./util/scheduler.js');
const { prefix, discordToken, discordUserID } = require('./config.json');
const database = require('./util/database.js');
const client = new Discord.Client();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
client.commands = new Discord.Collection();
for (const file of commandFiles) {
const command = require(`./commands/${file}`);
client.commands.set(command.name, command);
}
// ---------------------------------------------------------------
client.on('ready', () => {
scheduler.init(client);
winston.info('Restoring reminders and subscriptions..');
scheduler.restore();
winston.info('Ready.');
});
client.on('message', (msg) => {
if ((msg.author.id != discordUserID) || !msg.content.startsWith(prefix) || msg.author.bot) return;
// Separate command call and arguments
const args = msg.content.slice(prefix.length).split(/ +/);
const commandCall = args.shift().toLowerCase();
const command = client.commands.get(commandCall)
|| client.commands.find(cmd => cmd.aliases && cmd.aliases.includes(commandCall));
if (!command) return;
try {
command.execute(msg, args);
} catch (e) {
winston.error(`${command.name} execute() failed. `, e);
}
});
client.on('guildCreate', (guild) => {
winston.info('Joined guild. Creating guild data..');
});
client.on('guildDelete', (guild) => {
winston.info('Left guild. Deleting guild data..');
scheduler.cancelGuildJobs(guild.id);
// database.clearGuildData()?
});
// Start the bot
client.login(discordToken).catch((e) => {
winston.error('Login failed. Is the DISCORD_TOKEN env. variable set? ', e);
});