Procházet zdrojové kódy

Implement basic guide DB, add commands

denikson před 5 roky
rodič
revize
798ec33fb2
4 změnil soubory, kde provedl 134 přidání a 8 odebrání
  1. 1 0
      .gitignore
  2. 3 0
      .prettierrc
  3. 129 8
      main.js
  4. 1 0
      package.json

+ 1 - 0
.gitignore

@@ -30,3 +30,4 @@ node_modules
 
 token.js
 package-lock.json
+db.json

+ 3 - 0
.prettierrc

@@ -0,0 +1,3 @@
+{
+    "tabWidth": 4
+}

+ 129 - 8
main.js

@@ -1,24 +1,145 @@
 const Discord = require("discord.js");
 const TOKEN = require("./token.js");
+const lowdb = require("lowdb");
+const FileSync = require("lowdb/adapters/FileSync");
+
+const adapter = new FileSync("db.json");
+const db = lowdb(adapter);
+
+db._.mixin({
+    randomElement: array => array[Math.floor(Math.random() * array.length)]
+});
+
+db.defaults({
+    emotes: {
+        angery: [
+            "488797492352385034",
+            "488797455899688971",
+            "488796668750200842",
+            "488796396175097876",
+            "488793566982963231",
+            "488793511181811749",
+            "488791172085448718",
+            "489091926474096651"
+        ],
+        hug: [
+            "489094486132260864",
+            "489094428410249218",
+            "489094553148850176",
+            "489094604935790622",
+            "485109695846023209"
+        ]
+    },
+    specialUsers: ["141880968800763905", "307897683849510912", "335898304472678402"],
+    guides: {},
+    editors: {
+        roles: ["484046157971193866", "305844721622712322"],
+        users: ["307897683849510912", "170116346095468545"]
+    }
+}).write();
 
 const client = new Discord.Client();
-const PING_EMOTES_COUNT = 7;
 
-function getPingEmoteId() {
-    return Math.floor(Math.random() * PING_EMOTES_COUNT);
+function isAuthorised(member) {
+    if(db.get("editors.users").includes(member.id).value())
+        return true;
+    if(db.get("editors.roles").intersectionWith(member.roles.keyArray()).isEmpty().value())
+        return false;
+    return true;
 }
 
+const commands = {
+    "make guide": (msg, s) => {
+        if(!isAuthorised(msg.member))
+            return;
+        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();
+        msg.channel.send(
+            `${msg.author.toString()} Added/updated "${guideName}"!`
+        );
+    },
+    "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}"!`
+            );
+            return;
+        }
+        db.get("guides")
+            .unset(guideName)
+            .write();
+        msg.channel.send(
+            `${msg.author.toString()} Removed guide "${guideName}!"`
+        );
+    },
+    help: msg => {
+        let guides = db
+            .get("guides")
+            .keys()
+            .value()
+            .reduce((p, c) => `${p}\n${c}`, "");
+        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}\`\`\``
+        );
+    }
+};
+
 client.on("ready", () => {
     console.log("Ready!");
 });
 
 client.on("message", m => {
-    if (m.mentions.users.some(u => u.id == client.user.id)) {
-        const emote = `AngeryPing${getPingEmoteId()}`;
-        m.channel.send(client.emojis.find(e => e.name == emote).toString());
+    if (m.mentions.users.size == 0) return;
+    if (m.mentions.users.first().id == client.user.id) {
+        let content = m.cleanContent.substring(
+            `@${client.user.username} `.length
+        );
+
+        for (let c in commands) {
+            if (content.startsWith(c)) {
+                commands[c](m, content);
+                return;
+            }
+        }
+
+        let guide = db
+            .get("guides")
+            .get(content.trim())
+            .value();
+        if (guide) {
+            m.channel.send(guide);
+            return;
+        }
+
+        const emoteType = db
+            .get("specialUsers")
+            .includes(m.author.id)
+            .value()
+            ? "hug"
+            : "angery";
+        const id = db
+            .get("emotes")
+            .get(emoteType)
+            .randomElement()
+            .value();
+        m.channel.send(client.emojis.find(e => e.id == id).toString());
     } else if (m.content.includes("Noct")) {
-        m.channel.send(client.emojis.find(e => e.name == "mukuNeighWaaaaaa").toString());
+        m.channel.send(
+            client.emojis.find(e => e.name == "mukuNeighWaaaaaa").toString()
+        );
     }
 });
 
-client.login(TOKEN);
+client.login(TOKEN);

+ 1 - 0
package.json

@@ -21,6 +21,7 @@
   "license": "MIT",
   "dependencies": {
     "discord.js": "^11.4.2",
+    "lowdb": "^1.0.0",
     "uws": "^99.0.0"
   },
   "devDependencies": {