From 233855175143d0d0e671c138d0cb218054e6b52f Mon Sep 17 00:00:00 2001 From: Kage Date: Mon, 18 May 2020 08:11:22 +0000 Subject: [PATCH 1/3] Add /names command to Discord --- lib/bot.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/lib/bot.js b/lib/bot.js index fa3b1474..58a9e08b 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -159,8 +159,17 @@ class Bot { }); this.discord.on('message', (message) => { - // Ignore bot messages and people leaving/joining - this.sendToIRC(message); + // Show the IRC channel's /names list when asked for in Discord + if (message.content == '/names') { + const channelName = `#${message.channel.name}`; + const ircChannel = this.channelMapping[message.channel.id] || this.channelMapping[channelName]; + const ircNames = this.channelUsers[ircChannel].values(); + const ircNamesArr = new Array(...ircNames); + this.sendExactToDiscord(ircChannel, 'Users in ' + ircChannel + "\n> " + ircNamesArr.join(', ')); + } else { + // Ignore bot messages and people leaving/joining + this.sendToIRC(message); + } }); this.ircClient.on('message', this.sendToDiscord.bind(this)); From 9d512d3e26b3cbc5c066cbaf5fc24298368800ba Mon Sep 17 00:00:00 2001 From: Kage Date: Sun, 7 Jun 2020 11:40:51 +0000 Subject: [PATCH 2/3] Make the code a little more robust, and always populate channelUsers --- lib/bot.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/lib/bot.js b/lib/bot.js index 58a9e08b..6274a51c 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -160,12 +160,18 @@ class Bot { this.discord.on('message', (message) => { // Show the IRC channel's /names list when asked for in Discord - if (message.content == '/names') { + if (message.content.toLowerCase() == '/names') { const channelName = `#${message.channel.name}`; const ircChannel = this.channelMapping[message.channel.id] || this.channelMapping[channelName]; - const ircNames = this.channelUsers[ircChannel].values(); - const ircNamesArr = new Array(...ircNames); - this.sendExactToDiscord(ircChannel, 'Users in ' + ircChannel + "\n> " + ircNamesArr.join(', ')); + if (this.channelUsers[ircChannel]) { + const ircNames = this.channelUsers[ircChannel].values(); + const ircNamesArr = new Array(...ircNames); + this.sendExactToDiscord(ircChannel, 'Users in ' + ircChannel + "\n> " + ircNamesArr.join(', ')); + } else { + _logger.default.warn(`No channelUsers found for ${ircChannel} when /names requested`); + // Pass the command through if channelUsers is empty + this.sendToIRC(message); + } } else { // Ignore bot messages and people leaving/joining this.sendToIRC(message); @@ -239,7 +245,6 @@ class Bot { this.ircClient.on('names', (channelName, nicks) => { logger.debug('Received names:', channelName, nicks); - if (!this.ircStatusNotices) return; const channel = channelName.toLowerCase(); this.channelUsers[channel] = new Set(Object.keys(nicks)); }); From c4f230fec6dcb51739352edebb70f823a7e8da5d Mon Sep 17 00:00:00 2001 From: Kage Date: Sun, 7 Jun 2020 11:57:02 +0000 Subject: [PATCH 3/3] Always update channelUsers everywhere --- lib/bot.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/bot.js b/lib/bot.js index 6274a51c..ba0ce19d 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -185,13 +185,13 @@ class Bot { }); this.ircClient.on('nick', (oldNick, newNick, channels) => { - if (!this.ircStatusNotices) return; channels.forEach((channelName) => { const channel = channelName.toLowerCase(); if (this.channelUsers[channel]) { if (this.channelUsers[channel].has(oldNick)) { this.channelUsers[channel].delete(oldNick); this.channelUsers[channel].add(newNick); + if (!this.ircStatusNotices) return; this.sendExactToDiscord(channel, `*${oldNick}* is now known as ${newNick}`); } } else { @@ -202,18 +202,17 @@ class Bot { this.ircClient.on('join', (channelName, nick) => { logger.debug('Received join:', channelName, nick); - if (!this.ircStatusNotices) return; if (nick === this.ircClient.nick && !this.announceSelfJoin) return; const channel = channelName.toLowerCase(); // self-join is announced before names (which includes own nick) // so don't add nick to channelUsers if (nick !== this.ircClient.nick) this.channelUsers[channel].add(nick); + if (!this.ircStatusNotices) return; this.sendExactToDiscord(channel, `*${nick}* has joined the channel`); }); this.ircClient.on('part', (channelName, nick, reason) => { logger.debug('Received part:', channelName, nick, reason); - if (!this.ircStatusNotices) return; const channel = channelName.toLowerCase(); // remove list of users when no longer in channel (as it will become out of date) if (nick === this.ircClient.nick) { @@ -226,12 +225,13 @@ class Bot { } else { logger.warn(`No channelUsers found for ${channel} when ${nick} parted.`); } + if (!this.ircStatusNotices) return; this.sendExactToDiscord(channel, `*${nick}* has left the channel (${reason})`); }); this.ircClient.on('quit', (nick, reason, channels) => { logger.debug('Received quit:', nick, channels); - if (!this.ircStatusNotices || nick === this.ircClient.nick) return; + if (nick === this.ircClient.nick) return; channels.forEach((channelName) => { const channel = channelName.toLowerCase(); if (!this.channelUsers[channel]) { @@ -239,6 +239,7 @@ class Bot { return; } if (!this.channelUsers[channel].delete(nick)) return; + if (!this.ircStatusNotices) return; this.sendExactToDiscord(channel, `*${nick}* has quit (${reason})`); }); });