Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add /names command to Discord #539

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,23 @@ 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.toLowerCase() == '/names') {
const channelName = `#${message.channel.name}`;
const ircChannel = this.channelMapping[message.channel.id] || this.channelMapping[channelName];
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);
}
});

this.ircClient.on('message', this.sendToDiscord.bind(this));
Expand All @@ -170,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 {
Expand All @@ -187,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) {
Expand All @@ -211,26 +225,27 @@ 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]) {
logger.warn(`No channelUsers found for ${channel} when ${nick} quit, ignoring.`);
return;
}
if (!this.channelUsers[channel].delete(nick)) return;
if (!this.ircStatusNotices) return;
this.sendExactToDiscord(channel, `*${nick}* has quit (${reason})`);
});
});

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));
});
Expand Down