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')
class Joke extends BasePlugin {
constructor (config) {
super(config)
this.name = 'Joke'
}
2019-11-20 20:28:11 +02:00
help () {
// | Command | Description
return '.vitsi Antaa huikean vitsin!'
}
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