Browse Source

Add reactons to single-emote messages

denikson 6 years ago
parent
commit
9e46fa20fa
3 changed files with 38 additions and 6 deletions
  1. 1 1
      .eslintrc.js
  2. 2 1
      db.js
  3. 35 4
      main.js

+ 1 - 1
.eslintrc.js

@@ -16,7 +16,7 @@ module.exports = {
         ],
         "linebreak-style": [
             "error",
-            "unix"
+            "windows"
         ],
         "quotes": [
             "error",

+ 2 - 1
db.js

@@ -69,7 +69,8 @@ db.defaults({
     postedNewsGuids: {},
     feedOutputs: [
         "422693157692637184"
-    ]
+    ],
+    messageReactions: {}
 }).write();
 
 module.exports = db;

+ 35 - 4
main.js

@@ -92,10 +92,33 @@ const commands = {
             .remove({ name: guideName })
             .write();
         msg.channel.send(
-            `${msg.author.toString()} Removed guide "${guideName}!"`
+            `${msg.author.toString()} Removed guide "${guideName}"!`
         );
     },
-    help: msg => {
+    "react to": (msg, s) => {
+        const pattern = /^react to\s+(\<[^\>]+\>)\s+with\s+\<:[^:]+:([^\>]+)\>$/i;
+        let contents = pattern.exec(s);
+        if(contents != null) {
+            let reactable = contents[1];
+            let reactionEmoji = contents[2];
+            if(!client.emojis.has(reactionEmoji)){
+                msg.channel.send(`${msg.author.toString()} I cannot react with this emoji :(`);
+                return;
+            }
+            db.get("messageReactions").set(reactable, reactionEmoji).write();
+            msg.channel.send(`${msg.author.toString()} Added reaction!`);
+        }
+    },
+    "remove reaction to": (msg, s) => {
+        let content = s.substring("remove reaction to ".length).trim();
+        if(!db.get("messageReactions").has(content).value()) {
+            msg.channel.send(`${msg.author.toString()} No such reaction available!`);
+            return;
+        }
+        db.get("messageReactions").unset(content).write();
+        msg.channel.send(`${msg.author.toString()} Removed reaction!`);
+    },
+    "help": msg => {
         let guides = db
             .get("guides")
             .map(g => g.name)
@@ -114,7 +137,16 @@ client.on("ready", () => {
 });
 
 client.on("message", m => {
-    if (m.author.id == client.user.id || m.mentions.users.size == 0) return;
+    if (m.author.id == client.user.id) return;
+
+    let content = m.cleanContent.trim();
+    if(db.get("messageReactions").has(content).value()) {
+        m.react(client.emojis.get(db.get("messageReactions").get(content).value()));
+        return;
+    }
+
+    if(m.mentions.users.size == 0)
+        return;
 
     if (!db.get("reactableMentionedUsers").intersectionWith(m.mentions.users.map(u => u.id)).isEmpty().value()) {
         const emoteId = db
@@ -127,7 +159,6 @@ client.on("message", m => {
     }
 
     if (m.mentions.users.first().id == client.user.id) {
-        let content = m.cleanContent.trim();
         if(content.startsWith(`@${client.user.username}`)) {
             content = content
                 .substring(`@${client.user.username} `.length);