Browse Source

Change `guides` to array. Use best match for term searching

denikson 6 năm trước cách đây
mục cha
commit
36c00208ab
1 tập tin đã thay đổi với 41 bổ sung34 xóa
  1. 41 34
      main.js

+ 41 - 34
main.js

@@ -36,7 +36,7 @@ db.defaults({
         "335898304472678402"
     ],
     reactableMentionedUsers: ["307897683849510912", "335898304472678402"],
-    guides: {},
+    guides: [],
     editors: {
         roles: ["484046157971193866", "305844721622712322"],
         users: [
@@ -74,9 +74,20 @@ const commands = {
         let content = s.substring("make guide ".length);
         let guideName = content.substring(0, content.indexOf("\n")).trim();
         let guideContent = content.substring(content.indexOf("\n")).trim();
-        db.get("guides")
-            .set(guideName, guideContent)
-            .write();
+
+        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}"!`
         );
@@ -84,19 +95,15 @@ const commands = {
     "delete guide": (msg, s) => {
         if (!isAuthorised(msg.member)) return;
         let guideName = s.substring("delete guide ".length).trim();
-        if (
-            !db
-                .get("guides")
-                .has(guideName)
-                .value()
-        ) {
-            msg.channel.send(
-                `${msg.author.toString()} No guide "${guideName}"!`
-            );
+        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")
-            .unset(guideName)
+            .remove({ name: guideName })
             .write();
         msg.channel.send(
             `${msg.author.toString()} Removed guide "${guideName}!"`
@@ -105,9 +112,9 @@ const commands = {
     help: msg => {
         let guides = db
             .get("guides")
-            .keys()
-            .value()
-            .reduce((p, c) => `${p}\n${c}`, "");
+            .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 commands:\n\`\`\`${guides}\`\`\``
         );
@@ -122,13 +129,7 @@ client.on("ready", () => {
 client.on("message", m => {
     if (m.author.id == client.user.id || m.mentions.users.size == 0) return;
 
-    if (
-        !db
-            .get("reactableMentionedUsers")
-            .intersectionWith(m.mentions.users.map(u => u.id))
-            .isEmpty()
-            .value()
-    ) {
+    if (!db.get("reactableMentionedUsers").intersectionWith(m.mentions.users.map(u => u.id)).isEmpty().value()) {
         const emoteId = db
             .get("emotes")
             .get("angery")
@@ -139,9 +140,9 @@ client.on("message", m => {
     }
 
     if (m.mentions.users.first().id == client.user.id) {
-        let content = m.cleanContent.substring(
-            `@${client.user.username} `.length
-        ).toLowerCase();
+        let content = m.cleanContent
+            .substring(`@${client.user.username} `.length)
+            .toLowerCase();
 
         for (let c in commands) {
             if (content.startsWith(c)) {
@@ -150,13 +151,19 @@ client.on("message", m => {
             }
         }
 
-        let guide = db
-            .get("guides")
-            .get(content.trim())
-            .value();
-        if (guide) {
-            m.channel.send(guide);
-            return;
+        if (content.length > 0) {
+            let parts = content.trim().split(" ");
+            let guide = db
+                .get("guides")
+                .maxBy(k => db._.intersection(parts, k.name.split(" ")).length)
+                .value();
+            let hits =
+                guide !== undefined &&
+                db._.intersection(guide.name.split(" "), parts).length > 0;
+            if (hits) {
+                m.channel.send(guide.content);
+                return;
+            }
         }
 
         const emoteType = db