Skip to content

Commit

Permalink
Allow commandCharacters to work for messages sent to Discord
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Throne3d committed Apr 13, 2017
1 parent 884f0ca commit 4120c59
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 18 deletions.
47 changes: 31 additions & 16 deletions lib/bot.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -239,6 +240,7 @@ class Bot {
}

const patternMap = {
author: nickname,
nickname,
displayUsername,
text,
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -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
Expand Down
26 changes: 24 additions & 2 deletions test/bot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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 };
Expand Down Expand Up @@ -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();
Expand All @@ -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 });
Expand Down

0 comments on commit 4120c59

Please sign in to comment.