|
@@ -2,78 +2,131 @@ const db = require("../db.js");
|
|
|
const util = require("../util.js");
|
|
|
|
|
|
const documentation = {
|
|
|
- "make guide <keywords> <NEWLINE> <content>": {
|
|
|
+ "make <guidetype> <NEWLINE>name: <name> <NEWLINE>keywords: <keywords> <NEWLINE>contents: <content>": {
|
|
|
auth: true,
|
|
|
- description: "Creates a new guide."
|
|
|
+ description: "Creates a new guide of the specified type, the specified keywords and content."
|
|
|
},
|
|
|
- "delete guide <keywords>": {
|
|
|
+ "delete <guidetype> <keywords>": {
|
|
|
auth: true,
|
|
|
- description: "Deletes a guide."
|
|
|
+ description: "Deletes a guide of the specified type."
|
|
|
},
|
|
|
"guides": {
|
|
|
auth: false,
|
|
|
- description: "Lists all keywords that will trigger a guide."
|
|
|
+ description: "Lists all guides and keywords that trigger them."
|
|
|
+ },
|
|
|
+ "memes": {
|
|
|
+ auth: false,
|
|
|
+ description: "Lists all memes and keywords that trigger them."
|
|
|
+ },
|
|
|
+ "miscs": {
|
|
|
+ auth: false,
|
|
|
+ description: "Lists all additional keywords the bot reacts to."
|
|
|
}
|
|
|
};
|
|
|
|
|
|
-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();
|
|
|
+const VALID_GUIDE_TYPES = new Set(["meme", "guide", "misc"]);
|
|
|
|
|
|
- let guide = db.get("guides").find({
|
|
|
- name: guideName
|
|
|
- });
|
|
|
+const makePattern = /^make (\w+)\s+name:(.+)\s*keywords:(.+)\s*contents:((.*[\n\r]*)+)$/i;
|
|
|
+const deletePattern = /^delete (\w+)\s+(.+)$/i;
|
|
|
|
|
|
- if (!guide.isUndefined().value()) {
|
|
|
- guide.assign({
|
|
|
- content: guideContent
|
|
|
- }).write();
|
|
|
- } else {
|
|
|
- db.get("guides")
|
|
|
- .push({
|
|
|
- name: guideName,
|
|
|
- content: guideContent
|
|
|
- })
|
|
|
- .write();
|
|
|
- }
|
|
|
+function listGuides(msg, guideType, message){
|
|
|
+ let guides = db
|
|
|
+ .get(guideType)
|
|
|
+ .reduce((p, c) => `${p}\n${c.displayName} -- ${c.name}`, "\n")
|
|
|
+ .value();
|
|
|
+ msg.channel.send(`${msg.author.toString()} ${message}\n\`\`\`${guides}\`\`\`\n\nTo display the guides, ping me with one or more keywords, like \`@NoctBot sybaris com\``);
|
|
|
+}
|
|
|
|
|
|
- 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
|
|
|
- });
|
|
|
+const commands = [
|
|
|
+ {
|
|
|
+ pattern: makePattern,
|
|
|
+ action: (msg, s, match) => {
|
|
|
+ if (!util.isAuthorised(msg.member)) return;
|
|
|
+ let type = match[1].toLowerCase();
|
|
|
+ let name = match[2].trim();
|
|
|
+ let keywords = match[3].trim().toLowerCase();
|
|
|
+ let contents = match[4].trim();
|
|
|
|
|
|
- if (val.isUndefined().value()) {
|
|
|
- msg.channel.send(`${msg.author.toString()} No guide "${guideName}"!`);
|
|
|
- return;
|
|
|
+ if(contents.length == 0){
|
|
|
+ msg.channel.send(
|
|
|
+ `${msg.author.toString()} The guide must have some content!`
|
|
|
+ );
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ if(!VALID_GUIDE_TYPES.has(type)){
|
|
|
+ msg.channel.send(
|
|
|
+ `${msg.author.toString()} The type ${type} is not a valid guide type!`
|
|
|
+ );
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ let typeDB = `${type}s`;
|
|
|
+
|
|
|
+ let guide = db.get(typeDB).find({
|
|
|
+ name: keywords
|
|
|
+ });
|
|
|
+
|
|
|
+ if (!guide.isUndefined().value()) {
|
|
|
+ guide.assign({
|
|
|
+ displayName: name,
|
|
|
+ content: contents
|
|
|
+ }).write();
|
|
|
+ } else {
|
|
|
+ db.get(typeDB)
|
|
|
+ .push({
|
|
|
+ name: keywords,
|
|
|
+ displayName: name,
|
|
|
+ content: contents
|
|
|
+ })
|
|
|
+ .write();
|
|
|
+ }
|
|
|
+
|
|
|
+ msg.channel.send(
|
|
|
+ `${msg.author.toString()} Added/updated "${name}" (keywords \`${keywords}\`)!`
|
|
|
+ );
|
|
|
}
|
|
|
-
|
|
|
- db.get("guides")
|
|
|
- .remove({
|
|
|
- name: guideName
|
|
|
- })
|
|
|
- .write();
|
|
|
- msg.channel.send(
|
|
|
- `${msg.author.toString()} Removed guide "${guideName}"!`
|
|
|
- );
|
|
|
},
|
|
|
- "guides": msg => {
|
|
|
- let guides = db
|
|
|
- .get("guides")
|
|
|
- .map(g => g.name)
|
|
|
- .reduce((p, c) => `${p}\n${c}`, "\n")
|
|
|
- .value();
|
|
|
- msg.channel.send(`${msg.author.toString()} Ping me with one of the following keywords for a guide related to the topic:\n\`\`\`${guides}\`\`\``);
|
|
|
- }
|
|
|
-};
|
|
|
+ {
|
|
|
+ pattern: deletePattern,
|
|
|
+ action: (msg, s, match) => {
|
|
|
+ if (!util.isAuthorised(msg.member)) return;
|
|
|
+ let type = match[1];
|
|
|
+ let keywords = match[2].trim();
|
|
|
+
|
|
|
+ if(!VALID_GUIDE_TYPES.has(type)){
|
|
|
+ msg.channel.send(
|
|
|
+ `${msg.author.toString()} The type ${type} is not a valid guide type!`
|
|
|
+ );
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ let typeDB = `${type}s`;
|
|
|
+
|
|
|
+ let val = db.get(typeDB).find({
|
|
|
+ name: keywords
|
|
|
+ });
|
|
|
+
|
|
|
+ if (val.isUndefined().value()) {
|
|
|
+ msg.channel.send(`${msg.author.toString()} No ${type} "${keywords}"!`);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ db.get(typeDB)
|
|
|
+ .remove({
|
|
|
+ name: keywords
|
|
|
+ })
|
|
|
+ .write();
|
|
|
+
|
|
|
+ msg.channel.send(
|
|
|
+ `${msg.author.toString()} Removed ${type} "${keywords}"!`
|
|
|
+ );
|
|
|
+ }
|
|
|
+ },
|
|
|
+ { pattern: "guides", action: msg => listGuides(msg, "guides", "Here are the guides I have:") },
|
|
|
+ { pattern: "memes", action: msg => listGuides(msg, "memes", "Here are some random memes I have:") },
|
|
|
+ { pattern: "misc", action: msg => listGuides(msg, "misc", "These are some misc stuff I can also do:") },
|
|
|
+];
|
|
|
|
|
|
const onDirectMention = (msg, content, actionsDone) => {
|
|
|
if (actionsDone)
|