kulmapaikka-ircbot/plugins/joke.js
2017-07-15 13:58:28 +03:00

60 lines
1.2 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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;