Command-fix/src/server.js

188 lines
7.0 KiB
JavaScript
Raw Normal View History

// Check for environmental variables.
require('checkenv').check();
2017-08-03 03:48:16 +02:00
const discord = require('discord.js');
const path = require('path');
const schedule = require('node-schedule');
const fs = require('fs');
2016-12-08 04:52:37 +01:00
2017-08-03 03:48:16 +02:00
const logger = require('./logging.js');
const state = require('./state.js');
const data = require('./data.js');
2016-12-08 04:52:37 +01:00
state.responses = require('./responses.json');
2016-12-08 04:52:37 +01:00
var cachedModules = [];
2016-12-31 06:49:12 +01:00
var cachedTriggers = [];
2016-12-08 04:52:37 +01:00
var client = new discord.Client();
logger.info('Application startup. Configuring environment.');
process.on('unhandledRejection', (error, promise) => {
logger.error(`Unhandled promise rejection: ${error.message}.`, error);
});
process.on('uncaughtException', error => {
logger.error(`Unhandled exception: ${error.message}.`, error);
2017-09-24 21:44:49 +02:00
});
function findArray(haystack, arr) {
2017-09-30 01:38:00 +02:00
return arr.some(function (v) {
return haystack.indexOf(v) >= 0;
});
}
2016-12-08 04:52:37 +01:00
client.on('ready', () => {
// Initalize app channels.
state.logChannel = client.channels.get(process.env.DISCORD_LOG_CHANNEL);
state.guild = state.logChannel.guild;
2016-12-08 04:52:37 +01:00
logger.info('Bot is now online and connected to server.');
2016-12-08 04:52:37 +01:00
});
2017-09-30 01:38:00 +02:00
client.on('guildMemberAdd', (member) => {
member.addRole(process.env.DISCORD_RULES_ROLE);
state.stats.joins += 1;
});
2017-09-30 01:38:00 +02:00
client.on('guildMemberRemove', (member) => {
state.stats.leaves += 1;
});
// Output the stats for state.stats every 24 hours.
// Server is in UTC mode, 11:30 EST would be 03:30 UTC.
2017-09-30 01:38:00 +02:00
schedule.scheduleJob({ hour: 3, minute: 30 }, function () {
logger.info(`Here are today's stats for ${(new Date()).toLocaleDateString()}! ${state.stats.joins} users have joined, ${state.stats.ruleAccepts} users have accepted the rules, ${state.stats.leaves} users have left, ${state.stats.warnings} warnings have been issued.`);
state.logChannel.sendMessage(`Here are today's stats for ${(new Date()).toLocaleDateString()}! ${state.stats.joins} users have joined, ${state.stats.ruleAccepts} users have accepted the rules, ${state.stats.leaves} users have left, ${state.stats.warnings} warnings have been issued.`);
// Clear the stats for the day.
state.stats.joins = 0;
state.stats.ruleAccepts = 0;
state.stats.leaves = 0;
state.stats.warnings = 0;
});
2016-12-08 04:52:37 +01:00
client.on('message', message => {
2017-09-30 01:38:00 +02:00
if (message.author.bot && message.content.startsWith('.ban') === false) { return; }
2016-12-08 04:52:37 +01:00
if (message.guild == null && state.responses.pmReply) {
2016-12-08 04:52:37 +01:00
// We want to log PM attempts.
logger.info(`${message.author.username} ${message.author} [PM]: ${message.content}`);
state.logChannel.sendMessage(`${message.author} [PM]: ${message.content}`);
message.reply(state.responses.pmReply);
2016-12-08 04:52:37 +01:00
return;
}
2017-07-09 05:53:01 +02:00
logger.verbose(`${message.author.username} ${message.author} [Channel: ${message.channel.name} ${message.channel}]: ${message.content}`);
2016-12-08 04:52:37 +01:00
// Check if the channel is #rules, if so we want to follow a different logic flow.
if (message.channel.id === process.env.DISCORD_RULES_CHANNEL) {
if (message.content.includes(process.env.DISCORD_RULES_TRIGGER)) {
// We want to remove the 'Unauthorized' role from them once they agree to the rules.
logger.verbose(`${message.author.username} ${message.author} has accepted the rules, removing role ${process.env.DISCORD_RULES_ROLE}.`);
state.stats.ruleAccepts += 1;
message.member.removeRole(process.env.DISCORD_RULES_ROLE, 'Accepted the rules.');
}
// Delete the message in the channel to force a cleanup.
message.delete();
return;
} else if (message.content.startsWith('.') && message.content.startsWith('..') === false) {
// We want to make sure it's an actual command, not someone '...'-ing.
2016-12-08 04:52:37 +01:00
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 = state.responses.quotes[cmd]; cachedModuleType = 'Quote'; }
2016-12-08 04:52:37 +01:00
if (cachedModule) {
// Check access permissions.
2017-09-30 01:38:00 +02:00
if (cachedModule.roles !== undefined && findArray(message.member.roles.map(function (x) { return x.name; }), cachedModule.roles) === false) {
state.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}`);
2016-12-08 04:52:37 +01:00
return false;
}
logger.info(`${message.author.username} ${message.author} [Channel: ${message.channel}] executed command: ${message.content}`);
2016-12-08 04:52:37 +01:00
message.delete();
try {
2017-09-30 01:38:00 +02:00
if (cachedModuleType === 'Command') {
cachedModule.command(message);
2017-09-30 01:38:00 +02:00
} else if (cachedModuleType === 'Quote') {
cachedModules['quote.js'].command(message, cachedModule.reply);
}
} catch (err) { logger.error(err); }
// Warn after running command?
try {
// Check if the command requires a warning.
if (cmd !== 'warn' && cachedModule.warn === true) {
// Access check to see if the user has privilages to warn.
let warnCommand = cachedModules['warn.js'];
if (findArray(message.member.roles.map(function (x) { return x.name; }), warnCommand.roles)) {
// They are allowed to warn because they are in warn's roles.
warnCommand.command(message);
}
}
} catch (err) { logger.error(err); }
} else {
// Not a valid command.
2016-12-08 04:52:37 +01:00
}
2017-09-30 01:38:00 +02:00
} else if (message.author.bot === false) {
2016-12-31 06:49:12 +01:00
// This is a normal channel message.
2017-09-30 01:38:00 +02:00
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) {
logger.debug(`${message.author.username} ${message.author} [Channel: ${message.channel}] triggered: ${message.content}`);
try {
trigger.execute(message);
} catch (err) { logger.error(err); }
2016-12-31 06:49:12 +01:00
}
2017-09-30 01:38:00 +02:00
}
2016-12-31 06:49:12 +01:00
});
2016-12-08 04:52:37 +01:00
}
});
2016-12-08 04:52:37 +01:00
// Cache all command modules.
2017-03-31 03:18:23 +02:00
cachedModules = [];
fs.readdirSync('./src/commands/').forEach(function (file) {
// Load the module if it's a script.
2017-09-30 01:38:00 +02:00
if (path.extname(file) === '.js') {
if (file.includes('.disabled')) {
logger.info(`Did not load disabled module: ${file}`);
} else {
logger.info(`Loaded module: ${file}`);
cachedModules[file] = require(`./commands/${file}`);
}
}
2016-12-08 04:52:37 +01:00
});
// Cache all triggers.
2017-03-31 03:18:23 +02:00
cachedTriggers = [];
fs.readdirSync('./src/triggers/').forEach(function (file) {
// Load the trigger if it's a script.
2017-09-30 01:38:00 +02:00
if (path.extname(file) === '.js') {
if (file.includes('.disabled')) {
logger.info(`Did not load disabled trigger: ${file}`);
} else {
logger.info(`Loaded trigger: ${file}`);
cachedTriggers.push(require(`./triggers/${file}`));
}
}
});
data.readWarnings();
data.readBans();
// Load custom responses
if (process.env.DATA_CUSTOM_RESPONSES) {
data.readCustomResponses();
}
client.login(process.env.DISCORD_LOGIN_TOKEN);
2017-09-30 01:38:00 +02:00
logger.info('Startup completed. Established connection to Discord.');