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 custom formatting to IRC & Discord output #204

Merged
merged 7 commits into from
Mar 30, 2017

Conversation

Throne3d
Copy link
Collaborator

As requested by #13, it might sometimes be useful to have custom formatting in the output for IRC and Discord.

I'm not sure how exactly to add tests for this, as it seems like it's going to need a separate configuration for (almost) each test, and this doesn't look like it'll fit into the current bot.test.js file very easily. From a brief glance I can't see a 'context' feature in Chai as there is in RSpec, which would allow me to limit the beforeEach to just the tests present, and add separate ones at the end for this. It's possible I'm missing something.

I'm also not too strongly attached to this idea and it might make it harder to implement some things in the future, so if it'd be preferable to drop this, I don't care too strongly. (If people want it desperately enough, they can always just patch these commits on – hopefully it's clearly documenting enough, and it doesn't seem to break current tests.)

I'd appreciate feedback on how to proceed. Thanks.

@coveralls
Copy link

Coverage Status

Coverage decreased (-9.2%) to 88.108% when pulling 017176c on Throne3d:add/custom-formatting into 26626e5 on reactiflux:master.

@Throne3d
Copy link
Collaborator Author

Throne3d commented Mar 25, 2017

I guess I could add a new "describe" context, within the current one, for the tests that use a regular-config bot, and separate out the beforeEach into two bits? (Then use another context for new tests that don't.)

Also: huh, I really wasn't expecting coverage to decrease that much. Is there some easy way for me to extract out that repeated function in the replace? (I guess I could make an object with various properties of the local variables, use variableList[variableName] || match instead of a switch (variableName) with a default: return match?)

@Throne3d
Copy link
Collaborator Author

I've added tests for this (which should hopefully now pass, making this not a WIP), but perhaps instead of creating a new describe block to separate out the default-config bot tests, I should just do as some tests have already and redefine bot with a different config in the tests that need it.

Opinions?

@coveralls
Copy link

Coverage Status

Coverage increased (+0.2%) to 97.531% when pulling 136d6ac on Throne3d:add/custom-formatting into 26626e5 on reactiflux:master.

@Throne3d Throne3d force-pushed the add/custom-formatting branch from 136d6ac to dfe13b1 Compare March 26, 2017 20:57
@Throne3d
Copy link
Collaborator Author

Throne3d commented Mar 26, 2017

Nm, undid the contexting and just manually used const bot in the appropriate tests. (I apologize for how much I'm reworking things on further consideration.)

@coveralls
Copy link

Coverage Status

Coverage increased (+0.2%) to 97.531% when pulling dfe13b1 on Throne3d:add/custom-formatting into 26626e5 on reactiflux:master.

@Throne3d Throne3d changed the title [WIP] Add custom formatting to IRC & Discord output Add custom formatting to IRC & Discord output Mar 26, 2017
lib/bot.js Outdated
@@ -148,6 +168,13 @@ class Bot {
return this.commandCharacters.indexOf(message[0]) !== -1;
}

static substitutePattern(message, patternMapping) {
return message.replace(patternMatch, (match, varName) => {
if (varName in patternMapping) return patternMapping[varName];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could simplify this to (match, varName) => patternMapping[varName] || match?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main reason I didn't was in case the patternMapping was a blank string. I guess this probably isn't going to happen anywhere (unless there was somehow a zero-length discord message? But there's a guard for that), so it should be fine to simplify.

lib/bot.js Outdated
this.formatCommandPrelude = options.formatCommandPrelude || 'Command sent from Discord by {$nickname}:';
this.formatIRCText = options.formatIRCText || '<{$displayUsername}> {$text}';
this.formatURLAttachment = options.formatURLAttachment || '<{$displayUsername}> {$attachmentURL}';
// "{$keyName}" => "variableValue"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, but I think it makes more sense to put these comments above the variables.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense.

lib/bot.js Outdated
@@ -33,6 +34,25 @@ class Bot {
this.ircNickColor = options.ircNickColor !== false; // default to true
this.channels = _.values(options.channelMapping);

this.formatCommandPrelude = options.formatCommandPrelude || 'Command sent from Discord by {$nickname}:';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe create a format object and have these as properties? I.e.

"format": {
  "commandPrelude": "...",
  "ircText": "...",
  "urlAttachment": "..."
}

or something like that? Then just add a this.format = options.format || {}; above?

@ekmartin
Copy link
Member

Looks great, thanks! I've normally been against adding too many configuration options, but I think this could be useful.

@Throne3d Throne3d force-pushed the add/custom-formatting branch from dfe13b1 to 9848a89 Compare March 29, 2017 21:24
@Throne3d
Copy link
Collaborator Author

(Rebased onto master, rebased to move comments, updated to work with the new formatting-bridging thing ({$text} is now relevantly formatted), added a commit to move the formatting configs to a format object, added a commit to simplify substitutePattern.)

@coveralls
Copy link

coveralls commented Mar 29, 2017

Coverage Status

Coverage increased (+0.1%) to 97.938% when pulling 9848a89 on Throne3d:add/custom-formatting into 9915db1 on reactiflux:master.

@ekmartin ekmartin merged commit 1a7ea2d into reactiflux:master Mar 30, 2017
@Throne3d Throne3d deleted the add/custom-formatting branch March 30, 2017 14:19
@ekmartin
Copy link
Member

ekmartin commented Apr 4, 2017

Released in 2.3.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants