1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- const db = require("../db.js");
- const util = require("../util.js");
- const commands = {
- "make guide": msg => {
- if (!util.isAuthorised(msg.member)) return;
- let content = msg.content.substring(msg.content.indexOf("make guide") + "make guide ".length);
- let guideName = content.substring(0, content.indexOf("\n")).trim();
- let guideContent = content.substring(content.indexOf("\n")).trim();
- let guide = db.get("guides").find({
- name: guideName
- });
- if (!guide.isUndefined().value()) {
- guide.assign({
- content: guideContent
- }).write();
- } else {
- db.get("guides")
- .push({
- name: guideName,
- content: guideContent
- })
- .write();
- }
- msg.channel.send(
- `${msg.author.toString()} Added/updated "${guideName}"!`
- );
- },
- "delete guide": (msg, s) => {
- if (!util.isAuthorised(msg.member)) return;
- let guideName = s.substring("delete guide ".length).trim();
- let val = db.get("guides").find({
- name: guideName
- });
- if (val.isUndefined().value()) {
- msg.channel.send(`${msg.author.toString()} No guide "${guideName}"!`);
- return;
- }
- db.get("guides")
- .remove({
- name: guideName
- })
- .write();
- msg.channel.send(
- `${msg.author.toString()} Removed guide "${guideName}"!`
- );
- }
- };
- const onDirectMention = (msg, content, actionsDone) => {
- if (actionsDone)
- return false;
- let parts = content.trim().split(" ");
- let guide = db
- .get("guides")
- .map(g => Object.assign({
- parts: g.name.toLowerCase().split(" ")
- }, g))
- .sortBy(g => g.parts.length)
- .maxBy(k => db._.intersection(parts, k.parts).length)
- .value();
-
- let hits =
- guide !== undefined &&
- db._.intersection(guide.name.toLowerCase().split(" "), parts).length > 0;
-
- if (hits) {
- msg.channel.send(guide.content);
- return true;
- }
- return false;
- };
- module.exports = {
- commands: commands,
- onDirectMention: onDirectMention
- };
|