Added Pvm and cron

This commit is contained in:
lanxu 2018-10-27 14:50:31 +03:00
parent e24d0d75dd
commit f5facd076d
6 changed files with 256 additions and 44 deletions

15
app.js
View File

@ -7,7 +7,7 @@ const path = require('path');
// Load config // Load config
const config = require('./config'); const config = require('./config');
const cron = require('node-cron');
// Load new plugins // Load new plugins
const PluginManager = require('./plugins/pluginManager.js'); const PluginManager = require('./plugins/pluginManager.js');
@ -38,6 +38,7 @@ if(!config.irc.connect) {
return; return;
} }
var isConnected = false
// Start client // Start client
var client = new irc.Client(config.irc.server, config.irc.nick, { var client = new irc.Client(config.irc.server, config.irc.nick, {
debug: true, debug: true,
@ -131,3 +132,15 @@ client.addListener('names', function(channel, nicks) {
} }
} }
}); });
for (let i = 0; i < config.cron.length; i++) {
let row = config.cron[i]
let that = this;
cron.schedule(row.cron, () => {
if (client.conn !== null) {
for (let c = 0; c < config.irc.channels.length; c++) {
handleMessage('BOT', config.irc.channels[i] , row.command);
}
}
});
}

View File

@ -1,41 +1,51 @@
var config = { let config = {
plugins: [ plugins: [
'Character1', 'Character1',
'Character2', 'Character2',
'Dice', 'Dice',
'InstantAnswer', 'InstantAnswer',
'Kernel', 'Kernel',
'Tweet', 'Twitter',
'Url', 'Url',
'Weather', 'Weather',
'Joke', 'Youtube',
], 'Joke',
irc: { 'Mastodon',
connect: true, 'Users',
server: 'irc.example.com', 'Pvm'
nick: 'My bot', ],
channels: ['#botdev'] cron: [{
}, cron: '1 0 * * *',
members: { command: '.pvm'
'lanxu': { }],
user: /lanxu$/, irc: {
host: /^(.*)example\.com$/ connect: true,
}, server: 'irc.example.com',
}, nick: 'My bot',
owmCityIds: { channels: ['#botdev']
Tampere: 634963, },
Helsinki: 658225 members: {
}, 'lanxu': {
keys: { user: /lanxu$/,
twitter: { host: /^(.*)example\.com$/
consumerKey: '', }
consumerSecret: '' },
}, owmCityIds: {
google: { Tampere: 634963,
apiKey: '' Helsinki: 658225
}, },
owm: { keys: {
apiKey: '' twitter: {
} consumerKey: '',
} consumerSecret: ''
}; },
google: {
apiKey: ''
},
owm: {
apiKey: ''
}
}
}
module.exports = config

87
package-lock.json generated
View File

@ -4,6 +4,11 @@
"lockfileVersion": 1, "lockfileVersion": 1,
"requires": true, "requires": true,
"dependencies": { "dependencies": {
"@types/node": {
"version": "10.12.0",
"resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.0.tgz",
"integrity": "sha512-3TUHC3jsBAB7qVRGxT6lWyYo2v96BMmD2PTcl47H25Lu7UXtFH/2qqmKiVrnel6Ne//0TFYf6uvNX+HW2FRkLQ=="
},
"abbrev": { "abbrev": {
"version": "1.1.1", "version": "1.1.1",
"resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz", "resolved": "https://registry.npmjs.org/abbrev/-/abbrev-1.1.1.tgz",
@ -151,7 +156,7 @@
}, },
"axios": { "axios": {
"version": "0.18.0", "version": "0.18.0",
"resolved": "http://registry.npmjs.org/axios/-/axios-0.18.0.tgz", "resolved": "https://registry.npmjs.org/axios/-/axios-0.18.0.tgz",
"integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=", "integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=",
"requires": { "requires": {
"follow-redirects": "^1.3.0", "follow-redirects": "^1.3.0",
@ -247,6 +252,11 @@
"resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.5.1.tgz",
"integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA==" "integrity": "sha512-MKiLiV+I1AA596t9w1sQJ8jkiSr5+ZKi0WKrYGUn6d1Fx+Ij4tIj+m2WMQSGczs5jZVxV339chE8iwk6F64wjA=="
}, },
"boolbase": {
"version": "1.0.0",
"resolved": "https://registry.npmjs.org/boolbase/-/boolbase-1.0.0.tgz",
"integrity": "sha1-aN/1++YMUes3cl6p4+0xDcwed24="
},
"boom": { "boom": {
"version": "4.3.1", "version": "4.3.1",
"resolved": "https://registry.npmjs.org/boom/-/boom-4.3.1.tgz", "resolved": "https://registry.npmjs.org/boom/-/boom-4.3.1.tgz",
@ -411,6 +421,19 @@
"resolved": "https://registry.npmjs.org/chance/-/chance-1.0.13.tgz", "resolved": "https://registry.npmjs.org/chance/-/chance-1.0.13.tgz",
"integrity": "sha512-9cpcgmAIQiXC0eMgQuMZgXuHR2Y+gKUyGQnalqSAg5LlUJyJFsZeKyuHVSGhj+bx18ppH+Jo3VOayNeXR/7p9Q==" "integrity": "sha512-9cpcgmAIQiXC0eMgQuMZgXuHR2Y+gKUyGQnalqSAg5LlUJyJFsZeKyuHVSGhj+bx18ppH+Jo3VOayNeXR/7p9Q=="
}, },
"cheerio": {
"version": "1.0.0-rc.2",
"resolved": "https://registry.npmjs.org/cheerio/-/cheerio-1.0.0-rc.2.tgz",
"integrity": "sha1-S59TqBsn5NXawxwP/Qz6A8xoMNs=",
"requires": {
"css-select": "~1.2.0",
"dom-serializer": "~0.1.0",
"entities": "~1.1.1",
"htmlparser2": "^3.9.1",
"lodash": "^4.15.0",
"parse5": "^3.0.1"
}
},
"chokidar": { "chokidar": {
"version": "2.0.4", "version": "2.0.4",
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-2.0.4.tgz", "resolved": "https://registry.npmjs.org/chokidar/-/chokidar-2.0.4.tgz",
@ -616,6 +639,33 @@
"integrity": "sha1-ojD2T1aDEOFJgAmUB5DsmVRbyn4=", "integrity": "sha1-ojD2T1aDEOFJgAmUB5DsmVRbyn4=",
"dev": true "dev": true
}, },
"css-select": {
"version": "1.2.0",
"resolved": "http://registry.npmjs.org/css-select/-/css-select-1.2.0.tgz",
"integrity": "sha1-KzoRBTnFNV8c2NMUYj6HCxIeyFg=",
"requires": {
"boolbase": "~1.0.0",
"css-what": "2.1",
"domutils": "1.5.1",
"nth-check": "~1.0.1"
},
"dependencies": {
"domutils": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/domutils/-/domutils-1.5.1.tgz",
"integrity": "sha1-3NhIiib1Y9YQeeSMn3t+Mjc2gs8=",
"requires": {
"dom-serializer": "0",
"domelementtype": "1"
}
}
}
},
"css-what": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/css-what/-/css-what-2.1.2.tgz",
"integrity": "sha512-wan8dMWQ0GUeF7DGEPVjhHemVW/vy6xUYmFzRY8RYqgA0JtXC9rJmbScBjqSu6dg9q0lwPQy6ZAmJVr3PPTvqQ=="
},
"currently-unhandled": { "currently-unhandled": {
"version": "0.4.1", "version": "0.4.1",
"resolved": "https://registry.npmjs.org/currently-unhandled/-/currently-unhandled-0.4.1.tgz", "resolved": "https://registry.npmjs.org/currently-unhandled/-/currently-unhandled-0.4.1.tgz",
@ -2913,6 +2963,15 @@
"to-regex": "^3.0.1" "to-regex": "^3.0.1"
} }
}, },
"node-cron": {
"version": "2.0.3",
"resolved": "https://registry.npmjs.org/node-cron/-/node-cron-2.0.3.tgz",
"integrity": "sha512-eJI+QitXlwcgiZwNNSRbqsjeZMp5shyajMR81RZCqeW0ZDEj4zU9tpd4nTh/1JsBiKbF8d08FCewiipDmVIYjg==",
"requires": {
"opencollective-postinstall": "^2.0.0",
"tz-offset": "0.0.1"
}
},
"node-dev": { "node-dev": {
"version": "3.1.3", "version": "3.1.3",
"resolved": "https://registry.npmjs.org/node-dev/-/node-dev-3.1.3.tgz", "resolved": "https://registry.npmjs.org/node-dev/-/node-dev-3.1.3.tgz",
@ -3047,6 +3106,14 @@
"path-key": "^2.0.0" "path-key": "^2.0.0"
} }
}, },
"nth-check": {
"version": "1.0.2",
"resolved": "https://registry.npmjs.org/nth-check/-/nth-check-1.0.2.tgz",
"integrity": "sha512-WeBOdju8SnzPN5vTUJYxYUxLeXpCaVP5i5e0LF8fg7WORF2Wd7wFX/pk0tYZk7s8T+J7VLy0Da6J1+wCT0AtHg==",
"requires": {
"boolbase": "~1.0.0"
}
},
"number-is-nan": { "number-is-nan": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.0.tgz", "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.0.tgz",
@ -3134,6 +3201,11 @@
"resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.1.tgz", "resolved": "https://registry.npmjs.org/on-headers/-/on-headers-1.0.1.tgz",
"integrity": "sha1-ko9dD0cNSTQmUepnlLCFfBAGk/c=" "integrity": "sha1-ko9dD0cNSTQmUepnlLCFfBAGk/c="
}, },
"opencollective-postinstall": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/opencollective-postinstall/-/opencollective-postinstall-2.0.1.tgz",
"integrity": "sha512-saQQ9hjLwu/oS0492eyYotoh+bra1819cfAT5rjY/e4REWwuc8IgZ844Oo44SiftWcJuBiqp0SA0BFVbmLX0IQ=="
},
"optimist": { "optimist": {
"version": "0.6.1", "version": "0.6.1",
"resolved": "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz", "resolved": "https://registry.npmjs.org/optimist/-/optimist-0.6.1.tgz",
@ -3184,6 +3256,14 @@
"error-ex": "^1.2.0" "error-ex": "^1.2.0"
} }
}, },
"parse5": {
"version": "3.0.3",
"resolved": "https://registry.npmjs.org/parse5/-/parse5-3.0.3.tgz",
"integrity": "sha512-rgO9Zg5LLLkfJF9E6CCmXlSE4UVceloys8JrFqCcHloC3usd/kJCyPDwH2SOlzix2j3xaP9sUX3e8+kvkuleAA==",
"requires": {
"@types/node": "*"
}
},
"parseurl": { "parseurl": {
"version": "1.3.2", "version": "1.3.2",
"resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.2.tgz", "resolved": "https://registry.npmjs.org/parseurl/-/parseurl-1.3.2.tgz",
@ -4094,6 +4174,11 @@
"request": "^2.72.0" "request": "^2.72.0"
} }
}, },
"tz-offset": {
"version": "0.0.1",
"resolved": "https://registry.npmjs.org/tz-offset/-/tz-offset-0.0.1.tgz",
"integrity": "sha512-kMBmblijHJXyOpKzgDhKx9INYU4u4E1RPMB0HqmKSgWG8vEcf3exEfLh4FFfzd3xdQOw9EuIy/cP0akY6rHopQ=="
},
"undefsafe": { "undefsafe": {
"version": "2.0.2", "version": "2.0.2",
"resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.2.tgz", "resolved": "https://registry.npmjs.org/undefsafe/-/undefsafe-2.0.2.tgz",

View File

@ -15,6 +15,7 @@
}, },
"dependencies": { "dependencies": {
"chance": "=1.0.13", "chance": "=1.0.13",
"cheerio": "^1.0.0-rc.2",
"cron": "=1.3.0", "cron": "=1.3.0",
"file-stream-rotator": "=0.2.1", "file-stream-rotator": "=0.2.1",
"finalhandler": "=1.1.1", "finalhandler": "=1.1.1",
@ -24,6 +25,7 @@
"lokijs": "=1.5.3", "lokijs": "=1.5.3",
"mkdirp": "=0.5.1", "mkdirp": "=0.5.1",
"morgan": "=1.9.0", "morgan": "=1.9.0",
"node-cron": "^2.0.3",
"node-dev": "=3.1.3", "node-dev": "=3.1.3",
"node-uuid": "=1.4.8", "node-uuid": "=1.4.8",
"promise": "=8.0.1", "promise": "=8.0.1",

View File

@ -18,7 +18,11 @@ function RequestPromise(url, urlArgs, headers, json) {
json: json json: json
}, function(error, response, body) { }, function(error, response, body) {
if(!error && response.statusCode == 200) { if(!error && response.statusCode == 200) {
resolve(JSON.parse(body)); if (json === false) {
resolve(JSON.parse(body)); /// wtf
} else {
resolve(body)
}
} else { } else {
reject(error); reject(error);
} }

98
plugins/pvm.js Normal file
View File

@ -0,0 +1,98 @@
var Promise = require('promise')
var requestPromise = require('./helpers/requestPromise.js')
var BasePlugin = require('./base.js')
var cheerio = require('cheerio')
class Pwm extends BasePlugin {
constructor (config) {
super(config)
this.name = 'Pvm'
this.apikey = config.keys.owm.apiKey // owm for sunrise
}
test (input) {
var res = input.match(/^\.pvm$/i)
if (res) {
return true
}
return false
}
today () {
var today = new Date()
var month = today.getMonth() + 1
var day = today.getDate()
var dateString = day + '.' + month + '.'
return dateString
}
getWeather (city, code) {
var language = 'fi'
var urlArgs = {
'q': city,
'lang': language,
'APPID': this.apikey
}
var url = 'http://api.openweathermap.org/data/2.5/weather'
var promise = requestPromise(url, urlArgs)
return promise
}
getNimipaiva () {
let dateString = this.today()
var urlArgs = {}
var url = 'http://nimipäivä.fi/' + dateString + '/'
var promise = requestPromise(url, urlArgs, {
'Accept': 'application/json'
}, true)
return promise
}
getAllData () {
return Promise.all([
this.getNimipaiva(),
this.getWeather('Helsinki')
])
}
result (input) {
var that = this
var resultPromise = new Promise(function (resolve, reject) {
var res = input.match(/^\.pvm$/i)
if (res) {
that.getAllData().then(function (data) {
// console.log(data)
let weather = data[1]
if (weather.cod === '404') {
reject(new Error('Eioo'))
return
}
let sunriseDate = new Date(Number(weather.sys.sunrise) * 1000)
let sunsetDate = new Date(Number(weather.sys.sunset) * 1000)
let sunrise = sunriseDate.getHours() + '.' + sunriseDate.getMinutes()
let sunset = sunsetDate.getHours() + '.' + sunsetDate.getMinutes()
const $ = cheerio.load(data[0])
let names = []
let names_ = $('.text p').children('a').each(function (i, elem) {
names.push($(this).text())
})
let text = 'Pvm: Tänään on ' + that.today() + ' ja nimipäivää viettävät ' + names.join(', ') + '. Aurinko nousee ' + sunrise + ' ja laskee ' + sunset + '.'
resolve(text)
}, function (error) {
reject(error)
})
}
})
return resultPromise
}
}
module.exports = Pwm