Repository: reactiflux/discord-irc
Pull request #508 (2019-08-26 15:45:47)

ignoreUsers with discord id 
An attempt to implement suggestion from #489

Works for me, tests and style checks pass.

Throne3d: This looks really good, thank you! Would you be able to rebase this onto master? Sorry to have taken so long to review it.
Throne3d: Actually, I'll see if a merge and a squash works! I've had trouble with lots of merges and rebases in the past, and the GitHub interface doesn't seem to let me rebase it and check it can merge directly, so let's see how goes.

Patch:
diff --git a/README.md b/README.md
index 97fc9bc..cef053c 100644
--- a/README.md
+++ b/README.md
@@ -114,7 +114,8 @@ First you need to create a Discord bot user, which you can do by following the i
     "ircStatusNotices": true, // Enables notifications in Discord when people join/part in the relevant IRC channel
     "ignoreUsers": {
       "irc": ["irc_nick1", "irc_nick2"], // Ignore specified IRC nicks and do not send their messages to Discord.
-      "discord": ["discord_nick1", "discord_nick2"] // Ignore specified Discord nicks and do not send their messages to IRC.
+      "discord": ["discord_nick1", "discord_nick2"], // Ignore specified Discord nicks and do not send their messages to IRC.
+      "discordIds": ["198528216523210752"] // Ignore specified Discord ids and do not send their messages to IRC.
     },
     // List of webhooks per channel
     "webhooks": {
diff --git a/lib/bot.js b/lib/bot.js
index fa22536..0f6768e 100644
--- a/lib/bot.js
+++ b/lib/bot.js
@@ -47,6 +47,7 @@ class Bot {
     this.ignoreUsers = options.ignoreUsers || {};
     this.ignoreUsers.irc = this.ignoreUsers.irc || [];
     this.ignoreUsers.discord = this.ignoreUsers.discord || [];
+    this.ignoreUsers.discordIds = this.ignoreUsers.discordIds || [];
 
     // "{$keyName}" => "variableValue"
     // author/nickname: nickname of the user who sent the message
@@ -296,8 +297,12 @@ class Bot {
     return this.ignoreUsers.irc.some(i => i.toLowerCase() === user.toLowerCase());
   }
 
-  ignoredDiscordUser(user) {
-    return this.ignoreUsers.discord.some(i => i.toLowerCase() === user.toLowerCase());
+  ignoredDiscordUser(discordUser) {
+    const ignoredName = this.ignoreUsers.discord.some(
+      i => i.toLowerCase() === discordUser.username.toLowerCase()
+    );
+    const ignoredId = this.ignoreUsers.discordIds.some(i => i === discordUser.id);
+    return ignoredName || ignoredId;
   }
 
   static substitutePattern(message, patternMapping) {
@@ -312,7 +317,7 @@ class Bot {
     ) return;
 
     // Do not send to IRC if this user is on the ignore list.
-    if (this.ignoredDiscordUser(author.username)) {
+    if (this.ignoredDiscordUser(author)) {
       return;
     }
 


Test patch:
diff --git a/test/bot.test.js b/test/bot.test.js
index f6f68b2..5952464 100644
--- a/test/bot.test.js
+++ b/test/bot.test.js
@@ -1167,6 +1167,26 @@ describe('Bot', function () {
         guild: this.guild
       };
 
+      this.bot.sendToIRC(message);
+      ClientStub.prototype.say.should.not.have.been.called;
+    }
+  );
+  it(
+    'should not send messages to IRC if Discord user is ignored by id',
+    function () {
+      const message = {
+        content: 'text',
+        mentions: { users: [] },
+        channel: {
+          name: 'discord'
+        },
+        author: {
+          username: 'vasya_pupkin',
+          id: '4499'
+        },
+        guild: this.guild
+      };
+
       this.bot.sendToIRC(message);
       ClientStub.prototype.say.should.not.have.been.called;
     }
diff --git a/test/fixtures/single-test-config.json b/test/fixtures/single-test-config.json
index ece54ff..c130abc 100644
--- a/test/fixtures/single-test-config.json
+++ b/test/fixtures/single-test-config.json
@@ -21,6 +21,7 @@
   },
   "ignoreUsers": {
     "irc": ["irc_ignored_user"],
-    "discord": ["discord_ignored_user"]
+    "discord": ["discord_ignored_user"],
+    "discordIds": ["4499"]
   }
 }