-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
83 lines (74 loc) · 2.7 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
const config = require('./config.json');
const Discord = require('discord.js');
// Set up the database
const mongoose = require('mongoose');
mongoose.connect(config.mongourl, {
useNewUrlParser: true,
useUnifiedTopology: true
});
// Initialize bot
const bot = new Discord.Client({
disableEveryone: true,
disabledEvents: ['TYPING_START']
});
if (config.discordbotstoken) {
const express = require('express');
const http = require('http');
const server = http.createServer(express());
server.listen(config.discordbotswebhookport, () => {
console.log('Listening');
});
const DBL = require('dblapi.js');
const dbl = new DBL(config.discordbotstoken,
{ webhookPort: config.discordbotswebhookport, webhookServer: server }, bot);
}
// Set up commands
const commands = require('./commands');
const help = require('./commands/help');
const stop = require('./commands/stop');
bot.on('ready', () => {
bot.user.setActivity('lichess.org'); //you can set a default game
console.log(`Bot is online!\n${bot.users.size} users, in ${bot.guilds.size} servers connected.`);
});
bot.on('guildCreate', (guild) => {
console.log(`I've joined the guild ${guild.name} (${guild.id}), owned by ${guild.owner.user.username}.`);
});
bot.on('message', (msg) => {
//drop bot messages (including our own) to prevent feedback loops
if (msg.author.bot) {
return;
}
let cmdTxt = '';
let suffix = '';
if (msg.content[0] === config.prefix) {
cmdTxt = msg.content.split(' ')[0].substring(1);
suffix = msg.content.substring(cmdTxt.length + 2);
}
let command = commands[cmdTxt];
if (command) {
console.log(`Treating ${msg.content} from ${msg.author} (${msg.author.username}) as command`);
try {
commands[cmdTxt].process(bot, msg, suffix);
} catch (e) {
console.log(`Command failed:\n ${e.stack}`);
msg.channel.send(`Command ${cmdTxt} failed :(\n ${e.stack}`);
}
} else if (cmdTxt == 'help') {
help(bot, msg, suffix);
} else if (cmdTxt == 'stop') {
stop(bot, msg, suffix);
} else if (config.respondToInvalid) {
bot.sendMessage(msg.channel, `Invalid command: ${cmdTxt}`);
}
});
// Catch Errors before they crash the app.
process.on('uncaughtException', (err) => {
const errorMsg = err.stack.replace(new RegExp(`${__dirname}/`, 'g'), './');
console.error('Uncaught Exception: ', errorMsg);
process.exit(1); //Gracefully exit so systemd service may restart
});
process.on('unhandledRejection', err => {
console.error('Uncaught Promise Error: ', err);
process.exit(1); //Gracefully exit so systemd service may restart
});
bot.login(config.token);