kulmapaikka-ircbot/plugins/joke.js

60 lines
1.2 KiB
JavaScript
Raw Normal View History

var Promise = require('promise');
var requestPromise = require('./helpers/requestPromise.js');
var BasePlugin = require('./base.js');
var logger = require('winston');
class Joke extends BasePlugin {
constructor(config) {
super(config);
this.name = 'Joke';
}
test(input) {
var res = null;
if (res = input.match(/^\.vitsi$/i)) {
return true;
}
return false;
}
getJoke() {
var urlArgs = {};
var url = 'https://icanhazdadjoke.com/';
var promise = requestPromise(url, urlArgs, {
'Accept': 'application/json',
});
return promise;
}
result(input) {
var that = this;
var resultPromise = new Promise(function(resultResolve, resultReject) {
var res = null;
if (res = input.match(/^\.vitsi$/i)) {
that.getJoke().then(function(data) {
var text = 'Vitsi: ';
var joke = data.joke;
if(typeof joke === 'undefined' || joke === null) {
resultReject('No joke!');
}
text += "\"" + joke + "\"";
resultResolve(text);
}, function(error) {
resultReject(error);
});
}
});
return resultPromise;
}
}
module.exports = Joke;