Skip to content

Commit

Permalink
Merge pull request #322 from anirbanmu/ignore-users-both-ways
Browse files Browse the repository at this point in the history
Add the ability to ignore IRC/Discord users by nickname
  • Loading branch information
ekmartin authored Oct 4, 2017
2 parents 7fbf1d0 + 94d9096 commit 3bd3220
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ First you need to create a Discord bot user, which you can do by following the i
// with one of these characters (commands):
"commandCharacters": ["!", "."],
"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.
}
}
]
```
Expand Down
23 changes: 23 additions & 0 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class Bot {
this.ircStatusNotices = options.ircStatusNotices;
this.announceSelfJoin = options.announceSelfJoin;

// Nicks to ignore
this.ignoreUsers = options.ignoreUsers || {};
this.ignoreUsers.irc = this.ignoreUsers.irc || [];
this.ignoreUsers.discord = this.ignoreUsers.discord || [];

// "{$keyName}" => "variableValue"
// author/nickname: nickname of the user who sent the message
// discordChannel: Discord channel (e.g. #general)
Expand Down Expand Up @@ -263,6 +268,14 @@ class Bot {
return this.commandCharacters.some(prefix => message.startsWith(prefix));
}

ignoredIrcUser(user) {
return this.ignoreUsers.irc.some(i => i.toLowerCase() === user.toLowerCase());
}

ignoredDiscordUser(user) {
return this.ignoreUsers.discord.some(i => i.toLowerCase() === user.toLowerCase());
}

static substitutePattern(message, patternMapping) {
return message.replace(patternMatch, (match, varName) => patternMapping[varName] || match);
}
Expand All @@ -272,6 +285,11 @@ class Bot {
// Ignore messages sent by the bot itself:
if (author.id === this.discord.user.id) return;

// Do not send to IRC if this user is on the ignore list.
if (this.ignoredDiscordUser(author.username)) {
return;
}

const channelName = `#${message.channel.name}`;
const ircChannel = this.channelMapping[message.channel.id] ||
this.channelMapping[channelName];
Expand Down Expand Up @@ -351,6 +369,11 @@ class Bot {
const discordChannel = this.findDiscordChannel(channel);
if (!discordChannel) return;

// Do not send to Discord if this user is on the ignore list.
if (this.ignoredIrcUser(author)) {
return;
}

// Convert text formatting (bold, italics, underscore)
const withFormat = formatFromIRCToDiscord(text);

Expand Down
18 changes: 9 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions test/bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -831,4 +831,29 @@ describe('Bot', function () {
this.sendStub.should.have.been.calledOnce;
this.sendStub.getCall(0).args.should.deep.equal([msg]);
});

it('should not send messages to Discord if IRC user is ignored',
function () {
this.bot.sendToDiscord('irc_ignored_user', '#irc', 'message');
this.sendStub.should.not.have.been.called;
});

it('should not send messages to IRC if Discord user is ignored',
function () {
const message = {
content: 'text',
mentions: { users: [] },
channel: {
name: 'discord'
},
author: {
username: 'discord_ignored_user',
id: 'some id'
},
guild: this.guild
};

this.bot.sendToIRC(message);
ClientStub.prototype.say.should.not.have.been.called;
});
});
4 changes: 4 additions & 0 deletions test/fixtures/single-test-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@
"#discord": "#irc channelKey",
"#notinchannel": "#otherIRC",
"1234": "#channelforid"
},
"ignoreUsers": {
"irc": ["irc_ignored_user"],
"discord": ["discord_ignored_user"]
}
}

0 comments on commit 3bd3220

Please sign in to comment.