Initial help feature

This commit is contained in:
lanxu 2019-11-20 20:28:11 +02:00
parent 436fb393ac
commit d386c9d242
13 changed files with 160 additions and 3 deletions

View File

@ -31,7 +31,9 @@ function query () {
if (!isPm) {
pluginManager.testPlugins(message).then(function (results) {
for (var i = 0; i < results.length; i++) {
console.log(results[i])
let result = results[i]
console.log('[' + result.to + ']', result.message)
}
query()
}, function (error) {

52
logger.js Normal file
View File

@ -0,0 +1,52 @@
const fs = require('fs')
const path = require('path')
const winston = require('winston')
const logDirectory = path.join(__dirname, '/log/')
// ensure log directory exists
if (!fs.existsSync(logDirectory)) {
fs.mkdirSync(logDirectory)
}
let logger = winston.createLogger({
level: process.env.NODE_ENV !== 'production' ? 'error' : 'debug',
transports: [
new winston.transports.File({
level: 'info',
filename: path.join(logDirectory, '/ircbot.log'),
handleExceptions: true,
json: true,
maxsize: 5242880,
maxFiles: 5,
colorize: false,
timestamp: true
}),
new winston.transports.Console({
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
timestamp: true
})
],
exceptionHandlers: [
new winston.transports.Console({
json: false,
timestamp: true
}),
new winston.transports.File({
filename: logDirectory + '/exceptions.log',
json: false
})
],
exitOnError: false
})
logger.stream = {
write: function (message, encoding) {
logger.info(message)
}
}
module.exports = logger

View File

@ -12,6 +12,10 @@ class BasePlugin {
result (input) {
}
help () {
return 'No documentation'
}
}
// export default BasePlugin;

View File

@ -6,6 +6,11 @@ class Character1 extends BasePlugin {
this.name = 'Character1'
}
help () {
// | Command | Description
return '.luohahmo Luo D&D-hahmon statsit'
}
test (input) {
if (input.match(/^\.luohahmo$/i)) {
return true

View File

@ -109,6 +109,11 @@ class Character2 extends BasePlugin {
this.name = 'Character2'
}
help () {
// | Command | Description
return '.luohahmo2 Luo D&D-hahmon statsit nimellä ja luokalla'
}
test (input) {
if (input.match(/^\.luohahmo2$/i)) {
return true

View File

@ -6,6 +6,11 @@ class Dice extends BasePlugin {
this.name = 'Dice'
}
help () {
// | Command | Description
return '.heita | .heitä 1d4 Heitä noppaa. Esim. 1d4'
}
test (input) {
if (input.match(/^\.heit(a|ä) ([0-9]+)d([0-9]+)( ([ \w])*)*$/i)) {
return true

33
plugins/help.js Normal file
View File

@ -0,0 +1,33 @@
var BasePlugin = require('./base.js')
const fs = require('fs')
class Help extends BasePlugin {
constructor (config) {
super(config)
this.name = 'Help'
}
test (input) {
if (input.match(/^\.apua/i)) {
return true
}
return false
}
result (input) {
return new Promise((resolve, reject) => {
let help = []
this.config.plugins.forEach((pluginName) => {
if (fs.existsSync('./plugins/' + pluginName.toLowerCase() + '.js')) {
let Plugin = require('./' + pluginName.toLowerCase() + '.js')
let plugin = new Plugin(this.config)
if (typeof plugin.help === 'function') {
help.push({message: plugin.help(), to: 'nick'})
}
}
})
resolve(help)
})
}
}
module.exports = Help

View File

@ -7,6 +7,12 @@ class InstantAswer extends BasePlugin {
super(config)
this.name = 'InstantAswer'
}
help () {
// | Command | Description
return '.wtf <termi> Etsii vastauksen annetulle termille'
}
test (input) {
if (input.match(/^\.wtf (.+)$/i)) {
return true

View File

@ -8,6 +8,11 @@ class Joke extends BasePlugin {
this.name = 'Joke'
}
help () {
// | Command | Description
return '.vitsi Antaa huikean vitsin!'
}
test (input) {
if (input.match(/^\.vitsi$/i)) {
return true

View File

@ -17,6 +17,11 @@ class Kernel extends BasePlugin {
return promise
}
help () {
// | Command | Description
return '.kernel <release> Hakee viimeisimmän kernelin version'
}
test (input) {
// Kernel version
if (input.match(/^\.kernel( (.+))?/i)) {

View File

@ -42,6 +42,31 @@ class PluginManager {
})
}
validateResult (resultPromise) {
return new Promise((resolve) => {
let wrapperPromise = new Promise((resolve) => resolve(resultPromise))
wrapperPromise.then((results) => {
if (!Array.isArray(results)) {
results = [results]
}
let promises = []
results.forEach((result) => {
let to = 'channel'
let message = result
if (typeof result === 'object' && typeof result.to !== 'undefined') {
to = result.to
message = result.message
}
promises.push(new Promise({ to, message }))
})
resolve(promises)
})
})
}
testPlugins (input, publicChat) {
if (typeof publicChat === 'undefined') {
publicChat = true
@ -54,7 +79,7 @@ class PluginManager {
let plugin = this.plugins[i]
if (plugin.test(input)) {
logger.info('* Plugin ' + plugin.name + ' reported hit')
promises.push(plugin.result(input))
promises.push(this.validateResult(plugin.result(input)))
}
}
} else {
@ -63,7 +88,7 @@ class PluginManager {
if (plugin.test(input)) {
logger.info('* Plugin ' + plugin.name + ' reported hit')
promises.push(plugin.result(input))
promises.push(this.validateResult(plugin.result(input)))
}
}
}

View File

@ -10,6 +10,11 @@ class Pwm extends BasePlugin {
this.apikey = config.keys.owm.apiKey // owm for sunrise
}
help () {
// | Command | Description
return '.pvm Kertoo päivän, nimipäivän ja muita tietoja'
}
test (input) {
var res = input.match(/^\.pvm$/i)
if (res) {

View File

@ -31,6 +31,11 @@ class Weather extends BasePlugin {
}
}
help () {
// | Command | Description
return '.saa <kaupunki> Antaa sään maapallon tärkeimmistä sijainneista'
}
test (input) {
if (input.match(/^\.saa(.*)/i)) {
return true