2017-03-22 06:52:59 +00:00
|
|
|
// Require Discord Libraries
|
|
|
|
var Discordie = require('discordie');
|
|
|
|
|
|
|
|
// Require log4js for logging to files
|
|
|
|
var log4js = require('log4js');
|
|
|
|
|
|
|
|
// require custom settings
|
|
|
|
var config = require('./config.json');
|
2017-03-22 06:23:52 +00:00
|
|
|
|
|
|
|
// Configure log4js
|
|
|
|
log4js.configure({
|
|
|
|
appenders: [
|
|
|
|
{ type: 'console' },
|
|
|
|
{ type: 'file', filename: 'logs/console.log', category: 'console'},
|
|
|
|
{ type: 'file', filename: 'logs/activeusers.log', category: 'activeusers'},
|
|
|
|
{ type: 'file', filename: 'logs/channels.log', category: 'channels'}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
|
2017-03-23 01:57:57 +00:00
|
|
|
// set constant log4js variables
|
|
|
|
const logcon = log4js.getLogger('console');
|
|
|
|
const actcon = log4js.getLogger('activeusers');
|
|
|
|
const chancon = log4js.getLogger('channels');
|
|
|
|
|
2017-03-22 06:52:59 +00:00
|
|
|
// begin discord bot
|
2017-03-22 06:23:52 +00:00
|
|
|
const Events = Discordie.Events;
|
|
|
|
const client = new Discordie();
|
|
|
|
|
2017-03-22 06:52:59 +00:00
|
|
|
// issue connect to discord using the bot_token in config.json
|
2017-03-22 06:23:52 +00:00
|
|
|
client.connect({
|
|
|
|
token: config.bot_token
|
|
|
|
});
|
|
|
|
|
2017-03-22 06:52:59 +00:00
|
|
|
// once connected
|
2017-03-22 06:23:52 +00:00
|
|
|
client.Dispatcher.on(Events.GATEWAY_READY, e => {
|
2017-03-22 06:52:59 +00:00
|
|
|
// acknoledge connection to console logs
|
2017-03-22 06:23:52 +00:00
|
|
|
logcon.info('Connected as: ' + client.User.username);
|
2017-03-22 06:52:59 +00:00
|
|
|
// check for the number of active users every 30 seconds and log to the active users logs
|
2017-03-22 21:30:55 +00:00
|
|
|
setInterval(function() {
|
|
|
|
client.Users.fetchMembers(config.guild_id);
|
|
|
|
actcon.info(config.guild_name + " Active Users: " + client.Users.onlineMembersForGuild(config.guild_id).length);}, 30000)
|
2017-03-22 06:23:52 +00:00
|
|
|
});
|
|
|
|
|
2017-03-22 06:52:59 +00:00
|
|
|
// when messages are created
|
2017-03-22 06:23:52 +00:00
|
|
|
client.Dispatcher.on(Events.MESSAGE_CREATE, e => {
|
2017-03-22 06:52:59 +00:00
|
|
|
// log the guild name, the channel name, the username, and the message to the channels log
|
2017-03-22 06:23:52 +00:00
|
|
|
chancon.info(e.message.guild.name + ":" + " #" + e.message.channel.name + ": " + "<" + e.message.displayUsername + ">: "+ e.message.content);
|
|
|
|
});
|
2017-03-23 01:44:57 +00:00
|
|
|
|
|
|
|
// if connection is lost to Discord, issue a reconnect.
|
|
|
|
|
|
|
|
client.Dispatcher.on(Events.DISCONNECTED, e => {
|
|
|
|
// force disconnection to Discord
|
|
|
|
client.disconnect();
|
|
|
|
logcon.info('Disconnected from server ...');
|
|
|
|
|
|
|
|
// reconnect to Discord
|
|
|
|
logcon.info('Reconnecting to Discord ... ');
|
|
|
|
client.connect({
|
|
|
|
token: config.bot_token
|
|
|
|
});
|
|
|
|
});
|
2017-03-23 01:57:57 +00:00
|
|
|
|
|
|
|
// also, reconnect the bot hourly.
|
|
|
|
|
|
|
|
setInterval(function() {
|
|
|
|
logcon.info('Reconnecting to server per timeout of ' + config.reconnect + 'ms.');
|
|
|
|
client.disconnect();
|
|
|
|
client.connect({
|
|
|
|
token: config.bot_token
|
|
|
|
});
|
|
|
|
}, config.reconnect);
|