discord-stats-bot/bot.js

68 lines
2.1 KiB
JavaScript
Raw Normal View History

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-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
// set some variables for log4js
2017-03-22 06:23:52 +00:00
var logcon = log4js.getLogger('console');
var actcon = log4js.getLogger('activeusers');
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
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
// set some variables for log4js
2017-03-22 06:23:52 +00:00
var chancon = log4js.getLogger('channels');
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 => {
// set some variables for log4js
var logcon = log4js.getLogger('console');
// 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
});
});