2019-08-13 19:37:22 +03:00
|
|
|
var Promise = require('promise')
|
|
|
|
var requestPromise = require('./helpers/requestPromise.js')
|
|
|
|
var BasePlugin = require('./base.js')
|
2017-07-15 13:58:28 +03:00
|
|
|
|
|
|
|
class Joke extends BasePlugin {
|
2019-08-13 19:37:22 +03:00
|
|
|
constructor (config) {
|
|
|
|
super(config)
|
|
|
|
this.name = 'Joke'
|
2017-07-15 13:58:28 +03:00
|
|
|
}
|
|
|
|
|
2019-11-20 20:28:11 +02:00
|
|
|
help () {
|
|
|
|
// | Command | Description
|
|
|
|
return '.vitsi Antaa huikean vitsin!'
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
test (input) {
|
|
|
|
if (input.match(/^\.vitsi$/i)) {
|
|
|
|
return true
|
2017-07-15 13:58:28 +03:00
|
|
|
}
|
2019-08-13 19:37:22 +03:00
|
|
|
return false
|
2017-07-15 13:58:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
getJoke () {
|
|
|
|
var urlArgs = {}
|
|
|
|
var url = 'https://icanhazdadjoke.com/'
|
2017-07-15 13:58:28 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
var promise = requestPromise(url, urlArgs, {
|
|
|
|
'Accept': 'application/json'
|
|
|
|
})
|
2017-07-15 13:58:28 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
return promise
|
2017-07-15 13:58:28 +03:00
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
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: '
|
2017-07-15 13:58:28 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
var joke = data.joke
|
|
|
|
if (typeof joke === 'undefined' || joke === null) {
|
|
|
|
reject(new Error('No joke!'))
|
|
|
|
}
|
2017-07-15 13:58:28 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
text += '"' + joke + '"'
|
2017-07-15 13:58:28 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
resolve(text)
|
|
|
|
}, function (error) {
|
|
|
|
reject(error)
|
|
|
|
})
|
2017-07-15 13:58:28 +03:00
|
|
|
}
|
2019-08-13 19:37:22 +03:00
|
|
|
})
|
2017-07-15 13:58:28 +03:00
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
return resultPromise
|
2017-07-15 13:58:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 19:37:22 +03:00
|
|
|
module.exports = Joke
|