From bf7fd428a68f4c43d4a3981a52b86a328c5fd7b1 Mon Sep 17 00:00:00 2001 From: Kegan Myers Date: Sat, 9 Aug 2014 17:49:22 -0500 Subject: [PATCH] Allow better configuration --- index.js | 18 ++++++++++++++++-- package.json | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index fb8fd07..ce805e8 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,5 @@ -var _ = require('lodash'); +var fs = require('fs'), + _ = require('lodash'); module.exports = function(configuration) { var listenOn = (function(name) { @@ -14,10 +15,23 @@ module.exports = function(configuration) { return ["phrase"]; } })(configuration.command); + var phrases = configuration.phrases; + phrases = (typeof phrases === "object" && phrases instanceof Array) ? phrases : []; + + if (typeof configuration.phraseFile === "string") { + phrases = _.compact(_.union(phrases, fs.readFileSync(configuration.phraseFile).split("\n"))); + } + + function phrase() { + if (phrases.length) + return phrases[Math.floor(Math.random() * phrases.length)]; + + return "This phrase command has not been properly configured." + } function handler(slack) { try { - slack.replyUser(slack.text); + slack.replyUser(phrase()); } catch (e) { slack.error(e.toString()); } diff --git a/package.json b/package.json index 6bbfb73..499d6b4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "slack-phrase-command", - "version": "0.0.1", + "version": "0.0.2", "description": "", "main": "index.js", "scripts": {