All plugins are now refactored

This commit is contained in:
lanxu 2018-04-29 17:37:07 +03:00
parent 480249e3ee
commit f21792cd7b
20 changed files with 5753 additions and 686 deletions

View File

@ -1,25 +1,32 @@
#Kulmapaikka IRC Bot # Kulmapaikka IRC Bot
Irkkibotti kulmapaikkalaisille. Sisältää seuraavat huimat ominaisuudet: Irkkibotti kulmapaikkalaisille. Sisältää seuraavat huimat ominaisuudet:
|Ominaisuus|Kuvaus|
|----------|------|
| .sää / .saa | Antaa säätiedot tärkeimmille paikkakunnille. Hakee Tiedot OpenWeatherMapista ja Aurassa sijaitsevasta lämpöanturista. |
| .sää / .saa *kaupunki* | Antää säätiedot annetulle kaupungille
| .heita / .heitä x×d×n | Heittää x kertaa n-sivuista noppaa |
| .op | Antaa operaattorioikeudet kaikille ketkä sen ansaitsevat |
| .youtube *hakusana* | Hakee youtubesta hakusanalla |
| .luohahmo | Luo D&D-hahmon (yksinkertainen) |
| .luohahmo2 | Luo D&D-hahmon (monipuolinen) |
| .kernel *tyyppi* | Kertoo nykyisen uusimman kernel-version. Tyyppi voi olla longterm/stable/mainline. Mainline on oletus. |
| .wtf *kysymys* | Kertoo mitä termi tarkoittaa |
| .joke | Kertoo puujalkavitsin englanniksi |
- .sää / .saa ## Automaattiset toiminnot
* Antaa säätiedot tärkeimmille paikkakunnille. Hakee Tiedot OpenWeatherMapista ja Aurassa sijaitsevasta lämpöanturista.
- .sää / .saa *kaupunki*
* Antää säätiedot annetulle kaupungille
- .heita / .heitä *x*d*n*
* Heittää x kertaa n-sivuista noppaa
- .op
* Antaa operaattorioikeudet kaikille ketkä sen ansaitsevat
- .youtube *hakusana*
* Hakee youtubesta hakusanalla
- .luohahmo
* Luo D&D-hahmon
- .luohahmo2
* Luo D&D-hahmon
##Automaattiset toiminnot
- Youtube-linkeille tulee automaattisesti youtube-tiedot - Youtube-linkeille tulee automaattisesti youtube-tiedot
- Linkeistä näytetään automaattisesti otsikko (Testauksessa) - Twitter ja Mastodon -linkeistä automaattisesti näkyy sisältö
## Asennus
Muokkaa config.example.js ja tallenna config.js-tiedostona. Tämän jälkeen riittää:
```sh
npm install
npm run start
```
## Tekijä
Jukka Lankinen <jukka.lankinen@gmail.com>

256
app.js
View File

@ -1,229 +1,133 @@
// General includes // General includes
var irc = require('irc'); const irc = require('irc');
var fs = require('fs'); const fs = require('fs');
var winston = require('winston'); const winston = require('winston');
require('winston-daily-rotate-file'); require('winston-daily-rotate-file');
var path = require('path'); const path = require('path');
var iconCodeToText = {
'01d': 'selkeää',
'02d': 'puolipilvistä',
'03d': 'pilvistä',
'04d': 'pilvipeite rakoilee',
'09d': 'sadekuuroja',
'10d': 'vesisadetta',
'11d': 'ukkostaa',
'13d': 'lumisadetta',
'50d': 'sumua',
'01n': 'selkeää',
'02n': 'puolipilvistä',
'03n': 'pilvistä',
'04n': 'pilvipeite rakoilee',
'09n': 'sadekuuroja',
'10n': 'vesisadetta',
'11n': 'ukkostaa',
'13n': 'lumisadetta',
'50n': 'sumua'
};
// Load config // Load config
var config = require('./config'); const config = require('./config');
// Load new plugins // Load new plugins
var PluginManager = require('./plugins/pluginManager.js'); const PluginManager = require('./plugins/pluginManager.js');
var pluginManager = new PluginManager(config); const pluginManager = new PluginManager(config);
pluginManager.loadAllPlugins(); pluginManager.loadAllPlugins();
// Load plugins
var owmapi = require('./owmapi')(config);
var getTemperatureString = require('./plugins/getWeather')(config);
var getDnDCharacter = require('./plugins/getCharacter')();
var getDnDCharacter2 = require('./plugins/getCharacter2')();
var getDiceString = require('./plugins/getDice')();
var getUrlString = require('./plugins/getUrl')();
var getInstantAnswer = require('./plugins/getInstantAnswer')(config);
// Configure logger // Configure logger
// create a write stream (in append mode) // create a write stream (in append mode)
var logDirectory = __dirname + '/log'; const logDirectory = __dirname + '/log';
// ensure log directory exists // ensure log directory exists
if(!fs.existsSync(logDirectory)) { if(!fs.existsSync(logDirectory)) {
fs.mkdirSync(logDirectory); fs.mkdirSync(logDirectory);
} }
var logger = new (winston.Logger)({ var logger = new (winston.Logger)({
transports: [ transports: [
new (winston.transports.Console)(), new (winston.transports.Console)(),
new (winston.transports.DailyRotateFile)({ new (winston.transports.DailyRotateFile)({
filename: logDirectory + '/ircbot.log', filename: logDirectory + '/ircbot.log',
datePattern: '.yyyy-MM-dd', datePattern: '.yyyy-MM-dd',
maxsize: 20000 maxsize: 20000
}) })
] ]
}); });
if(!config.irc.connect) { if(!config.irc.connect) {
logger.info('Not connecting to IRC. Exit'); logger.info('Not connecting to IRC. Exit');
return; return;
} }
// 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,
channels: config.irc.channels, channels: config.irc.channels,
//floodProtection: true, //floodProtection: true,
//sasl: true, //sasl: true,
millisecondsOfSilenceBeforePingSent: 60000, millisecondsOfSilenceBeforePingSent: 60000,
}); });
var handleMessage = function(from, to, message) { var handleMessage = function(from, to, message) {
logger.info(from + ' => ' + to + ': ' + message); logger.info(from + ' => ' + to + ': ' + message);
pluginManager.testPlugins(message).then(function(results) { pluginManager.testPlugins(message).then(function(results) {
for(var i = 0; i < results.length; i++) { for(var i = 0; i < results.length; i++) {
client.say(to, results[i]); client.say(to, results[i]);
} }
}, function(error) { }, function(error) {
logger.error('Plugin error: ', error); logger.error('Plugin error: ', error);
}); });
//if (to.match(/^[#&]/)) { var res = null;
// channel message
if (message.match(/^\.sää$/i) || message.match(/^\.saa$/i)) {
getTemperatureString(config.owmCityIds).then(function(str) {
client.say(to, 'Sää: ' + str);
});
}
var res = null; // Op
if (message.match(/^\.op$/i)) {
client.send('NAMES', to);
}
// Weather // reload config
if (res = message.match(/^\.s(ää|aa) ([^0-9]+)$/i)) { if (message.match(/^\.paivita$/i)) {
//console.log('kaupunki', res[1]); // Using require for config files is dangerous
owmapi.getTemperature(res[2]).then(function(result) { // Rewrite this in the future
//console.log(result); var filename = path.resolve('./config.js');
if(result.cod === '404') { delete require.cache[filename];
//console.log('Eioo: ' + res[1]); config = require('./config');
client.say(to, 'Eioo: ' + res[2]); client.say(to, 'Ladattiin asetustiedosto. ' + Object.keys(config.members).length + ' käyttäjäasetusta. Päivitetään operaattorioikeuksia...');
} else { client.send('NAMES', to);
var city = result.name; }
var country = result.sys.country;
var temp = (result.main.temp-273.15).toFixed(1);
var code = result.weather[0].icon;
var sunriseDate = new Date(result.sys.sunrise*1000);
var sunsetDate = new Date(result.sys.sunset*1000);
var sunrise = sunriseDate.getHours() + '.' + sunriseDate.getMinutes();
var sunset = sunsetDate.getHours() + '.' + sunsetDate.getMinutes();
var humidity = result.main.humidity;
var pressure = result.main.pressure;
var windspeed = result.wind.speed;
client.say(to, 'Sää: '+city+', '+country+' ('+temp+' °C, '+ iconCodeToText[code] +', ilmankosteus: '+humidity+' %, ilmanpaine: '+pressure+' hPa, tuulen nopeus: '+windspeed+' m/s)');
}
}, function(error) {
console.log('ERROR ',error);
client.say(to, 'Oho! Tapahtui virhe. Yritä myöhemmin uudelleen...');
});
}
// Create Character
if (res = message.match(/^\.luohahmo$/i)) {
var character = getDnDCharacter();
client.say(to, ''+character);
}
// Create Character
if (res = message.match(/^\.luohahmo2$/i)) {
var character = getDnDCharacter2();
client.say(to, ''+character);
}
// Instant Answer
if (res = message.match(/^\.wtf (.+)$/i)) {
getInstantAnswer(res[1]).then(function(answer) {
client.say(to, '' + answer);
})
}
// Throw dice
if (res = message.match(/^\.heit(a|ä) ([0-9]+)d([0-9]+)$/i)) {
var th = getDiceString(res[2], res[3]);
client.say(to, 'Heitettiin: '+th);
}
if (message.match(/^\.heit(a|ä) voltti homo$/i)) {
client.say(to, 'steam://run/1250');
}
// Op
if (message.match(/^\.op$/i)) {
client.send('NAMES', to);
}
// reload config
if (message.match(/^\.paivita$/i)) {
// Using require for config files is dangerous
// Rewrite this in the future
var filename = path.resolve('./config.js');
delete require.cache[filename];
config = require('./config');
client.say(to, 'Ladattiin asetustiedosto. ' + Object.keys(config.members).length + ' käyttäjäasetusta. Päivitetään operaattorioikeuksia...');
client.send('NAMES', to);
}
}; };
client.addListener('message', function (from, to, message) { client.addListener('message', function (from, to, message) {
if(String(from) !== String(config.irc.nick)) { if(String(from) !== String(config.irc.nick)) {
handleMessage(from, to, message); handleMessage(from, to, message);
} }
}); });
client.addListener('pm', function (from, message) { client.addListener('pm', function (from, message) {
if(String(from) !== String(config.irc.nick)) { if(String(from) !== String(config.irc.nick)) {
handleMessage(from, from, message); handleMessage(from, from, message);
} }
}); });
client.addListener('join', function(channel, who) { client.addListener('join', function(channel, who) {
client.whois(who, function(result) { client.whois(who, function(result) {
console.log(result); console.log(result);
var members = config.members; var members = config.members;
for(var memberNick in members) { for(var memberNick in members) {
var member = members[memberNick]; var member = members[memberNick];
if(result.host.match(member.host) && result.user.match(member.user)) { if(result.host.match(member.host) && result.user.match(member.user)) {
client.send('MODE', channel, '+o', result.nick); client.send('MODE', channel, '+o', result.nick);
} }
} }
}); });
}); });
client.addListener('error', function(message) { client.addListener('error', function(message) {
console.log('error: ', message); console.log('error: ', message);
}); });
client.addListener('names', function(channel, nicks) { client.addListener('names', function(channel, nicks) {
if(typeof nicks[config.irc.nick] !== 'undefined' && nicks[config.irc.nick] === '@') { if(typeof nicks[config.irc.nick] !== 'undefined' && nicks[config.irc.nick] === '@') {
// go through nicks // go through nicks
for(var nick in nicks) { for(var nick in nicks) {
if(nicks[nick] !== '@') { if(nicks[nick] !== '@') {
client.whois(nick, function(result) { client.whois(nick, function(result) {
var members = config.members; var members = config.members;
for(var memberNick in members) { for(var memberNick in members) {
var member = members[memberNick]; var member = members[memberNick];
if(result.host.match(member.host) && result.user.match(member.user)) { if(result.host.match(member.host) && result.user.match(member.user)) {
client.send('MODE', channel, '+o', result.nick); client.send('MODE', channel, '+o', result.nick);
} }
} }
}); });
} }
} }
} }
}); });

76
appTest.js Normal file
View File

@ -0,0 +1,76 @@
// Start without app
function prompt(question, callback) {
var stdin = process.stdin,
stdout = process.stdout;
stdin.resume();
stdout.write(question);
stdin.once('data', function (data) {
callback(data.toString().trim());
});
}
const fs = require('fs');
const winston = require('winston');
require('winston-daily-rotate-file');
const path = require('path');
// Load config
const config = require('./config');
// Load new plugins
const PluginManager = require('./plugins/pluginManager.js');
const pluginManager = new PluginManager(config);
pluginManager.loadAllPlugins();
// Configure logger
// create a write stream (in append mode)
const logDirectory = __dirname + '/log';
// ensure log directory exists
if(!fs.existsSync(logDirectory)) {
fs.mkdirSync(logDirectory);
}
var logger = new (winston.Logger)({
transports: [
new (winston.transports.Console)(),
new (winston.transports.DailyRotateFile)({
filename: logDirectory + '/ircbot-test.log',
datePattern: '.yyyy-MM-dd',
maxsize: 20000
})
]
});
const isPm = false
function query() {
prompt('> ', function (message) {
if(!isPm) {
pluginManager.testPlugins(message).then(function(results) {
for(var i = 0; i < results.length; i++) {
console.log(results[i]);
}
query()
}, function(error) {
logger.error('Plugin error: ', error);
});
} else {
// PM
pluginManager.testPlugins(message, false).then(function(results) {
for(var i = 0; i < results.length; i++) {
console.log(results[i]);
}
query()
}, function(error) {
logger.error('Plugin error: ', error);
});
}
});
}
query()

View File

@ -1,71 +0,0 @@
var request = require('request');
var Promise = require('promise');
var requestPromise = function(url, urlArgs) {
var promise = new Promise(function(resolve, reject) {
request({
url: url,
qs: urlArgs,
json: false
}, function(error, response, body) {
if(!error && response.statusCode == 200) {
resolve(JSON.parse(body));
} else {
reject(error);
}
});
});
return promise;
};
/**
* weather API module.
* @module kulmapaikka/owmapi
*/
module.exports = function(config) {
'use strict';
var apikey = config.keys.owm.apiKey;
return {
/** Get temperatures from Pena API (Aura) */
getTemperature: function(city, code) {
var language = 'fi';
var urlArgs = {
'q': city,
'lang': language,
'APPID': apikey
};
var url = 'http://api.openweathermap.org/data/2.5/weather';
var promise = requestPromise(url, urlArgs);
return promise;
},
getTemperatures: function(cities, code) {
if(typeof code === 'undefined') {
code = 'fi';
}
var language = 'fi';
var idList = '';
for(var city in cities) {
idList += cities[city] + ',';
}
idList = idList.substring(0,idList.length - 1);
var urlArgs = {
'id': idList,
'lang': language,
'APPID': apikey
};
var url = 'http://api.openweathermap.org/data/2.5/group';
var promise = requestPromise(url, urlArgs);
return promise;
}
};
};

4865
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@ -14,27 +14,26 @@
"url": "https://lanxu@bitbucket.org/lanxu/kulmapaikka-ircbot" "url": "https://lanxu@bitbucket.org/lanxu/kulmapaikka-ircbot"
}, },
"dependencies": { "dependencies": {
"chance": "=1.0.6", "chance": "=1.0.13",
"cron": "1.2.1", "cron": "=1.3.0",
"file-stream-rotator": "0.1.0", "file-stream-rotator": "=0.2.1",
"finalhandler": "1.0.0", "finalhandler": "=1.1.1",
"googleapis": "18.0.0", "googleapis": "=29.0.0",
"html-to-text": "^3.3.0", "html-to-text": "=4.0.0",
"irc": "0.5.2", "irc": "=0.5.2",
"mkdirp": "~0.5.1", "lokijs": "=1.5.3",
"morgan": "1.8.1", "mkdirp": "=0.5.1",
"node-ddg-api": "0.1.4", "morgan": "=1.9.0",
"node-dev": "3.1.3", "node-dev": "=3.1.3",
"node-uuid": "1.4.7", "node-uuid": "=1.4.8",
"promise": "^7.0.0", "promise": "=8.0.1",
"request": "^2.55.0", "request": "=2.85.0",
"request-promise": "^4.1.1", "request-promise": "=4.2.2",
"twitter": "1.7.0", "twitter": "=1.7.1",
"winston": "2.3.1", "winston": "=2.4.2",
"winston-daily-rotate-file": "1.4.6", "winston-daily-rotate-file": "=3.1.2"
"html-to-text": "3.3.0"
}, },
"devDependencies": { "devDependencies": {
"nodemon": "^1.2.1" "nodemon": "=1.17.3"
} }
} }

View File

@ -1,52 +0,0 @@
var request = require('request');
var Promise = require('promise');
var requestPromise = function(url, urlArgs) {
var promise = new Promise(function(resolve, reject) {
request({
url: url,
qs: urlArgs,
json: false
}, function(error, response, body) {
if(!error && response.statusCode == 200) {
resolve(JSON.parse(body));
} else {
reject(error);
}
});
});
return promise;
};
/**
* Pena API module.
* @module kulmapaikka/penaapi
*/
module.exports = function(config) {
'use strict';
return {
/** Get temperatures from Pena API (Aura) */
getTemperatures: function(start, stop, limit) {
var d = new Date();
var urlArgs = {};
if(typeof limit === 'undefined') {
limit = 20;
}
if(typeof start !== 'undefined' && typeof stop !== 'undefined') {
urlArgs.limit = limit;
urlArgs.start = start;
urlArgs.stop = stop;
}
var url = 'http://masa.dy.fi/api/temperature.php';
var promise = requestPromise(url, urlArgs);
return promise;
}
};
};

View File

@ -2,14 +2,15 @@
class BasePlugin { class BasePlugin {
constructor(config) { constructor(config) {
this.name = 'BasePlugin'; this.name = 'BasePlugin';
this.config = config;
} }
test(input) { test(input) {
} }
result(input) { result(input) {
} }
} }

62
plugins/character1.js Normal file
View File

@ -0,0 +1,62 @@
var Promise = require('promise');
var requestPromise = require('./helpers/requestPromise.js');
var BasePlugin = require('./base.js');
var logger = require('winston');
class Character1 extends BasePlugin {
constructor(config) {
super(config);
this.name = 'Character1';
}
test(input) {
var res = null;
if (res = input.match(/^\.luohahmo$/i)) {
return true;
}
return false;
}
throwStat() {
// Heittää 4 kertaa d6:n, tiputtaa heikoimman tuloksen ja laskee
// loput yhteen
var throws = [];
var sum = 0;
for( var i = 0; i < 4; i++ ) {
var t = Math.floor(Math.random()*6)+1;
throws.push(t);
}
throws.sort();
throws.reverse();
var sum = 0;
for(var i = 0; i < 3; i++) {
sum += throws[i];
}
return sum;
}
getDnDCharacter() {
var statPoints = 'DnD-statsit:';
var total = 0;
for( var i = 0; i < 6; i++) {
var newStat = this.throwStat();
statPoints += ' '; // erotetaan sanat toisistaan
statPoints += newStat;
total += Number(newStat);
}
statPoints += ' ('+total+')';
return statPoints;
};
result(input) {
// Different cases
let info = this.getDnDCharacter();
return info;
}
}
module.exports = Character1;

130
plugins/character2.js Normal file
View File

@ -0,0 +1,130 @@
var Promise = require('promise');
var requestPromise = require('./helpers/requestPromise.js');
var BasePlugin = require('./base.js');
var logger = require('winston');
var Chance = require('chance');
var chance = new Chance();
class Character {
constructor() {
this.gender = null;
this.name = null;
this.class = null;
this.alignment = null;
this.stats = [];
this.statSum = -1;
}
create() {
this.gender = chance.gender();
this.name = chance.first({gender: this.gender});
// Throw stats
for(var i = 0; i < 6; i++) {
var throws = [chance.d6(), chance.d6(), chance.d6(), chance.d6()].sort();
throws.splice(0, 1);
var stat = throws.reduce(function(a, b) { return a + b; }, 0);
this.stats.push(stat);
}
this.statSum = this.stats.reduce(function(a, b) { return a + b; }, 0);
// Select race
var races = {
'Human': '',
'Dwarf': 'Fighter',
'Elf': 'Wizard',
'Gnome': 'Bard',
'Half-Elf': '',
'Half-Orc': 'Barbarian',
'Halfling': 'Rogue',
};
this.race = Object.keys(races)[chance.natural({min: 0, max: Object.keys(races).length-1})];
// Select alignment
var alignmentY = ['Good', 'Neutral', 'Evil'].splice(chance.natural({min: 0, max: 2}), 1);
var alignmentX = ['Lawful', 'Neutral', 'Chaotic'].splice(chance.natural({min: 0, max: 2}), 1);
this.alignment = (alignmentY[0] === 'Neutral' && alignmentX[0] === 'Neutral') ? 'True Neutral' : alignmentX[0] + ' ' + alignmentY[0];
// Select class
var classes = {
'Barbarian' : '',
'Bard': '',
'Cleric': '',
'Druid': '',
'Fighter': '',
'Monk': '',
'Paladin': '',
'Ranger': '',
'Rogue': '',
'Sorcerer': '',
}
this.class = Object.keys(classes)[chance.natural({min: 0, max: Object.keys(classes).length-1})];
// Select age
var roll = function(times, func) {
var sum = 0;
for(var i = 0; i < times; i++) {
sum += chance[func]();
}
return sum;
}
var ages = {
'Human': { start: 15, classes: [roll(1, 'd4'), roll(1, 'd6'), roll(2, 'd6')] },
'Dwarf': { start: 40, classes: [roll(3, 'd6'), roll(5, 'd6'), roll(6, 'd6')] },
'Elf': { start: 110, classes: [roll(4, 'd6'), roll(6, 'd6'), roll(10, 'd6')] },
'Gnome': { start: 40, classes: [roll(4, 'd6'), roll(6, 'd6'), roll(9, 'd6')] },
'Half-Elf': { start: 20, classes: [roll(1, 'd6'), roll(2, 'd6'), roll(3, 'd6')] },
'Half-Orc': { start: 14, classes: [roll(1, 'd4'), roll(1, 'd6'), roll(2, 'd6')] },
'Halfling': { start: 20, classes: [roll(2, 'd4'), roll(3, 'd6'), roll(4, 'd6')] },
};
var ageClasses = {
'Barbarian': 0, 'Rogue': 0, 'Sorcerer': 0,
'Bard': 1, 'Fighter': 1, 'Paladin': 1, 'Ranger': 1,
'Cleric': 2, 'Druid': 2, 'Monk': 2, 'Wizard': 2,
};
this.age = ages[this.race].start + ages[this.race].classes[ageClasses[this.class]];
}
toString() {
var str = this.age + " years old " + this.alignment + " " + this.gender + " " + this.race + " " + this.class + " named " + this.name;
str += " with stats ";
this.stats.forEach(function(val) {
str += val + " ";
});
str += " ("+this.statSum+")";
return str;
}
}
class Character2 extends BasePlugin {
constructor(config) {
super(config);
this.name = 'Character2';
}
test(input) {
var res = null;
if (res = input.match(/^\.luohahmo2$/i)) {
return true;
}
return false;
}
result(input) {
// Different cases
let newChar = new Character();
newChar.create();
let result = newChar.toString();
return result;
}
}
module.exports = Character2;

68
plugins/dice.js Normal file
View File

@ -0,0 +1,68 @@
var Promise = require('promise');
var requestPromise = require('./helpers/requestPromise.js');
var BasePlugin = require('./base.js');
var logger = require('winston');
class Dice extends BasePlugin {
constructor(config) {
super(config);
this.name = 'Dice';
}
test(input) {
var res = null;
if (res = input.match(/^\.heit(a|ä) ([0-9]+)d([0-9]+)$/i)) {
return true;
}
if (res = input.match(/^\.heit(a|ä) voltti homo$/i)) {
return true
}
return false;
}
getDiceString(numThrows, sides) {
var total = 0;
var totalThrows = [];
numThrows = Number(numThrows);
sides = Number(sides);
if(numThrows === 0 || sides === 0) {
return 'Tyhjää ei voi heittää';
}
if(numThrows > 100) {
return 'Ei jaksa...';
}
for(var a = 0; a < numThrows; a++) {
var t = Math.floor(Math.random()*sides)+1;
totalThrows.push(t);
total += t;
}
var resultString = '';
totalThrows.forEach(function(value) {
resultString += value + ', ';
});
resultString = resultString.substring(0, resultString.length - 2);
resultString += ' (yhteensä: ' + total + '. '+ (total/numThrows).toFixed(2) + ' per heitto)';
return resultString;
}
result(input) {
// Different cases
let res = null
if (res = input.match(/^\.heit(a|ä) ([0-9]+)d([0-9]+)$/i)) {
var th = this.getDiceString(res[2], res[3]);
return 'Heitettiin: '+th;
}
if (input.match(/^\.heit(a|ä) voltti homo$/i)) {
return 'steam://run/1250';
}
}
}
module.exports = Dice;

View File

@ -1,40 +0,0 @@
/**
* Based on Cb's get character code
*/
module.exports = function(config) {
var throwStat = function() {
// Heittää 4 kertaa d6:n, tiputtaa heikoimman tuloksen ja laskee
// loput yhteen
var throws = [];
var sum = 0;
for( var i = 0; i < 4; i++ ) {
var t = Math.floor(Math.random()*6)+1;
throws.push(t);
}
throws.sort();
throws.reverse();
var sum = 0;
for(var i = 0; i < 3; i++) {
sum += throws[i];
}
return sum;
};
var getDnDCharacter = function() {
var statPoints = 'DnD-statsit:';
var total = 0;
for( var i = 0; i < 6; i++) {
var newStat = throwStat();
statPoints += ' '; // erotetaan sanat toisistaan
statPoints += newStat;
total += Number(newStat);
}
statPoints += ' ('+total+')';
return statPoints;
};
return getDnDCharacter;
};

View File

@ -1,107 +0,0 @@
var Chance = require('chance');
module.exports = function(config) {
var chance = new Chance();
var method = function() {
var result = '';
Character = function() {
this.gender = null;
this.name = null;
this.class = null;
this.alignment = null;
this.stats = [];
this.statSum = -1;
};
Character.prototype.create = function() {
this.gender = chance.gender();
this.name = chance.first({gender: this.gender});
// Throw stats
for(var i = 0; i < 6; i++) {
var throws = [chance.d6(), chance.d6(), chance.d6(), chance.d6()].sort();
throws.splice(0, 1);
var stat = throws.reduce(function(a, b) { return a + b; }, 0);
this.stats.push(stat);
}
this.statSum = this.stats.reduce(function(a, b) { return a + b; }, 0);
// Select race
var races = {
'Human': '',
'Dwarf': 'Fighter',
'Elf': 'Wizard',
'Gnome': 'Bard',
'Half-Elf': '',
'Half-Orc': 'Barbarian',
'Halfling': 'Rogue',
};
this.race = Object.keys(races)[chance.natural({min: 0, max: Object.keys(races).length-1})];
// Select alignment
var alignmentY = ['Good', 'Neutral', 'Evil'].splice(chance.natural({min: 0, max: 2}), 1);
var alignmentX = ['Lawful', 'Neutral', 'Chaotic'].splice(chance.natural({min: 0, max: 2}), 1);
this.alignment = (alignmentY[0] === 'Neutral' && alignmentX[0] === 'Neutral') ? 'True Neutral' : alignmentX[0] + ' ' + alignmentY[0];
// Select class
var classes = {
'Barbarian' : '',
'Bard': '',
'Cleric': '',
'Druid': '',
'Fighter': '',
'Monk': '',
'Paladin': '',
'Ranger': '',
'Rogue': '',
'Sorcerer': '',
}
this.class = Object.keys(classes)[chance.natural({min: 0, max: Object.keys(classes).length-1})];
// Select age
var roll = function(times, func) {
var sum = 0;
for(var i = 0; i < times; i++) {
sum += chance[func]();
}
return sum;
}
var ages = {
'Human': { start: 15, classes: [roll(1, 'd4'), roll(1, 'd6'), roll(2, 'd6')] },
'Dwarf': { start: 40, classes: [roll(3, 'd6'), roll(5, 'd6'), roll(6, 'd6')] },
'Elf': { start: 110, classes: [roll(4, 'd6'), roll(6, 'd6'), roll(10, 'd6')] },
'Gnome': { start: 40, classes: [roll(4, 'd6'), roll(6, 'd6'), roll(9, 'd6')] },
'Half-Elf': { start: 20, classes: [roll(1, 'd6'), roll(2, 'd6'), roll(3, 'd6')] },
'Half-Orc': { start: 14, classes: [roll(1, 'd4'), roll(1, 'd6'), roll(2, 'd6')] },
'Halfling': { start: 20, classes: [roll(2, 'd4'), roll(3, 'd6'), roll(4, 'd6')] },
};
var ageClasses = {
'Barbarian': 0, 'Rogue': 0, 'Sorcerer': 0,
'Bard': 1, 'Fighter': 1, 'Paladin': 1, 'Ranger': 1,
'Cleric': 2, 'Druid': 2, 'Monk': 2, 'Wizard': 2,
};
this.age = ages[this.race].start + ages[this.race].classes[ageClasses[this.class]];
};
Character.prototype.toString = function() {
var str = this.age + " years old " + this.alignment + " " + this.gender + " " + this.race + " " + this.class + " named " + this.name;
str += " with stats ";
this.stats.forEach(function(val) {
str += val + " ";
});
str += " ("+this.statSum+")";
return str;
};
var newChar = new Character();
newChar.create();
result = newChar.toString();
return result;
};
return method;
}; // export

View File

@ -1,36 +0,0 @@
module.exports = function(config) {
var getDiceString = function(numThrows, sides) {
var total = 0;
var totalThrows = [];
numThrows = Number(numThrows);
sides = Number(sides);
if(numThrows === 0 || sides === 0) {
return 'Tyhjää ei voi heittää';
}
if(numThrows > 100) {
return 'Ei jaksa...';
}
for(var a = 0; a < numThrows; a++) {
var t = Math.floor(Math.random()*sides)+1;
totalThrows.push(t);
total += t;
}
var resultString = '';
totalThrows.forEach(function(value) {
resultString += value + ', ';
});
resultString = resultString.substring(0, resultString.length - 2);
resultString += ' (yhteensä: ' + total + '. '+ (total/numThrows).toFixed(2) + ' per heitto)';
return resultString;
};
return getDiceString;
};

View File

@ -1,57 +0,0 @@
var penaapi = require('./../penaapi')();
var Promise = require('promise');
var iconCodeToText = {
'01d': 'selkeää',
'02d': 'puolipilvistä',
'03d': 'pilvistä',
'04d': 'pilvipeite rakoilee',
'09d': 'sadekuuroja',
'10d': 'vesisadetta',
'11d': 'ukkostaa',
'13d': 'lumisadetta',
'50d': 'sumua',
'01n': 'selkeää',
'02n': 'puolipilvistä',
'03n': 'pilvistä',
'04n': 'pilvipeite rakoilee',
'09n': 'sadekuuroja',
'10n': 'vesisadetta',
'11n': 'ukkostaa',
'13n': 'lumisadetta',
'50n': 'sumua'
};
module.exports = function(config) {
var owmapi = require('./../owmapi')(config);
var getTemperatureString = function(owmCityIds) {
var promise = new Promise(function(resolve, reject) {
Promise.all([
owmapi.getTemperatures(owmCityIds),
penaapi.getTemperatures()
]).then(function(results) {
var resultString = '';
// OWM
results[0].list.forEach(function(data) {
var city = data.name;
var temp = (data.main.temp-273.15).toFixed(1);
var code = data.weather[0].icon;
resultString += '' + city + ' ('+temp+' °C, '+iconCodeToText[code]+'), ';
});
if(results[1].length > 0) {
var temp = Number(results[1][0].temp).toFixed(1);
resultString += 'Aura (' + temp + ' °C)';
}
resolve(resultString);
});
});
return promise;
};
return getTemperatureString;
};

View File

@ -1,106 +1,101 @@
var Promise = require('promise'); const Promise = require('promise');
var requestPromise = require('./requestPromise.js'); const requestPromise = require('./requestPromise.js');
var google = require('googleapis'); const {google} = require('googleapis');
var youtube = google.youtube('v3'); const youtube = google.youtube('v3');
const apis = google.getSupportedAPIs();
module.exports = function(config) { module.exports = function(config) {
'use strict'; 'use strict';
var apikey = config.keys.google.apiKey; var apikey = config.keys.google.apiKey;
return { return {
getYoutubeVideoById: function(videoId) { getYoutubeVideoById: function(videoId) {
var promise = new Promise(function(resolve, reject) { var promise = new Promise(function(resolve, reject) {
var videosParams = { var videosParams = {
part: 'snippet,contentDetails,statistics', part: 'snippet,contentDetails,statistics',
id: videoId, id: videoId,
key: apikey key: apikey
}; };
console.log(videosParams); var thisResolve = resolve;
var thisResolve = resolve; var thisReject = reject;
var thisReject = reject; youtube.videos.list(videosParams, function(err, response) {
youtube.videos.list(videosParams, function(err, resp) { let resp = response.data;
//console.log('respo', resp); if(typeof resp === 'undefined' || resp === null || typeof resp.items === 'undefined' || resp.items === null || resp.items.length <= 0) {
thisReject('No results');
return;
}
if(typeof resp === 'undefined' || resp === null || typeof resp.items === 'undefined' || resp.items === null || resp.items.length <= 0) { var duration = resp.items[0].contentDetails.duration;
thisReject('No results'); var formattedTime = duration.replace("PT","").replace("H","t ").replace("M","m ").replace("S","s");
return; var data = {
} videoId: videoId,
url: 'https://youtu.be/'+videoId,
title: resp.items[0].snippet.title,
channelTitle: resp.items[0].snippet.channelTitle,
statistics: resp.items[0].statistics,
duration: formattedTime
};
thisResolve(data);
});
});
return promise;
},
getYoutubeVideo: function(query) {
var duration = resp.items[0].contentDetails.duration; var params = {
var formattedTime = duration.replace("PT","").replace("H","t ").replace("M","m ").replace("S","s"); part: 'snippet',
var data = { q: query,
videoId: videoId, type: 'video',
url: 'https://youtu.be/'+videoId, order: 'viewCount',
title: resp.items[0].snippet.title, key: apikey
channelTitle: resp.items[0].snippet.channelTitle, };
statistics: resp.items[0].statistics, console.log(params);
duration: formattedTime var promise = new Promise(function(resolve, reject) {
}; youtube.search.list(params, function(err, resp) {
console.log(resp); if (err) {
console.log(resp.items[0]); console.log('An error occured', err);
console.log(data); reject(err);
thisResolve(data); }
});
});
return promise;
},
getYoutubeVideo: function(query) {
var params = { if(resp === null || resp.items === null) {
part: 'snippet', reject('No results (null)');
q: query, return;
type: 'video', }
order: 'viewCount',
key: apikey
};
console.log(params);
var promise = new Promise(function(resolve, reject) {
youtube.search.list(params, function(err, resp) {
if (err) {
console.log('An error occured', err);
reject(err);
}
if(resp === null || resp.items === null) { if(resp.items.length <= 0) {
reject('No results (null)'); reject('No results');
return; return;
} }
if(resp.items.length <= 0) { var videoId = resp.items[0].id.videoId;
reject('No results'); var data = {
return; videoId: videoId,
} url: 'https://youtu.be/'+videoId,
title: resp.items[0].snippet.title,
var videoId = resp.items[0].id.videoId; channelTitle: resp.items[0].snippet.channelTitle
var data = { };
videoId: videoId,
url: 'https://youtu.be/'+videoId,
title: resp.items[0].snippet.title,
channelTitle: resp.items[0].snippet.channelTitle
};
var videosParams = { var videosParams = {
part: 'snippet,contentDetails,statistics', part: 'snippet,contentDetails,statistics',
id: videoId, id: videoId,
key: apikey key: apikey
}; };
youtube.videos.list(videosParams, function(err2, resp2) { youtube.videos.list(videosParams, function(err2, resp2) {
//console.log('respo', resp2); //console.log('respo', resp2);
if(resp2.items.length <= 0) { if(resp2.items.length <= 0) {
reject('No results'); reject('No results');
return; return;
} }
data.statistics = resp2.items[0].statistics; data.statistics = resp2.items[0].statistics;
var duration = resp2.items[0].contentDetails.duration; var duration = resp2.items[0].contentDetails.duration;
var formattedTime = duration.replace("PT","").replace("H","t ").replace("M","m ").replace("S","s"); var formattedTime = duration.replace("PT","").replace("H","t ").replace("M","m ").replace("S","s");
data.duration = formattedTime; data.duration = formattedTime;
resolve(data); resolve(data);
}); });
}); });
}); });
return promise; return promise;
} }
}; };
}; };

80
plugins/instantanswer.js Normal file
View File

@ -0,0 +1,80 @@
var Promise = require('promise');
var requestPromise = require('./helpers/requestPromise.js');
var BasePlugin = require('./base.js');
var logger = require('winston');
class InstantAswer extends BasePlugin {
constructor(config) {
super(config);
this.name = 'InstantAswer';
}
test(input) {
var res = null;
if (res = input.match(/^\.wtf (.+)$/i)) {
return true;
}
return false;
}
getDDG(query) {
var urlArgs = {
q: query,
kp: -1,
skip_disambig: '0',
format: 'json'
};
let url = 'https://api.duckduckgo.com/';
var promise = requestPromise(url, urlArgs, {
'Accept': 'application/json',
});
return promise;
}
getAnswer(query) {
let that = this;
var promise = new Promise(function(resolve, reject) {
that.getDDG(query).then((response) => {
var str = '';
if(response.AbstractText !== '') {
var text = '';
if(response.AbstractText.length > 350) {
text = response.AbstractText.substring(0, 350-response.AbstractURL.length) + '...';
} else {
text = response.AbstractText;
}
str = 'Vastaus: ' + text + '" | ' + response.AbstractURL;
resolve(str);
}
else if(response.RelatedTopics.length > 0) {
str = 'Vastaus: "' + response.RelatedTopics[0].Text + '" | ' + response.RelatedTopics[0].FirstURL;
resolve(str);
} else {
reject();
}
}, reject);
});
return promise;
}
result(input) {
let that = this;
var resultPromise = new Promise(function(resultResolve, resultReject) {
var res = null;
if (res = input.match(/^\.wtf (.+)$/i)) {
let query = res[1]
that.getAnswer(query).then(str => {
resultResolve(str);
})
} else {
resultReject();
}
});
return resultPromise;
}
}
module.exports = InstantAswer;

View File

@ -5,7 +5,9 @@ var Promise = require('promise');
class PluginManager { class PluginManager {
constructor(config) { constructor(config) {
this.enabledPlugins = []; this.enabledPlugins = [];
this.enabledPmPlugins = [];
this.plugins = []; this.plugins = [];
this.pmPlugins = [];
this.config = config; this.config = config;
var that = this; var that = this;
@ -17,31 +19,56 @@ class PluginManager {
logger.info('* Exists. Enabling...'); logger.info('* Exists. Enabling...');
that.enabledPlugins.push(pluginName.toLowerCase()); that.enabledPlugins.push(pluginName.toLowerCase());
} }
if(fs.existsSync('./plugins/pm'+pluginName.toLowerCase()+'.js')) {
}); logger.info('* Exists as PM plugin. Enabling...');
that.enabledPmPlugins.push(pluginName.toLowerCase());
}
});
} }
loadAllPlugins() { loadAllPlugins() {
var that = this; var that = this;
this.enabledPlugins.forEach(function(pluginName) { this.enabledPlugins.forEach(function(pluginName) {
var Plugin = require('./' + pluginName + '.js'); var Plugin = require('./' + pluginName + '.js');
var plugin = new Plugin(that.config); var plugin = new Plugin(that.config);
that.plugins.push(plugin); that.plugins.push(plugin);
}); });
this.enabledPmPlugins.forEach(function(pluginName) {
var Plugin = require('./pm' + pluginName + '.js');
var plugin = new Plugin(that.config);
that.pmPlugins.push(plugin);
});
} }
testPlugins(input) { testPlugins(input, publicChat) {
if (typeof publicChat === 'undefined') {
publicChat = true
}
var promises = []; var promises = [];
for(let i = 0; i < this.plugins.length; i++) {
var plugin = this.plugins[i];
if(plugin.test(input)) { if (publicChat) {
logger.info('* Plugin ' + plugin.name + ' reported hit'); for(let i = 0; i < this.plugins.length; i++) {
var plugin = this.plugins[i];
if(plugin.test(input)) {
logger.info('* Plugin ' + plugin.name + ' reported hit');
promises.push(plugin.result(input));
}
}
} else {
for(let i = 0; i < this.pmPlugins.length; i++) {
var plugin = this.pmPlugins[i];
promises.push(plugin.result(input)); if(plugin.test(input)) {
logger.info('* Plugin ' + plugin.name + ' reported hit');
promises.push(plugin.result(input));
}
} }
} }
return Promise.all(promises); return Promise.all(promises);
} }

31
plugins/pmusers.js Normal file
View File

@ -0,0 +1,31 @@
var Promise = require('promise');
var requestPromise = require('./helpers/requestPromise.js');
var BasePlugin = require('./base.js');
var logger = require('winston');
class Users extends BasePlugin {
constructor(config) {
super(config);
this.name = 'Users';
}
test(input) {
var res = null;
if (res = input.match(/.*/i)) {
return true;
}
return false;
}
result(input) {
// Different cases
let info = '';
info += 'Hei! Olen ' + this.config.irc.nick + '!' + '\n';
info += 'Tarjoan yksityisviestien kautta ominaisuuksia!' + '\n';
info += 'Voidaksesi käyttää niitä, sinun on rekisteröidyttävä lähettämällä viesti \".rekisteröi\".';
return info;
}
}
module.exports = Users;

185
plugins/weather.js Normal file
View File

@ -0,0 +1,185 @@
var Promise = require('promise');
var requestPromise = require('./helpers/requestPromise.js');
var BasePlugin = require('./base.js');
var logger = require('winston');
class Weather extends BasePlugin {
constructor(config) {
super(config);
this.name = 'Weather';
this.apikey = config.keys.owm.apiKey;
this.iconCodeToText = {
'01d': 'selkeää',
'02d': 'puolipilvistä',
'03d': 'pilvistä',
'04d': 'pilvipeite rakoilee',
'09d': 'sadekuuroja',
'10d': 'vesisadetta',
'11d': 'ukkostaa',
'13d': 'lumisadetta',
'50d': 'sumua',
'01n': 'selkeää',
'02n': 'puolipilvistä',
'03n': 'pilvistä',
'04n': 'pilvipeite rakoilee',
'09n': 'sadekuuroja',
'10n': 'vesisadetta',
'11n': 'ukkostaa',
'13n': 'lumisadetta',
'50n': 'sumua'
};
}
test(input) {
var res = null;
if (res = input.match(/^\.saa(.*)/i)) {
return true;
}
return false;
}
getTemperature(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;
}
getTemperatures(cities, code) {
logger.info('getting temperatures ', cities, code);
if(typeof code === 'undefined') {
code = 'fi';
}
var language = 'fi';
var idList = '';
for(var city in cities) {
idList += cities[city] + ',';
}
idList = idList.substring(0,idList.length - 1);
var urlArgs = {
'id': idList,
'lang': language,
'APPID': this.apikey
};
var url = 'http://api.openweathermap.org/data/2.5/group';
var promise = requestPromise(url, urlArgs);
return promise;
}
getPenaTemperatures(start, stop, limit) {
logger.info('getting pena', start, stop, limit);
var d = new Date();
var urlArgs = {};
if(typeof limit === 'undefined') {
limit = 20;
}
urlArgs.limit = limit;
if(typeof start !== 'undefined' && typeof stop !== 'undefined') {
urlArgs.limit = limit;
urlArgs.start = start;
urlArgs.stop = stop;
}
var url = 'http://masa.dy.fi/api/temperature.php';
var promise = requestPromise(url, urlArgs);
return promise;
}
getTemperatureString(owmCityIds) {
let that = this;
var promise = new Promise(function(resolve, reject) {
Promise.all([
that.getTemperatures(owmCityIds),
that.getPenaTemperatures()
]).then(function(results) {
var resultString = '';
// OWM
results[0].list.forEach(function(data) {
var city = data.name;
var temp = (data.main.temp-273.15).toFixed(1);
var code = data.weather[0].icon;
resultString += '' + city + ' ('+temp+' °C, '+that.iconCodeToText[code]+'), ';
});
if(results[1].length > 0) {
var temp = Number(results[1][0].temp).toFixed(1);
resultString += 'Aura (' + temp + ' °C)';
}
resolve(resultString);
}, (str) => reject(str));
});
return promise;
}
result(input) {
// Different cases
let that = this;
var resultPromise = new Promise(function(resultResolve, resultReject) {
if (input.match(/^\.sää$/i) || input.match(/^\.saa$/i)) {
that.getTemperatureString(that.config.owmCityIds).then(function(str) {
resultResolve('Sää: ' + str);
}, function(str) {
resultReject(str)
});
}
var res = null;
// Weather
if (res = input.match(/^\.s(ää|aa) ([^0-9]+)$/i)) {
//console.log('kaupunki', res[1]);
that.getTemperature(res[2]).then(function(result) {
//console.log(result);
if(result.cod === '404') {
//console.log('Eioo: ' + res[1]);
client.say(to, 'Eioo: ' + res[2]);
} else {
var city = result.name;
var country = result.sys.country;
var temp = (result.main.temp-273.15).toFixed(1);
var code = result.weather[0].icon;
var sunriseDate = new Date(result.sys.sunrise*1000);
var sunsetDate = new Date(result.sys.sunset*1000);
var sunrise = sunriseDate.getHours() + '.' + sunriseDate.getMinutes();
var sunset = sunsetDate.getHours() + '.' + sunsetDate.getMinutes();
var humidity = result.main.humidity;
var pressure = result.main.pressure;
var windspeed = result.wind.speed;
let str = 'Sää: '+city+', '+country+' ('+temp+' °C, '+ that.iconCodeToText[code] +', ilmankosteus: '+humidity+' %, ilmanpaine: '+pressure+' hPa, tuulen nopeus: '+windspeed+' m/s)';
resultResolve('Sää: ' + str);
}
}, function(error) {
logger.error('ERROR ',error);
resultResolve('Oho! Tapahtui virhe. Yritä myöhemmin uudelleen...');
});
}
});
return resultPromise;
}
}
module.exports = Weather;