diff --git a/lib/bot.js b/lib/bot.js index 8417047a..fd38a650 100644 --- a/lib/bot.js +++ b/lib/bot.js @@ -37,24 +37,25 @@ class Bot { this.ircStatusNotices = options.ircStatusNotices; this.announceSelfJoin = options.announceSelfJoin; - this.format = options.format || {}; // "{$keyName}" => "variableValue" - // nickname: discord nickname - // displayUsername: nickname with wrapped colors - // text: the (IRC-formatted) message content + // author/nickname: nickname of the user who sent the message // discordChannel: Discord channel (e.g. #general) // ircChannel: IRC channel (e.g. #irc) + // text: the (appropriately formatted) message content + this.format = options.format || {}; + + // "{$keyName}" => "variableValue" + // displayUsername: nickname with wrapped colors // attachmentURL: the URL of the attachment (only applicable in formatURLAttachment) - this.formatCommandPrelude = this.format.commandPrelude || 'Command sent from Discord by {$nickname}:'; this.formatIRCText = this.format.ircText || '<{$displayUsername}> {$text}'; this.formatURLAttachment = this.format.urlAttachment || '<{$displayUsername}> {$attachmentURL}'; // "{$keyName}" => "variableValue" - // author: IRC nickname - // text: the (Discord-formatted) message content + // side: "Discord" or "IRC" + this.formatCommandPrelude = this.format.commandPrelude || 'Command sent from {$side} by {$nickname}:'; + + // "{$keyName}" => "variableValue" // withMentions: text with appropriate mentions reformatted - // discordChannel: Discord channel (e.g. #general) - // ircChannel: IRC channel (e.g. #irc) this.formatDiscord = this.format.discord || '**<{$author}>** {$withMentions}'; // Keep track of { channel => [list, of, usernames] } for ircStatusNotices @@ -239,6 +240,7 @@ class Bot { } const patternMap = { + author: nickname, nickname, displayUsername, text, @@ -247,7 +249,9 @@ class Bot { }; if (this.isCommandMessage(text)) { + patternMap.side = 'Discord'; const prelude = Bot.substitutePattern(this.formatCommandPrelude, patternMap); + logger.debug('Sending command message to IRC', ircChannel, text); this.ircClient.say(ircChannel, prelude); this.ircClient.say(ircChannel, text); } else { @@ -299,6 +303,23 @@ class Bot { // Convert text formatting (bold, italics, underscore) const withFormat = formatFromIRCToDiscord(text); + const patternMap = { + author, + nickname: author, + text: withFormat, + discordChannel: `#${discordChannel.name}`, + ircChannel: channel + }; + + if (this.isCommandMessage(text)) { + patternMap.side = 'IRC'; + const prelude = Bot.substitutePattern(this.formatCommandPrelude, patternMap); + logger.debug('Sending command message to Discord', `#${discordChannel.name}`, text); + discordChannel.sendMessage(prelude); + discordChannel.sendMessage(text); + return; + } + const withMentions = withFormat.replace(/@[^\s]+\b/g, (match) => { const search = match.substring(1); const guild = discordChannel.guild; @@ -320,13 +341,7 @@ class Bot { return match; }); - const patternMap = { - author, - text: withFormat, - withMentions, - discordChannel: `#${discordChannel.name}`, - ircChannel: channel - }; + patternMap.withMentions = withMentions; // Add bold formatting: // Use custom formatting from config / default formatting with bold author diff --git a/test/bot.test.js b/test/bot.test.js index 32d7f737..aaa98e91 100644 --- a/test/bot.test.js +++ b/test/bot.test.js @@ -448,7 +448,7 @@ describe('Bot', function () { this.bot.parseText(message).should.equal('hi hi hi '); }); - it('should hide usernames for commands', function () { + it('should hide usernames for commands to IRC', function () { const text = '!test command'; const guild = createGuildStub(); const message = { @@ -471,6 +471,15 @@ describe('Bot', function () { ClientStub.prototype.say.getCall(1).args.should.deep.equal(['#irc', text]); }); + it('should hide usernames for commands to Discord', function () { + const username = 'ircuser'; + const text = '!command'; + + this.bot.sendToDiscord(username, '#irc', text); + this.sendMessageStub.getCall(0).args.should.deep.equal(['Command sent from IRC by ircuser:']); + this.sendMessageStub.getCall(1).args.should.deep.equal([text]); + }); + it('should use nickname instead of username when available', function () { const text = 'testmessage'; const newConfig = { ...config, ircNickColor: false }; @@ -682,7 +691,7 @@ describe('Bot', function () { this.sendMessageStub.should.have.been.calledWith(expected); }); - it('should respect custom formatting for Discord', function () { + it('should respect custom formatting for regular Discord output', function () { const format = { discord: '<{$author}> {$ircChannel} => {$discordChannel}: {$text}' }; this.bot = new Bot({ ...configMsgFormatDefault, format }); this.bot.connect(); @@ -694,6 +703,19 @@ describe('Bot', function () { this.sendMessageStub.should.have.been.calledWith(expected); }); + it('should respect custom formatting for commands in Discord output', function () { + const format = { commandPrelude: '{$nickname} from {$ircChannel} sent command to {$discordChannel}:' }; + this.bot = new Bot({ ...configMsgFormatDefault, format }); + this.bot.connect(); + + const username = 'test'; + const msg = '!testcmd'; + const expected = 'test from #irc sent command to #discord:'; + this.bot.sendToDiscord(username, '#irc', msg); + this.sendMessageStub.getCall(0).args.should.deep.equal([expected]); + this.sendMessageStub.getCall(1).args.should.deep.equal([msg]); + }); + it('should respect custom formatting for regular IRC output', function () { const format = { ircText: '<{$nickname}> {$discordChannel} => {$ircChannel}: {$text}' }; this.bot = new Bot({ ...configMsgFormatDefault, format });