Skip to content
This repository has been archived by the owner on Aug 27, 2018. It is now read-only.

Commit

Permalink
Monitors addition (#71)
Browse files Browse the repository at this point in the history
* Monitors Loading && Monitors Running

* Message Event ignores bot messages and runs monitors

* Profanity- 1st Monitor Example

* Ignores Bots, Outputs Error Stacks into console

* Semver minor bump

Monitors

* removal of max-len from linter

* oops. I mean semver patch
  • Loading branch information
UnseenFaith authored and CyberiumShadow committed Nov 19, 2016
1 parent 4ca7374 commit b396018
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"guard-for-in": "warn",
"consistent-return": ["warn", { "treatUndefinedAsUnspecified": true }],
"no-use-before-define": ["warn", { "functions": true, "classes": true }],
"no-eval": "warn"
"no-eval": "warn",
"max-len": "off"
}
}
7 changes: 6 additions & 1 deletion app.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ exports.start = (config) => {
client.commands = new Discord.Collection();
client.aliases = new Discord.Collection();
client.commandInhibitors = new Discord.Collection();
client.commandMonitors = new Discord.Collection();
client.dataProviders = new Discord.Collection();

client.coreBaseDir = `${__dirname}/`;
Expand All @@ -25,6 +26,7 @@ exports.start = (config) => {
client.funcs.loadDataProviders(client);
client.funcs.loadCommands(client);
client.funcs.loadCommandInhibitors(client);
client.funcs.loadCommandMonitors(client);
client.funcs.loadEvents(client);
});

Expand All @@ -42,8 +44,10 @@ exports.start = (config) => {
});

client.on("message", (msg) => {
if (msg.author.bot) return;
const conf = client.funcs.confs.get(msg.guild);
msg.guildConf = conf;
client.funcs.runCommandMonitors(client, msg).catch(reason => msg.channel.sendMessage(reason).catch(console.error));
if (!msg.content.startsWith(conf.prefix) && !client.config.prefixMention.test(msg.content)) return;
let prefixLength = conf.prefix.length;
if (client.config.prefixMention.test(msg.content)) prefixLength = client.config.prefixMention.exec(msg.content)[0].length + 1;
Expand Down Expand Up @@ -74,7 +78,8 @@ exports.start = (config) => {
})
.catch((reason) => {
if (reason) {
msg.channel.sendMessage(reason).catch(console.error);
client.funcs.log(reason.stack, 'error');
msg.channel.sendCode(reason).catch(console.error);
}
});
});
Expand Down
58 changes: 58 additions & 0 deletions functions/loadCommandMonitors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const fs = require("fs-extra");
const path = require("path");

module.exports = (client) => {
client.commandMonitors.clear();
const counts = [0, 0];
loadCommandMonitors(client, client.coreBaseDir, counts).then((counts) => {
loadCommandMonitors(client, client.clientBaseDir, counts).then((counts) => {
const [p, o] = counts;
client.funcs.log(`Loaded ${p} command monitors, with ${o} optional.`);
});
});
};

const loadCommandMonitors = (client, baseDir, counts) => {
return new Promise((resolve, reject) => {
const dir = path.resolve(`${baseDir}./monitors/`);
fs.ensureDir(dir, (err) => {
if (err) console.error(err);
fs.readdir(dir, (err, files) => {
if (err) console.error(err);
let [p, o] = counts;
try {
files = files.filter((f) => { return f.slice(-3) === ".js"; });
files.forEach((f) => {
const file = f.split(".");
let props;
if (file[1] !== "opt") {
props = require(`${dir}/${f}`);
client.commandMonitors.set(file[0], props);
p++;
} else if (client.config.commandMonitors.includes(file[0])) {
props = require(`${dir}/${f}`);
client.commandMonitors.set(file[0], props);
o++;
}
});
} catch (e) {
if (e.code === "MODULE_NOT_FOUND") {
const module = /'[^']+'/g.exec(e.toString());
client.funcs.installNPM(module[0].slice(1, -1))
.then(() => {
client.funcs.loadCommandMonitors(client);
})
.catch((e) => {
console.error(e);
process.exit();
});
} else {
console.error(e);
}
reject();
}
resolve([p, o]);
});
});
});
};
16 changes: 16 additions & 0 deletions functions/runCommandMonitors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module.exports = (client, msg) => {
return new Promise((resolve, reject) => {
const mps = [true];
client.commandMonitors.forEach((mProc) => {
if (mProc.conf.enabled) {
mps.push(mProc.run(client, msg));
}
});
Promise.all(mps)
.then((value) => {
resolve(value);
}, (reason) => {
reject(reason);
});
});
};
19 changes: 19 additions & 0 deletions monitors/profanity.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const profanityFinder = require("profanity-finder");

const findProfanity = profanityFinder.findprofanity;

exports.conf = {
enabled: false,
};

exports.run = (client, msg) => {
return new Promise((resolve, reject) => {
const bool = findProfanity(msg.content);
if (bool) {
msg.delete();
reject("You're not allowed to use profanity in your messages!");
} else {
resolve();
}
});
};
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "komada",
"version": "0.8.9",
"version": "0.8.10",
"author": "Evelyne Lachance",
"description": "Komada: Croatian for 'pieces', is a modular bot system including reloading modules and easy to use custom commands.",
"main": "app.js",
Expand Down

0 comments on commit b396018

Please sign in to comment.