Skip sending falsy command preludes

Fixes #239 by allowing falsy command preludes to override default, and
then manually skipping them in the sendToIrc and sendToDiscord methods.
This commit is contained in:
Edward Jones 2017-07-01 21:11:59 +01:00
parent 47a5a6c085
commit 1aff7a99ef
2 changed files with 45 additions and 5 deletions

View File

@ -51,7 +51,11 @@ class Bot {
this.formatURLAttachment = this.format.urlAttachment || '<{$displayUsername}> {$attachmentURL}';
// "{$keyName}" => "variableValue"
// side: "Discord" or "IRC"
this.formatCommandPrelude = this.format.commandPrelude || 'Command sent from {$side} by {$nickname}:';
if ('commandPrelude' in this.format) {
this.formatCommandPrelude = this.format.commandPrelude;
} else {
this.formatCommandPrelude = 'Command sent from {$side} by {$nickname}:';
}
// "{$keyName}" => "variableValue"
// withMentions: text with appropriate mentions reformatted
@ -282,9 +286,12 @@ 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);
// if (prelude) this.ircClient.say(ircChannel, prelude);
if (this.formatCommandPrelude) {
const prelude = Bot.substitutePattern(this.formatCommandPrelude, patternMap);
this.ircClient.say(ircChannel, prelude);
}
this.ircClient.say(ircChannel, text);
} else {
if (text !== '') {
@ -345,9 +352,11 @@ class Bot {
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);
if (this.formatCommandPrelude) {
const prelude = Bot.substitutePattern(this.formatCommandPrelude, patternMap);
discordChannel.sendMessage(prelude);
}
discordChannel.sendMessage(text);
return;
}

View File

@ -811,4 +811,35 @@ describe('Bot', function () {
const expected = `<otherauthor> #discord => #irc, attachment: ${attachmentUrl}`;
ClientStub.prototype.say.should.have.been.calledWith('#irc', expected);
});
it('should not bother with command prelude if falsy', function () {
const format = { commandPrelude: null };
this.bot = new Bot({ ...configMsgFormatDefault, format });
this.bot.connect();
const text = '!testcmd';
const guild = createGuildStub();
const message = {
content: text,
mentions: { users: [] },
channel: {
name: 'discord'
},
author: {
username: 'testauthor',
id: 'not bot id'
},
guild
};
this.bot.sendToIRC(message);
ClientStub.prototype.say.should.have.been.calledOnce;
ClientStub.prototype.say.getCall(0).args.should.deep.equal(['#irc', text]);
const username = 'test';
const msg = '!testcmd';
this.bot.sendToDiscord(username, '#irc', msg);
this.sendMessageStub.should.have.been.calledOnce;
this.sendMessageStub.getCall(0).args.should.deep.equal([msg]);
});
});