Skip to content

Instantly share code, notes, and snippets.

@Dirrk
Created August 6, 2016 16:41
Show Gist options
  • Save Dirrk/7d5f3022ebad04ae45c732324943658a to your computer and use it in GitHub Desktop.
Save Dirrk/7d5f3022ebad04ae45c732324943658a to your computer and use it in GitHub Desktop.
/**
* Created by Derek Rada on 3/10/2015.
*/
var SlackPlugin = require('../../lib/slackService/v3/slackPlugin');
var util = require('util');
var GiphyApi= require('./GiphyApi');
function GiphyPlugin() {
SlackPlugin.call(this);
var self = this;
self.name = "Giphy";
self.giphy = new GiphyApi();
console.log("GiphyPlugin added to Plugins");
};
util.inherits(GiphyPlugin, SlackPlugin);
GiphyPlugin.prototype.process = function process(message, next) {
var self = this;
if (!(message && message.text)) {
self.stats.discarded++;
next();
return;
}
if (message.text.toString().search(/^!giphy.*/i) >= 0) {
var words = message.text.toString().split(' ');
self.stats.consumed++;
if (isAprilFirst()) {
self.sendMessage(message.locationId, ["<http://i.giphy.com/Vuw9m5wXviFIQ.gif|April Fools Day>"]);
} else if (words.length > 1) {
if (words[words.length - 1].search(/help$/i) >= 0) {
self.sendHelp(message.locationId);
} else if (words[words.length -1].search(/random$/i) >= 0) {
console.log("Random");
self.giphy.random(function (messages) {
self.sendMessage(message.locationId, messages);
});
} else {
console.log("Search");
words.shift();
self.giphy.search(words, function (messages) {
self.sendMessage(message.locationId, messages);
});
}
} else {
self.sendHelp(message.locationId);
}
} else {
self.stats.discarded++;
next()
}
};
GiphyPlugin.prototype.sendHelp = function (location) {
var messages = [
"Giphy Help:\n",
"!giphy (help) - shows this help message\n",
"!giphy random - shows a random giphy\n",
"!giphy any words - searches giphy for 'any words' and returns back a random gif from the results\n"
];
this.sendMessage(location, messages);
};
function isAprilFirst() {
var now = new Date();
return (now.getMonth() == 3 && now.getDate() == 1);
}
module.exports = GiphyPlugin;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment