kulmapaikka-ircbot/plugins/joke.js

55 lines
1.1 KiB
JavaScript
Raw Normal View History

var Promise = require('promise')
var requestPromise = require('./helpers/requestPromise.js')
var BasePlugin = require('./base.js')
class Joke extends BasePlugin {
constructor (config) {
super(config)
this.name = 'Joke'
}
test (input) {
if (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 (resolve, reject) {
if (input.match(/^\.vitsi$/i)) {
that.getJoke().then(function (data) {
var text = 'Vitsi: '
var joke = data.joke
if (typeof joke === 'undefined' || joke === null) {
reject(new Error('No joke!'))
}
text += '"' + joke + '"'
resolve(text)
}, function (error) {
reject(error)
})
}
})
return resultPromise
}
}
module.exports = Joke