Skip to content

Commit 4120c59

Browse files
committed
Allow commandCharacters to work for messages sent to Discord
Fixes #217. - Reorganize the comments describing how to format the custom formatting options - Make 'author' and 'nickname' synonyms in the custom formatting - Add a 'side' parameter for commandPrelude (takes 'Discord' or 'IRC', depending on whether the message was from Discord or IRC) - Add debug messages upon sending command messages - Add similar functionality for command messages sent in IRC to Discord, as from Discord to IRC - Add tests for the above and custom formatting of it
1 parent 884f0ca commit 4120c59

File tree

2 files changed

+55
-18
lines changed

2 files changed

+55
-18
lines changed

lib/bot.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,25 @@ class Bot {
3737
this.ircStatusNotices = options.ircStatusNotices;
3838
this.announceSelfJoin = options.announceSelfJoin;
3939

40-
this.format = options.format || {};
4140
// "{$keyName}" => "variableValue"
42-
// nickname: discord nickname
43-
// displayUsername: nickname with wrapped colors
44-
// text: the (IRC-formatted) message content
41+
// author/nickname: nickname of the user who sent the message
4542
// discordChannel: Discord channel (e.g. #general)
4643
// ircChannel: IRC channel (e.g. #irc)
44+
// text: the (appropriately formatted) message content
45+
this.format = options.format || {};
46+
47+
// "{$keyName}" => "variableValue"
48+
// displayUsername: nickname with wrapped colors
4749
// attachmentURL: the URL of the attachment (only applicable in formatURLAttachment)
48-
this.formatCommandPrelude = this.format.commandPrelude || 'Command sent from Discord by {$nickname}:';
4950
this.formatIRCText = this.format.ircText || '<{$displayUsername}> {$text}';
5051
this.formatURLAttachment = this.format.urlAttachment || '<{$displayUsername}> {$attachmentURL}';
5152

5253
// "{$keyName}" => "variableValue"
53-
// author: IRC nickname
54-
// text: the (Discord-formatted) message content
54+
// side: "Discord" or "IRC"
55+
this.formatCommandPrelude = this.format.commandPrelude || 'Command sent from {$side} by {$nickname}:';
56+
57+
// "{$keyName}" => "variableValue"
5558
// withMentions: text with appropriate mentions reformatted
56-
// discordChannel: Discord channel (e.g. #general)
57-
// ircChannel: IRC channel (e.g. #irc)
5859
this.formatDiscord = this.format.discord || '**<{$author}>** {$withMentions}';
5960

6061
// Keep track of { channel => [list, of, usernames] } for ircStatusNotices
@@ -239,6 +240,7 @@ class Bot {
239240
}
240241

241242
const patternMap = {
243+
author: nickname,
242244
nickname,
243245
displayUsername,
244246
text,
@@ -247,7 +249,9 @@ class Bot {
247249
};
248250

249251
if (this.isCommandMessage(text)) {
252+
patternMap.side = 'Discord';
250253
const prelude = Bot.substitutePattern(this.formatCommandPrelude, patternMap);
254+
logger.debug('Sending command message to IRC', ircChannel, text);
251255
this.ircClient.say(ircChannel, prelude);
252256
this.ircClient.say(ircChannel, text);
253257
} else {
@@ -299,6 +303,23 @@ class Bot {
299303
// Convert text formatting (bold, italics, underscore)
300304
const withFormat = formatFromIRCToDiscord(text);
301305

306+
const patternMap = {
307+
author,
308+
nickname: author,
309+
text: withFormat,
310+
discordChannel: `#${discordChannel.name}`,
311+
ircChannel: channel
312+
};
313+
314+
if (this.isCommandMessage(text)) {
315+
patternMap.side = 'IRC';
316+
const prelude = Bot.substitutePattern(this.formatCommandPrelude, patternMap);
317+
logger.debug('Sending command message to Discord', `#${discordChannel.name}`, text);
318+
discordChannel.sendMessage(prelude);
319+
discordChannel.sendMessage(text);
320+
return;
321+
}
322+
302323
const withMentions = withFormat.replace(/@[^\s]+\b/g, (match) => {
303324
const search = match.substring(1);
304325
const guild = discordChannel.guild;
@@ -320,13 +341,7 @@ class Bot {
320341
return match;
321342
});
322343

323-
const patternMap = {
324-
author,
325-
text: withFormat,
326-
withMentions,
327-
discordChannel: `#${discordChannel.name}`,
328-
ircChannel: channel
329-
};
344+
patternMap.withMentions = withMentions;
330345

331346
// Add bold formatting:
332347
// Use custom formatting from config / default formatting with bold author

test/bot.test.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ describe('Bot', function () {
448448
this.bot.parseText(message).should.equal('hi hi hi ');
449449
});
450450

451-
it('should hide usernames for commands', function () {
451+
it('should hide usernames for commands to IRC', function () {
452452
const text = '!test command';
453453
const guild = createGuildStub();
454454
const message = {
@@ -471,6 +471,15 @@ describe('Bot', function () {
471471
ClientStub.prototype.say.getCall(1).args.should.deep.equal(['#irc', text]);
472472
});
473473

474+
it('should hide usernames for commands to Discord', function () {
475+
const username = 'ircuser';
476+
const text = '!command';
477+
478+
this.bot.sendToDiscord(username, '#irc', text);
479+
this.sendMessageStub.getCall(0).args.should.deep.equal(['Command sent from IRC by ircuser:']);
480+
this.sendMessageStub.getCall(1).args.should.deep.equal([text]);
481+
});
482+
474483
it('should use nickname instead of username when available', function () {
475484
const text = 'testmessage';
476485
const newConfig = { ...config, ircNickColor: false };
@@ -682,7 +691,7 @@ describe('Bot', function () {
682691
this.sendMessageStub.should.have.been.calledWith(expected);
683692
});
684693

685-
it('should respect custom formatting for Discord', function () {
694+
it('should respect custom formatting for regular Discord output', function () {
686695
const format = { discord: '<{$author}> {$ircChannel} => {$discordChannel}: {$text}' };
687696
this.bot = new Bot({ ...configMsgFormatDefault, format });
688697
this.bot.connect();
@@ -694,6 +703,19 @@ describe('Bot', function () {
694703
this.sendMessageStub.should.have.been.calledWith(expected);
695704
});
696705

706+
it('should respect custom formatting for commands in Discord output', function () {
707+
const format = { commandPrelude: '{$nickname} from {$ircChannel} sent command to {$discordChannel}:' };
708+
this.bot = new Bot({ ...configMsgFormatDefault, format });
709+
this.bot.connect();
710+
711+
const username = 'test';
712+
const msg = '!testcmd';
713+
const expected = 'test from #irc sent command to #discord:';
714+
this.bot.sendToDiscord(username, '#irc', msg);
715+
this.sendMessageStub.getCall(0).args.should.deep.equal([expected]);
716+
this.sendMessageStub.getCall(1).args.should.deep.equal([msg]);
717+
});
718+
697719
it('should respect custom formatting for regular IRC output', function () {
698720
const format = { ircText: '<{$nickname}> {$discordChannel} => {$ircChannel}: {$text}' };
699721
this.bot = new Bot({ ...configMsgFormatDefault, format });

0 commit comments

Comments
 (0)