2016-12-07 22:52:37 -05:00
|
|
|
var discord = require('discord.js');
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var config = require('config');
|
|
|
|
|
|
|
|
var logger = require('./logging.js');
|
|
|
|
var app = require('./app.js');
|
|
|
|
var data = require('./data.js');
|
|
|
|
|
|
|
|
var cachedModules = [];
|
2016-12-31 00:49:12 -05:00
|
|
|
var cachedTriggers = [];
|
2016-12-07 22:52:37 -05:00
|
|
|
var client = new discord.Client();
|
|
|
|
|
|
|
|
function findArray(haystack, arr) {
|
|
|
|
return arr.some(function (v) {
|
|
|
|
return haystack.indexOf(v) >= 0;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
client.on('ready', () => {
|
|
|
|
// Initalize app channels.
|
|
|
|
app.logChannel = client.channels.get(config.logChannel);
|
|
|
|
app.guild = app.logChannel.guild;
|
|
|
|
|
2017-03-30 21:17:21 -04:00
|
|
|
logger.info('Bot is now online and connected to server.');
|
2016-12-07 22:52:37 -05:00
|
|
|
});
|
|
|
|
|
|
|
|
client.on('message', message => {
|
|
|
|
if (message.author.bot && message.content.startsWith('.ban') == false) { return; }
|
|
|
|
|
|
|
|
if (message.guild == null) {
|
|
|
|
// We want to log PM attempts.
|
|
|
|
logger.info(`${message.author.username} ${message.author} [PM]: ${message.content}`);
|
|
|
|
app.logChannel.sendMessage(`${message.author} [PM]: ${message.content}`);
|
|
|
|
message.reply(config.pmReply);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.verbose(`${message.author.username} ${message.author} [Channel: ${message.channel}]: ${message.content}`);
|
|
|
|
|
|
|
|
if (message.content.startsWith(config.commandPrefix)) {
|
|
|
|
let cmd = message.content.split(' ')[0].slice(1);
|
|
|
|
|
|
|
|
// Check by the name of the command.
|
|
|
|
let cachedModule = cachedModules[`${cmd}.js`];
|
|
|
|
let cachedModuleType = 'Command';
|
|
|
|
// Check by the quotes in the configuration.
|
|
|
|
if (cachedModule == null) { cachedModule = config.quotes[cmd]; cachedModuleType = 'Quote'; }
|
|
|
|
|
|
|
|
if (cachedModule) {
|
|
|
|
// Check access permissions.
|
|
|
|
if (cachedModule.roles != undefined && findArray(message.member.roles.map(function(x) { return x.name; }), cachedModule.roles) == false) {
|
|
|
|
app.logChannel.sendMessage(`${message.author} attempted to use admin command: ${message.content}`);
|
|
|
|
logger.info(`${message.author.username} ${message.author} attempted to use admin command: ${message.content}`)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-30 21:29:52 -04:00
|
|
|
logger.info(`${message.author.username} ${message.author} [Channel: ${message.channel}] executed command: ${message.content}`);
|
2016-12-07 22:52:37 -05:00
|
|
|
message.delete();
|
|
|
|
|
2017-01-15 12:37:43 -05:00
|
|
|
try {
|
|
|
|
if (cachedModuleType == 'Command') {
|
|
|
|
cachedModule.command(message);
|
|
|
|
} else if (cachedModuleType == 'Quote') {
|
|
|
|
cachedModules['quote.js'].command(message, cachedModule.reply);
|
|
|
|
}
|
|
|
|
} catch (err) { logger.error(err); }
|
2016-12-07 22:52:37 -05:00
|
|
|
|
2017-01-15 12:37:43 -05:00
|
|
|
try {
|
|
|
|
// Check if the command requires a warning.
|
|
|
|
if (cmd != 'warn' && cachedModule.warn == true) {
|
|
|
|
cachedModules['warn.js'].command(message);
|
|
|
|
}
|
|
|
|
} catch (err) { logger.error(err); }
|
2016-12-07 22:52:37 -05:00
|
|
|
} else {
|
|
|
|
// Not a valid command.
|
|
|
|
}
|
2017-01-29 15:50:10 -05:00
|
|
|
} else if (message.author.bot == false) {
|
2016-12-31 00:49:12 -05:00
|
|
|
// This is a normal channel message.
|
|
|
|
cachedTriggers.forEach(function(trigger) {
|
|
|
|
if (trigger.roles == undefined || findArray(message.member.roles.map(function(x) { return x.name; }), trigger.roles)) {
|
|
|
|
if (trigger.trigger(message) == true) {
|
2017-03-30 21:29:52 -04:00
|
|
|
logger.info(`${message.author.username} ${message.author} [Channel: ${message.channel}] triggered: ${message.content}`);
|
2017-01-15 12:37:43 -05:00
|
|
|
try {
|
|
|
|
trigger.execute(message);
|
|
|
|
} catch (err) { logger.error(err); }
|
2016-12-31 00:49:12 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-12-07 22:52:37 -05:00
|
|
|
}
|
2017-03-30 21:17:21 -04:00
|
|
|
});
|
2016-12-07 22:52:37 -05:00
|
|
|
|
2017-03-30 21:17:21 -04:00
|
|
|
// Cache all command modules.
|
2017-03-30 21:18:23 -04:00
|
|
|
cachedModules = [];
|
2017-03-30 21:17:21 -04:00
|
|
|
require("fs").readdirSync('./commands/').forEach(function(file) {
|
|
|
|
// Load the module if it's a script.
|
|
|
|
if (path.extname(file) == '.js') {
|
|
|
|
logger.info(`Loaded module: ${file}`);
|
|
|
|
cachedModules[file] = require(`./commands/${file}`);
|
|
|
|
}
|
2016-12-07 22:52:37 -05:00
|
|
|
});
|
|
|
|
|
2017-03-30 21:17:21 -04:00
|
|
|
// Cache all triggers.
|
2017-03-30 21:18:23 -04:00
|
|
|
cachedTriggers = [];
|
2017-03-30 21:17:21 -04:00
|
|
|
require("fs").readdirSync('./triggers/').forEach(function(file) {
|
|
|
|
// Load the trigger if it's a script.
|
|
|
|
if (path.extname(file) == '.js') {
|
|
|
|
logger.info(`Loaded trigger: ${file}`);
|
2017-03-30 21:29:52 -04:00
|
|
|
cachedTriggers[file] = require(`./triggers/${file}`);
|
2017-03-30 21:17:21 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
data.readWarnings();
|
|
|
|
data.readBans();
|
|
|
|
logger.info('Startup completed.');
|
|
|
|
|
2016-12-07 22:52:37 -05:00
|
|
|
client.login(config.clientLoginToken);
|