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
Irkkibotti kulmapaikkalaisille. Sisältää seuraavat huimat ominaisuudet:
- .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
- .luohahmo2
* Luo D&D-hahmon
|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 |
## Automaattiset toiminnot
- 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>

112
app.js
View File

@ -1,51 +1,22 @@
// General includes
var irc = require('irc');
var fs = require('fs');
var winston = require('winston');
const irc = require('irc');
const fs = require('fs');
const winston = require('winston');
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
var config = require('./config');
const config = require('./config');
// Load new plugins
var PluginManager = require('./plugins/pluginManager.js');
var pluginManager = new PluginManager(config);
const PluginManager = require('./plugins/pluginManager.js');
const pluginManager = new PluginManager(config);
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
// create a write stream (in append mode)
var logDirectory = __dirname + '/log';
const logDirectory = __dirname + '/log';
// ensure log directory exists
if(!fs.existsSync(logDirectory)) {
fs.mkdirSync(logDirectory);
@ -88,75 +59,8 @@ var handleMessage = function(from, to, message) {
logger.error('Plugin error: ', error);
});
//if (to.match(/^[#&]/)) {
// 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;
// Weather
if (res = message.match(/^\.s(ää|aa) ([^0-9]+)$/i)) {
//console.log('kaupunki', res[1]);
owmapi.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;
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);

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"
},
"dependencies": {
"chance": "=1.0.6",
"cron": "1.2.1",
"file-stream-rotator": "0.1.0",
"finalhandler": "1.0.0",
"googleapis": "18.0.0",
"html-to-text": "^3.3.0",
"irc": "0.5.2",
"mkdirp": "~0.5.1",
"morgan": "1.8.1",
"node-ddg-api": "0.1.4",
"node-dev": "3.1.3",
"node-uuid": "1.4.7",
"promise": "^7.0.0",
"request": "^2.55.0",
"request-promise": "^4.1.1",
"twitter": "1.7.0",
"winston": "2.3.1",
"winston-daily-rotate-file": "1.4.6",
"html-to-text": "3.3.0"
"chance": "=1.0.13",
"cron": "=1.3.0",
"file-stream-rotator": "=0.2.1",
"finalhandler": "=1.1.1",
"googleapis": "=29.0.0",
"html-to-text": "=4.0.0",
"irc": "=0.5.2",
"lokijs": "=1.5.3",
"mkdirp": "=0.5.1",
"morgan": "=1.9.0",
"node-dev": "=3.1.3",
"node-uuid": "=1.4.8",
"promise": "=8.0.1",
"request": "=2.85.0",
"request-promise": "=4.2.2",
"twitter": "=1.7.1",
"winston": "=2.4.2",
"winston-daily-rotate-file": "=3.1.2"
},
"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,6 +2,7 @@
class BasePlugin {
constructor(config) {
this.name = 'BasePlugin';
this.config = config;
}
test(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,8 +1,8 @@
var Promise = require('promise');
var requestPromise = require('./requestPromise.js');
var google = require('googleapis');
var youtube = google.youtube('v3');
const Promise = require('promise');
const requestPromise = require('./requestPromise.js');
const {google} = require('googleapis');
const youtube = google.youtube('v3');
const apis = google.getSupportedAPIs();
module.exports = function(config) {
'use strict';
var apikey = config.keys.google.apiKey;
@ -15,12 +15,10 @@ module.exports = function(config) {
key: apikey
};
console.log(videosParams);
var thisResolve = resolve;
var thisReject = reject;
youtube.videos.list(videosParams, function(err, resp) {
//console.log('respo', resp);
youtube.videos.list(videosParams, function(err, response) {
let resp = response.data;
if(typeof resp === 'undefined' || resp === null || typeof resp.items === 'undefined' || resp.items === null || resp.items.length <= 0) {
thisReject('No results');
return;
@ -36,9 +34,6 @@ module.exports = function(config) {
statistics: resp.items[0].statistics,
duration: formattedTime
};
console.log(resp);
console.log(resp.items[0]);
console.log(data);
thisResolve(data);
});
});

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 {
constructor(config) {
this.enabledPlugins = [];
this.enabledPmPlugins = [];
this.plugins = [];
this.pmPlugins = [];
this.config = config;
var that = this;
@ -17,7 +19,10 @@ class PluginManager {
logger.info('* Exists. Enabling...');
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());
}
});
}
@ -29,19 +34,41 @@ class PluginManager {
var plugin = new Plugin(that.config);
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, publicChat) {
if (typeof publicChat === 'undefined') {
publicChat = true
}
testPlugins(input) {
var promises = [];
if (publicChat) {
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];
if(plugin.test(input)) {
logger.info('* Plugin ' + plugin.name + ' reported hit');
promises.push(plugin.result(input));
}
}
}
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;