Removed commands: ban, clearWarnings, warn, warnings. Removed data.js in favor of inline fs.readFile. Deleted models. Deleted server.js warnings. Removed warning related functionality from state.js. Changed dep. discord.js function sendMessage to send. Cleanup.

This commit is contained in:
chris062689 2018-02-28 22:14:09 -05:00
parent b5cf83a9fe
commit b9a9fce06b
12 changed files with 23 additions and 190 deletions

View File

@ -1,24 +0,0 @@
const state = require('../state.js');
const data = require('../data.js');
const logger = require('../logging.js');
const UserBan = require('../models/UserBan.js');
exports.roles = ['Admins', 'Moderators', 'CitraBot'];
exports.command = function (message) {
message.mentions.users.map((user) => {
var count = state.warnings.filter(x => x.id === user.id && !x.cleared).length || 0;
message.channel.sendMessage(`${user} ${user.username}, You will now be banned from this channel.`);
logger.info(`${message.author.toString()} has banned ${user.toString()} ${user} ${user.username}.`);
state.logChannel.sendMessage(`${message.author} has banned ${user} ${user.username} [${count}].`);
state.bans.push(new UserBan(user.id, user.username, message.author.id, message.author.username, count));
message.guild.member(user).ban().catch(function (error) {
state.logChannel.sendMessage(`Error banning ${user} ${user.username}`);
logger.error(`Error banning ${user.toString()} ${user} ${user.username}.`, error);
});
data.flushBans();
});
};

View File

@ -1,20 +0,0 @@
const state = require('../state.js');
const data = require('../data.js');
const logger = require('../logging.js');
exports.roles = ['Admins', 'Moderators'];
exports.command = function (message) {
message.mentions.users.map((user) => {
var count = state.warnings.filter(x => x.id === user.id && !x.cleared);
if (count != null && count.length > 0) {
count.forEach(warning => { warning.cleared = true; });
data.flushWarnings();
message.channel.sendMessage(`${user}, your warnings have been cleared.`);
} else {
message.channel.sendMessage(`${user}, you have no warnings to clear.`);
}
logger.info(`${message.author.toString()} has cleared all warnings for ${user.toString()} [${count}].`);
state.logChannel.sendMessage(`${message.author.toString()} has cleared all warnings for ${user.toString()} [${count}].`);
});
};

View File

@ -1,4 +1,4 @@
exports.roles = ['Admins', 'Moderators', 'CitraBot']; exports.roles = ['Admins', 'Moderators'];
exports.command = function (message) { exports.command = function (message) {
var role = '345247291843805185'; var role = '345247291843805185';
message.mentions.users.map((user) => { message.mentions.users.map((user) => {
@ -7,10 +7,10 @@ exports.command = function (message) {
if (alreadyJoined) { if (alreadyJoined) {
member.removeRole(role); member.removeRole(role);
message.channel.sendMessage(`${user}'s speech has been revoked in the #development channel.`); message.channel.send(`${user}'s speech has been revoked in the #development channel.`);
} else { } else {
member.addRole(role); member.addRole(role);
message.channel.sendMessage(`${user} has been granted speech in the #development channel.`); message.channel.send(`${user} has been granted speech in the #development channel.`);
} }
}); });
} }

View File

@ -7,5 +7,5 @@ exports.command = function (message, reply) {
replyMessage = `${message.mentions.users.map(user => `${user}`)} ${reply}`; replyMessage = `${message.mentions.users.map(user => `${user}`)} ${reply}`;
} }
message.channel.sendMessage(replyMessage); message.channel.send(replyMessage);
}; };

View File

@ -1,24 +0,0 @@
const state = require('../state.js');
const data = require('../data.js');
const logger = require('../logging.js');
const UserWarning = require('../models/UserWarning.js');
exports.roles = ['Admins', 'Moderators'];
exports.command = function (message) {
message.mentions.users.map((user) => {
var count = state.warnings.filter(x => x.id === user.id && !x.cleared).length || 0;
message.channel.sendMessage(`${user} You have been warned. Additional infractions may result in a ban.`);
logger.info(`${message.author.username} ${message.author} has warned ${user.username} ${user} [${count} + 1].`);
state.logChannel.sendMessage(`${message.author} has warned ${user} [${count} + 1].`);
state.warnings.push(new UserWarning(user.id, user.username, message.author.id, message.author.username, count));
data.flushWarnings();
state.stats.warnings += 1;
if (count + 1 >= 3) {
state.logChannel.sendMessage(`.ban ${user}`);
}
});
};

View File

@ -1,8 +0,0 @@
const state = require('../state.js');
exports.command = function (message) {
message.mentions.users.map((user) => {
var warnings = state.warnings.filter(x => x.id === user.id && !x.cleared);
message.channel.sendMessage(`${user}, you have ${warnings.length} total warnings.`);
});
};

View File

@ -1,49 +0,0 @@
const fs = require('fs');
const state = require('./state.js');
const logger = require('./logging.js');
function readWarnings () {
// Load the warnings file into the bans variable.
fs.readFile('/data/discordWarnings.json', 'utf8', function (err, data) {
if (err) { throw err; }
state.warnings = JSON.parse(data);
logger.debug('Loaded warnings file.');
});
}
function readBans () {
// Load the ban file into the bans variable.
fs.readFile('/data/discordBans.json', 'utf8', function (err, data) {
if (err) { throw err; }
state.bans = JSON.parse(data);
logger.debug('Loaded bans file.');
});
}
function readCustomResponses()
{
// Load the responses file into the responses variable.
fs.readFile('/data/responses.json', 'utf8', function (err, data) {
if (err) { throw err; }
state.responses = JSON.parse(data);
logger.debug('Loaded responses file from external source.');
});
}
function flushWarnings () {
var warningsJson = JSON.stringify(state.warnings, null, 4);
if (!fs.existsSync('./data/')) fs.mkdirSync('data');
fs.writeFile('/data/discordWarnings.json', warningsJson, 'utf8', function (err) {
if (err) { logger.error(err); }
});
}
function flushBans () {
var bansJson = JSON.stringify(state.bans, null, 4);
if (!fs.existsSync('data')) fs.mkdirSync('data');
fs.writeFile('/data/discordBans.json', bansJson, 'utf8', function (err) {
if (err) { logger.error(err); }
});
}
module.exports = { readWarnings: readWarnings, readBans: readBans, readCustomResponses: readCustomResponses, flushWarnings: flushWarnings, flushBans: flushBans };

View File

@ -1,12 +0,0 @@
class UserBan {
constructor (id, username, warnedBy, warnedByUsername, priorWarnings) {
this.id = id;
this.username = username;
this.date = new Date();
this.warnedBy = warnedBy;
this.warnedByUsername = warnedByUsername;
this.priorWarnings = priorWarnings;
}
}
module.exports = UserBan;

View File

@ -1,12 +0,0 @@
class UserWarning {
constructor (id, username, warnedBy, warnedByUsername, priorWarnings) {
this.id = id;
this.username = username;
this.date = new Date();
this.warnedBy = warnedBy;
this.warnedByUsername = warnedByUsername;
this.priorWarnings = priorWarnings;
}
}
module.exports = UserWarning;

View File

@ -8,16 +8,23 @@ const fs = require('fs');
const logger = require('./logging.js'); const logger = require('./logging.js');
const state = require('./state.js'); const state = require('./state.js');
const data = require('./data.js');
state.responses = require('./responses.json'); // Load custom responses
if (process.env.DATA_CUSTOM_RESPONSES) {
fs.readFile('/data/responses.json', 'utf8', function (err, data) {
if (err) { throw err; }
state.responses = JSON.parse(data);
logger.debug('Loaded responses file from external source.');
});
} else {
state.responses = require('./responses.json');
logger.warn(`Loaded resources file from the default, no external source found.`);
}
var cachedModules = []; var cachedModules = [];
var cachedTriggers = []; var cachedTriggers = [];
var client = new discord.Client(); var client = new discord.Client();
logger.info('Application startup. Configuring environment.');
process.on('unhandledRejection', (error, promise) => { process.on('unhandledRejection', (error, promise) => {
logger.error(`Unhandled promise rejection: ${error.message}.`, error); logger.error(`Unhandled promise rejection: ${error.message}.`, error);
}); });
@ -52,14 +59,15 @@ client.on('guildMemberRemove', (member) => {
// Output the stats for state.stats every 24 hours. // Output the stats for state.stats every 24 hours.
// Server is in UTC mode, 11:30 EST would be 03:30 UTC. // Server is in UTC mode, 11:30 EST would be 03:30 UTC.
schedule.scheduleJob({ hour: 3, minute: 30 }, function () { 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.`); 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.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.`); meta: { stats: state.stats }
});
state.logChannel.send(`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.`);
// Clear the stats for the day. // Clear the stats for the day.
state.stats.joins = 0; state.stats.joins = 0;
state.stats.ruleAccepts = 0; state.stats.ruleAccepts = 0;
state.stats.leaves = 0; state.stats.leaves = 0;
state.stats.warnings = 0;
}); });
client.on('message', message => { client.on('message', message => {
@ -68,7 +76,7 @@ client.on('message', message => {
if (message.guild == null && state.responses.pmReply) { if (message.guild == null && state.responses.pmReply) {
// We want to log PM attempts. // We want to log PM attempts.
logger.info(`${message.author.username} ${message.author} [PM]: ${message.content}`); logger.info(`${message.author.username} ${message.author} [PM]: ${message.content}`);
state.logChannel.sendMessage(`${message.author} [PM]: ${message.content}`); state.logChannel.send(`${message.author} [PM]: ${message.content}`);
message.reply(state.responses.pmReply); message.reply(state.responses.pmReply);
return; return;
} }
@ -101,7 +109,7 @@ client.on('message', message => {
if (cachedModule) { if (cachedModule) {
// Check access permissions. // Check access permissions.
if (cachedModule.roles !== undefined && findArray(message.member.roles.map(function (x) { return x.name; }), cachedModule.roles) === false) { 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}`); state.logChannel.send(`${message.author} attempted to use admin command: ${message.content}`);
logger.info(`${message.author.username} ${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; return false;
} }
@ -116,21 +124,6 @@ client.on('message', message => {
cachedModules['quote.js'].command(message, cachedModule.reply); cachedModules['quote.js'].command(message, cachedModule.reply);
} }
} catch (err) { logger.error(err); } } 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.
} }
} else if (message.author.bot === false) { } else if (message.author.bot === false) {
// This is a normal channel message. // This is a normal channel message.
@ -175,13 +168,5 @@ fs.readdirSync('./src/triggers/').forEach(function (file) {
} }
}); });
data.readWarnings();
data.readBans();
// Load custom responses
if (process.env.DATA_CUSTOM_RESPONSES) {
data.readCustomResponses();
}
client.login(process.env.DISCORD_LOGIN_TOKEN); client.login(process.env.DISCORD_LOGIN_TOKEN);
logger.info('Startup completed. Established connection to Discord.'); logger.info('Startup completed. Established connection to Discord.');

View File

@ -2,14 +2,11 @@
var State = function () { var State = function () {
this.guild = null; this.guild = null;
this.logChannel = null; this.logChannel = null;
this.warnings = [];
this.responses = null; this.responses = null;
this.bans = [];
this.stats = { this.stats = {
joins: 0, joins: 0,
ruleAccepts: 0, ruleAccepts: 0,
leaves: 0, leaves: 0
warnings: 0
}; };
}; };

View File

@ -35,7 +35,7 @@ exports.execute = function (message) {
// Set path to type of comment (issues/pull) // Set path to type of comment (issues/pull)
let path = response.request.uri.pathname.split('/')[3]; let path = response.request.uri.pathname.split('/')[3];
message.channel.sendMessage(`Github ${map[path]}: ${url}`); message.channel.send(`Github ${map[path]}: ${url}`);
} }
}); });