Explorar o código

Add quotes feature

denikson %!s(int64=6) %!d(string=hai) anos
pai
achega
e9f6d5fe39
Modificáronse 3 ficheiros con 74 adicións e 2 borrados
  1. 70 0
      commands/quote.js
  2. 2 1
      commands/react.js
  3. 2 1
      db.js

+ 70 - 0
commands/quote.js

@@ -0,0 +1,70 @@
+const db = require("../db.js");
+const util = require("../util.js");
+
+const quotePattern = /add quote by "([^"]+)"\s*(.*)/i;
+
+function minify(str, maxLength) {
+    let result = str.replace("\n", "");
+    if(result.length > maxLength)
+        result = `${result.substring(0, maxLength - 3)}...`;
+    return result;
+}
+
+const commands = {
+    "add quote": (msg, c) => {
+        if(!util.isAuthorised(msg.author))
+            return;
+
+        let result = quotePattern.exec(c);
+        
+        if(result == null)
+            return;
+
+        let author = result[1].trim();
+        let message = result[2].trim();
+
+        db.get("quotes").push({
+            author: author,
+            message: message
+        }).write();
+
+        msg.channel.send(`${msg.author.toString()} Added quote #${db.get("quotes").size().value()}!`);
+    },
+    "random quote": (msg) => {
+        if(db.get("quotes").size().value() == 0){
+            msg.channel.send("I have no quotes!");
+            return;
+        }
+        let quote = db.get("quotes").randomElement().value();
+        let index = db.get("quotes").indexOf(quote).value();
+        msg.channel.send(`Quote #${index + 1}:\n*"${quote.message}"*\n- ${quote.author}`);
+    },
+    "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)
+            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)) {
+            msg.channel.send(`${msg.author.toString()} To prevent spamming, only bot moderators can view all quotes!`);
+            return;        
+        }
+
+        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();
+        msg.channel.send(`${msg.author.toString()}I know the following quotes:\n\`\`\`${quotes}\`\`\``);
+    }
+};
+
+
+module.exports = {
+    commands: commands
+};

+ 2 - 1
commands/react.js

@@ -2,10 +2,11 @@ const db = require("../db.js");
 const util = require("../util.js");
 const client = require("../client.js");
 
+const pattern = /^react to\s+"([^"]+)"\s+with\s+\<:[^:]+:([^\>]+)\>$/i;
+
 const commands = {
     "react to": (msg, s) => {
         if (!util.isAuthorised(msg.member)) return;
-        const pattern = /^react to\s+"([^"]+)"\s+with\s+\<:[^:]+:([^\>]+)\>$/i;
         let contents = pattern.exec(s);
         if (contents != null) {
             let reactable = contents[1].trim().toLowerCase();

+ 2 - 1
db.js

@@ -76,7 +76,8 @@ db.defaults({
         "297109482905796608": 0.25,
         "297117726546198528": 0.1,
         "401837400109875216": 0.1
-    }
+    },
+    quotes: []
 }).write();
 
 module.exports = db;