2015-06-20 03:04:20 +03:00

173 lines
4.0 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// IRC Bot
var config = {
irc: {
server: 'irc.elisa.fi',
nick: 'Ookami-dev',
channels: ['#kulmapaikka-dev', '#hi-tech', '#kulmapaikka']
}
};
var members = [
{
nick: 'lanxu',
hostmask: ''
}
];
var irc = require('irc');
var penaapi = require('./penaapi')();
var owmapi = require('./owmapi')();
var owmCityIds = {
Tampere: 634963,
Karinainen: 654059,
Lappeenranta: 648900
};
var getTemperatureString = function() {
var Promise = require('promise');
var promise = new Promise(function(resolve, reject) {
Promise.all([
owmapi.getTemperatures(owmCityIds),
penaapi.getTemperatures()
]).then(function(results) {
var resultString = '';
//console.log(results);
// OWM
results[0].list.forEach(function(data) {
//console.log('' + data.name + ': ' +(data.main.temp-273.15).toFixed(1));
var city = data.name;
var temp = (data.main.temp-273.15).toFixed(1);
resultString += '' + city + ' ('+temp+' °C), ';
});
if(results[1].length > 0) {
//console.log(result[0].temp);
var temp = Number(results[1][0].temp).toFixed(1);
resultString += 'Aura (' + temp + ' °C)';
}
//console.log('Temp:', resultString);
// Pena
resolve(resultString);
});
});
return promise;
};
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;
};
/*
var res = null;
var message= '.heita 4d6';
if (res = message.match(/^\.heita ([0-9]+)d([0-9]+)$/i)) {
console.log(getDiceString(res[1], res[2]));
}
*/
/*
var message = '.saa Tampere'; var res = null;
if (res = message.match(/^\.saa ([^0-9 ]+)$/i)) {
console.log('kaupunki', res[1]);
owmapi.getTemperature(res[1]).then(function(result) {
console.log(result);
if(result.cod === '404') {
console.log('Eioo');
} else {
var city = result.name;
var temp = (result.main.temp-273.15).toFixed(1);
console.log(city, temp);
}
}, function(error) {
console.log('fas',error);
});
}
*/
var client = new irc.Client(config.irc.server, config.irc.nick, {
debug: true,
channels: config.irc.channels,
});
client.addListener('message', function (from, to, message) {
console.log(from + ' => ' + to + ': ' + message);
if (to.match(/^[#&]/)) {
// channel message
if (message.match(/^\.sää$/i) || message.match(/^\.saa$/i)) {
getTemperatureString().then(function(str) {
client.say(to, 'Sää: ' + str);
});
}
var res = null;
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 temp = (result.main.temp-273.15).toFixed(1);
//console.log(city, temp);
client.say(to, 'Sää: '+city+' ('+temp+' °C)');
}
}, function(error) {
console.log('ERROR ',error);
client.say(to, 'Oho! Tapahtui virhe. Yritä myöhemmin uudelleen...');
});
}
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');
}
}
});
client.addListener('join', function(channel, who) {
setTimeout(function() {
// client.say(channel, 'Moi ' + who + '!');
}, 4000);
});