2017-09-30 01:21:48 +02:00
|
|
|
// 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');
|
2017-10-05 04:21:36 +02:00
|
|
|
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');
|
2017-10-05 04:21:36 +02:00
|
|
|
const state = require('./state.js');
|
2018-04-04 01:28:33 +02:00
|
|
|
const data = require('./data.js');
|
2016-12-08 04:52:37 +01:00
|
|
|
|
2018-04-04 01:28:33 +02:00
|
|
|
state.responses = require('./responses.json');
|
2017-09-30 01:21:48 +02:00
|
|
|
|
2019-07-22 02:56:54 +02:00
|
|
|
let cachedModules = [];
|
|
|
|
let cachedTriggers = [];
|
|
|
|
const client = new discord.Client();
|
2016-12-08 04:52:37 +01:00
|
|
|
|
2019-07-22 02:55:55 +02:00
|
|
|
let mediaUsers = new Map();
|
|
|
|
|
2018-04-04 01:28:33 +02:00
|
|
|
logger.info('Application startup. Configuring environment.');
|
|
|
|
|
2018-01-20 06:11:52 +01:00
|
|
|
process.on('unhandledRejection', (error, promise) => {
|
2019-07-22 02:56:54 +02:00
|
|
|
logger.error(`Unhandled promise rejection: ${error.message}.`, { meta: error });
|
2018-01-20 06:11:52 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
process.on('uncaughtException', error => {
|
2018-04-04 02:05:01 +02:00
|
|
|
logger.error(`Unhandled exception: ${error.message}.`, { meta: error });
|
|
|
|
process.exit(-1);
|
2017-09-24 21:44:49 +02:00
|
|
|
});
|
|
|
|
|
2019-07-22 02:56:54 +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
|
|
|
|
2019-08-16 08:51:51 +02:00
|
|
|
function IsIgnoredCategory (categoryName) {
|
|
|
|
const IgnoredCategory = ['welcome', 'team', 'website-team'];
|
|
|
|
return IgnoredCategory.includes(categoryName);
|
2019-08-12 09:44:22 +02:00
|
|
|
}
|
|
|
|
|
2016-12-08 04:52:37 +01:00
|
|
|
client.on('ready', () => {
|
2019-07-22 02:56:54 +02:00
|
|
|
// Initialize app channels.
|
2017-10-05 04:21:36 +02:00
|
|
|
state.logChannel = client.channels.get(process.env.DISCORD_LOG_CHANNEL);
|
2019-08-09 20:26:55 +02:00
|
|
|
state.msglogChannel = client.channels.get(process.env.DISCORD_MSGLOG_CHANNEL);
|
2017-10-05 04:21:36 +02:00
|
|
|
state.guild = state.logChannel.guild;
|
2016-12-08 04:52:37 +01:00
|
|
|
|
2017-03-31 03:17:21 +02:00
|
|
|
logger.info('Bot is now online and connected to server.');
|
2016-12-08 04:52:37 +01:00
|
|
|
});
|
|
|
|
|
2018-09-07 03:10:21 +02:00
|
|
|
client.on('error', (x) => {
|
2019-07-22 02:56:54 +02:00
|
|
|
logger.error(x);
|
|
|
|
logger.error('Restarting process.');
|
|
|
|
process.exit(1);
|
|
|
|
});
|
2018-09-07 03:10:21 +02:00
|
|
|
client.on('warn', (x) => {
|
2019-07-22 02:56:54 +02:00
|
|
|
logger.warn(x);
|
|
|
|
});
|
2018-09-07 03:10:21 +02:00
|
|
|
|
2019-07-22 02:56:54 +02:00
|
|
|
client.on('debug', (x) => null);
|
2018-09-07 03:10:21 +02:00
|
|
|
|
|
|
|
client.on('disconnect', () => {
|
2019-07-22 02:56:54 +02:00
|
|
|
logger.warn('Disconnected from Discord server.');
|
|
|
|
});
|
2018-09-07 03:10:21 +02:00
|
|
|
client.on('reconnecting', () => {
|
2019-07-22 02:56:54 +02:00
|
|
|
logger.warn('Reconnecting...');
|
|
|
|
});
|
2018-09-07 03:10:21 +02:00
|
|
|
|
2017-09-30 01:38:00 +02:00
|
|
|
client.on('guildMemberAdd', (member) => {
|
2018-02-28 01:19:14 +01:00
|
|
|
member.addRole(process.env.DISCORD_RULES_ROLE);
|
2017-07-20 03:45:58 +02:00
|
|
|
});
|
|
|
|
|
2019-08-09 20:26:55 +02:00
|
|
|
client.on('messageDelete', message => {
|
2019-08-16 08:51:51 +02:00
|
|
|
if (IsIgnoredCategory(message.channel.parent.name) == false) {
|
2019-08-12 09:44:22 +02:00
|
|
|
if (message.content && message.content.startsWith('.') == false && message.author.bot == false) {
|
|
|
|
const deletionEmbed = new discord.RichEmbed()
|
|
|
|
.setAuthor(message.author.tag, message.author.displayAvatarURL)
|
|
|
|
.setDescription(`Message deleted in ${message.channel}`)
|
|
|
|
.addField('Content', message.cleanContent, false)
|
|
|
|
.setTimestamp()
|
|
|
|
.setColor('RED');
|
|
|
|
|
|
|
|
state.msglogChannel.send(deletionEmbed);
|
|
|
|
logger.info(`${message.author.username} ${message.author} deleted message: ${message.cleanContent}.`);
|
|
|
|
}
|
2019-08-09 20:26:55 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
client.on('messageUpdate', (oldMessage, newMessage) => {
|
2019-08-16 08:51:51 +02:00
|
|
|
const AllowedRoles = ['Administrators', 'Moderators', 'Team', 'VIP'];
|
|
|
|
if (!findArray(oldMessage.member.roles.map(function (x) { return x.name; }), AllowedRoles)) {
|
|
|
|
if (IsIgnoredCategory(oldMessage.channel.parent.name) == false) {
|
|
|
|
const oldM = oldMessage.cleanContent;
|
|
|
|
const newM = newMessage.cleanContent;
|
|
|
|
if (oldMessage.content != newMessage.content && oldM && newM) {
|
|
|
|
const editedEmbed = new discord.RichEmbed()
|
|
|
|
.setAuthor(oldMessage.author.tag, oldMessage.author.displayAvatarURL)
|
|
|
|
.setDescription(`Message edited in ${oldMessage.channel} [Jump To Message](${newMessage.url})`)
|
|
|
|
.addField('Before', oldM, false)
|
|
|
|
.addField('After', newM, false)
|
|
|
|
.setTimestamp()
|
|
|
|
.setColor('GREEN');
|
|
|
|
|
|
|
|
state.msglogChannel.send(editedEmbed);
|
|
|
|
logger.info(`${oldMessage.author.username} ${oldMessage.author} edited message from: ${oldM} to: ${newM}.`);
|
|
|
|
}
|
2019-08-12 09:44:22 +02:00
|
|
|
}
|
2019-08-09 20:26:55 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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
|
|
|
|
2018-01-20 05:29:33 +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}`);
|
2018-04-04 01:50:20 +02:00
|
|
|
state.logChannel.send(`${message.author} [PM]: ${message.content}`);
|
2018-01-20 05:29:33 +01:00
|
|
|
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
|
|
|
|
2019-07-22 02:55:55 +02:00
|
|
|
if (message.channel.id === process.env.DISCORD_MEDIA_CHANNEL && !message.author.bot) {
|
|
|
|
const AllowedMediaRoles = ['Administrators', 'Moderators', 'Team', 'VIP'];
|
|
|
|
if (!findArray(message.member.roles.map(function (x) { return x.name; }), AllowedMediaRoles)) {
|
|
|
|
const urlRegex = new RegExp(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{2,256}\.[a-z]{2,4}\b([-a-zA-Z0-9@:%_+.~#?&//=]*)/gi);
|
|
|
|
if (message.attachments.size > 0 || message.content.match(urlRegex)) {
|
|
|
|
mediaUsers.set(message.author.id, true);
|
|
|
|
} else if (mediaUsers.get(message.author.id)) {
|
|
|
|
mediaUsers.set(message.author.id, false);
|
|
|
|
} else {
|
|
|
|
message.delete();
|
|
|
|
mediaUsers.set(message.author.id, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-28 01:19:14 +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) {
|
2019-03-07 05:25:59 +01:00
|
|
|
if (message.content.toLowerCase().includes(process.env.DISCORD_RULES_TRIGGER)) {
|
2018-02-28 01:19:14 +01:00
|
|
|
// 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}.`);
|
|
|
|
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.
|
2018-01-20 05:29:33 +01:00
|
|
|
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) {
|
2018-04-04 01:50:20 +02:00
|
|
|
state.logChannel.send(`${message.author} attempted to use admin command: ${message.content}`);
|
2017-08-03 01:11:05 +02:00
|
|
|
logger.info(`${message.author.username} ${message.author} attempted to use admin command: ${message.content}`);
|
2016-12-08 04:52:37 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-03-31 03:29:52 +02:00
|
|
|
logger.info(`${message.author.username} ${message.author} [Channel: ${message.channel}] executed command: ${message.content}`);
|
2016-12-08 04:52:37 +01:00
|
|
|
message.delete();
|
|
|
|
|
2017-01-15 18:37:43 +01:00
|
|
|
try {
|
2017-09-30 01:38:00 +02:00
|
|
|
if (cachedModuleType === 'Command') {
|
2017-01-15 18:37:43 +01:00
|
|
|
cachedModule.command(message);
|
2017-09-30 01:38:00 +02:00
|
|
|
} else if (cachedModuleType === 'Quote') {
|
2017-01-15 18:37:43 +01:00
|
|
|
cachedModules['quote.js'].command(message, cachedModule.reply);
|
|
|
|
}
|
|
|
|
} catch (err) { logger.error(err); }
|
2018-04-04 01:28:33 +02:00
|
|
|
|
|
|
|
// Warn after running command?
|
|
|
|
try {
|
|
|
|
// Check if the command requires a warning.
|
|
|
|
if (cmd !== 'warn' && cachedModule.warn === true) {
|
2019-07-22 02:56:54 +02:00
|
|
|
// Access check to see if the user has privileges to warn.
|
2018-04-04 01:28:33 +02:00
|
|
|
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
|
|
|
}
|
2017-03-31 03:17:21 +02:00
|
|
|
});
|
2016-12-08 04:52:37 +01:00
|
|
|
|
2017-03-31 03:17:21 +02:00
|
|
|
// Cache all command modules.
|
2017-03-31 03:18:23 +02:00
|
|
|
cachedModules = [];
|
2017-10-05 04:21:36 +02:00
|
|
|
fs.readdirSync('./src/commands/').forEach(function (file) {
|
2017-03-31 03:17:21 +02:00
|
|
|
// Load the module if it's a script.
|
2017-09-30 01:38:00 +02:00
|
|
|
if (path.extname(file) === '.js') {
|
2017-04-08 01:36:45 +02:00
|
|
|
if (file.includes('.disabled')) {
|
|
|
|
logger.info(`Did not load disabled module: ${file}`);
|
|
|
|
} else {
|
|
|
|
logger.info(`Loaded module: ${file}`);
|
|
|
|
cachedModules[file] = require(`./commands/${file}`);
|
|
|
|
}
|
2017-03-31 03:17:21 +02:00
|
|
|
}
|
2016-12-08 04:52:37 +01:00
|
|
|
});
|
|
|
|
|
2017-03-31 03:17:21 +02:00
|
|
|
// Cache all triggers.
|
2017-03-31 03:18:23 +02:00
|
|
|
cachedTriggers = [];
|
2017-03-31 03:17:21 +02:00
|
|
|
|
2018-04-04 01:28:33 +02:00
|
|
|
data.readWarnings();
|
|
|
|
data.readBans();
|
|
|
|
|
|
|
|
// Load custom responses
|
|
|
|
if (process.env.DATA_CUSTOM_RESPONSES) {
|
|
|
|
data.readCustomResponses();
|
|
|
|
}
|
|
|
|
|
2017-09-30 01:21:48 +02:00
|
|
|
client.login(process.env.DISCORD_LOGIN_TOKEN);
|
2017-09-30 01:38:00 +02:00
|
|
|
logger.info('Startup completed. Established connection to Discord.');
|