Browse Source

Add documentation to all commands

denikson 6 years ago
parent
commit
77028b4e42
6 changed files with 108 additions and 24 deletions
  1. 25 1
      commands/guide.js
  2. 23 9
      commands/help.js
  3. 32 12
      commands/quote.js
  4. 17 1
      commands/react.js
  5. 9 0
      main.js
  6. 2 1
      util.js

+ 25 - 1
commands/guide.js

@@ -1,6 +1,21 @@
 const db = require("../db.js");
 const util = require("../util.js");
 
+const documentation = {
+    "make guide <keywords> <NEWLINE> <content>": {
+        auth: true,
+        description: "Creates a new guide."
+    },
+    "delete guide <keywords>": {
+        auth: true,
+        description: "Deletes a guide."
+    },
+    "guides": {
+        auth: false,
+        description: "Lists all keywords that will trigger a guide."
+    }
+};
+
 const commands = {
     "make guide": msg => {
         if (!util.isAuthorised(msg.member)) return;
@@ -49,6 +64,14 @@ const commands = {
         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}\`\`\``);
     }
 };
 
@@ -79,5 +102,6 @@ const onDirectMention = (msg, content, actionsDone) => {
 
 module.exports = {
     commands: commands,
-    onDirectMention: onDirectMention
+    onDirectMention: onDirectMention,
+    documentation: documentation
 };

+ 23 - 9
commands/help.js

@@ -1,15 +1,29 @@
-const db = require("../db.js");
+const util = require("../util.js");
 
 const commands = {
     "help": msg => {
-        let guides = db
-            .get("guides")
-            .map(g => g.name)
-            .reduce((p, c) => `${p}\n${c}`, "")
-            .value();
-        msg.channel.send(
-            `Hello! I am NoctBot! I have answers for the most common C(O)M-related questions.\nJust ping me with one of the following keywords:\n\`\`\`${guides}\`\`\``
-        );
+        let isAuthed = util.isAuthorised(msg.author);
+
+        let baseCommands = "\n";
+        let modCommands = "\n";
+
+        for(let command in util.documentation) {
+            if(!util.documentation.hasOwnProperty(command))
+                continue;
+            
+            let doc = util.documentation[command];
+            if(isAuthed && doc.auth)
+                modCommands = `${modCommands}${command}  -  ${doc.description}\n`;
+            else if(!doc.auth)
+                baseCommands = `${baseCommands}${command} - ${doc.description}\n`;                
+        }
+
+        let message = `Hello! I am NoctBot! My job is to help with C(O)M-related problems!\nPing me with one of the following commands:\n\`\`\`${baseCommands}\`\`\``;
+        
+        if(isAuthed)
+            message = `${msg.author.toString()} ${message}\n👑**Moderator commands**👑\n\`\`\`${modCommands}\`\`\``;
+
+        msg.channel.send(message);
     }
 };
 

+ 32 - 12
commands/quote.js

@@ -1,23 +1,42 @@
 const db = require("../db.js");
 const util = require("../util.js");
 
+const documentation = {
+    "add quote by \"<author>\" <NEWLINE> <quote>": {
+        auth: true,
+        description: "Adds a quote"
+    },
+    "remove quote <quote_index>": {
+        auth: true,
+        description: "Removes quote. Use \"list quotes\" to get the <quote_index>!"
+    },
+    "quotes": {
+        auth: true,
+        description: "Lists all known quotes."
+    },
+    "random quote": {
+        auth: false,
+        description: "Shows a random quote by someone special..."
+    }
+};
+
 const quotePattern = /add quote by "([^"]+)"\s*(.*)/i;
 
 function minify(str, maxLength) {
     let result = str.replace("\n", "");
-    if(result.length > maxLength)
+    if (result.length > maxLength)
         result = `${result.substring(0, maxLength - 3)}...`;
     return result;
 }
 
 const commands = {
     "add quote": (msg, c) => {
-        if(!util.isAuthorised(msg.author))
+        if (!util.isAuthorised(msg.author))
             return;
 
         let result = quotePattern.exec(c);
-        
-        if(result == null)
+
+        if (result == null)
             return;
 
         let author = result[1].trim();
@@ -31,7 +50,7 @@ const commands = {
         msg.channel.send(`${msg.author.toString()} Added quote #${db.get("quotes").size().value()}!`);
     },
     "random quote": (msg) => {
-        if(db.get("quotes").size().value() == 0){
+        if (db.get("quotes").size().value() == 0) {
             msg.channel.send("I have no quotes!");
             return;
         }
@@ -42,29 +61,30 @@ const commands = {
     "remove quote": (msg, c) => {
         let quoteNum = c.substring("remove quote".length).trim();
         let val = parseInt(quoteNum);
-        if(isNaN(val) || db.get("quotes").size().value() < val - 1)
+        if (isNaN(val) || db.get("quotes").size().value() < val - 1)
             return;
 
         db.get("quotes").pullAt(val - 1).write();
         msg.channel.send(`${msg.author.toString()} Removed quote #${val}!`);
     },
-    "list quotes": msg => {
-        if(!util.isAuthorised(msg.author)) {
+    "quotes": msg => {
+        if (!util.isAuthorised(msg.author)) {
             msg.channel.send(`${msg.author.toString()} To prevent spamming, only bot moderators can view all quotes!`);
-            return;        
+            return;
         }
 
-        if(db.get("quotes").size().value() == 0){
+        if (db.get("quotes").size().value() == 0) {
             msg.channel.send("I have no quotes!");
             return;
         }
 
-        let quotes =  db.get("quotes").reduce((prev, curr, i) => `${prev}[${i+1}] "${minify(curr.message, 10)}" by ${curr.author}\n`, "").value();
+        let quotes = db.get("quotes").reduce((prev, curr, i) => `${prev}[${i+1}] "${minify(curr.message, 10)}" by ${curr.author}\n`, "\n").value();
         msg.channel.send(`${msg.author.toString()}I know the following quotes:\n\`\`\`${quotes}\`\`\``);
     }
 };
 
 
 module.exports = {
-    commands: commands
+    commands: commands,
+    documentation: documentation
 };

+ 17 - 1
commands/react.js

@@ -4,6 +4,21 @@ const client = require("../client.js");
 
 const pattern = /^react to\s+"([^"]+)"\s+with\s+\<:[^:]+:([^\>]+)\>$/i;
 
+const documentation = {
+    "react to \"<message>\" with <emote>": {
+        auth: true,
+        description: "React to <message> with <emote>."
+    },
+    "remove reaction to <message>": {
+        auth: true,
+        description: "Stops reacting to <message>."
+    },
+    "reactions": {
+        auth: false,
+        description: "Lists all known messages this bot can react to."
+    }
+};
+
 const commands = {
     "react to": (msg, s) => {
         if (!util.isAuthorised(msg.member)) return;
@@ -85,5 +100,6 @@ const onIndirectMention = (msg, actionsDone) => {
 module.exports = {
     commands: commands,
     onMessage: onMessage,
-    onIndirectMention: onIndirectMention
+    onIndirectMention: onIndirectMention,
+    documentation: documentation
 };

+ 9 - 0
main.js

@@ -2,6 +2,7 @@ const TOKEN = require("./token.js");
 const fs = require("fs");
 const path = require("path");
 const client = require("./client.js");
+const util = require("./util.js");
 
 const REACT_PROBABILITY = 0.6;
 
@@ -88,6 +89,14 @@ function main() {
                     commands[command] = obj.commands[command];
             }
         }
+
+        if (obj.documentation) {
+            for (let command in obj.documentation) {
+                if (obj.documentation.hasOwnProperty(command))
+                    util.documentation[command] = obj.documentation[command];
+            }
+        }
+
         if (obj.onMessage)
             msgActions.push(obj.onMessage);
 

+ 2 - 1
util.js

@@ -28,5 +28,6 @@ function isAuthorised(member) {
 
 module.exports = {
     isAuthorised: isAuthorised,
-    isValidImage: isValidImage
+    isValidImage: isValidImage,
+    documentation: {}
 };