// IRC Bot var config = { irc: { server: 'irc.elisa.fi', nick: 'Ookami-dev', channels: ['#kulmapaikka-dev'] } }; var members = { 'lanxu': { user: /julankin$/, host: /^jumi\.lut\.fi$/ }, 'asdfas': { user: /asdfas$/, host: /(.*)\.ssp\.fi$/ }, 'asdfas2': { user: /Asdfas$/, host: /(.*)\.elisa-mobile\.fi$/ }, 'cbd': { user: /cbd$/, host: /kapsi\.fi$/ }, 'fitzhood': { user: /Fitzhood$/, host: /(.*)ssp\.fi$/ }, 'mindflayer': { user: /mindflaye$/, host: /(.*)opintanner\.fi$/ }, 'pariah': { user: /rautia$/, host: /mozart\.cc\.tut\.fi$/ }, 'zoid': { user: /jolaakso$/, host: /kapsi\.fi$/ }, 'palin': { user: /Palin$/, host: /(.*)\.elisa-mobile\.fi$/ }, 'rootmer': { user: /ror$/, host: /(.*)\.inet\.fi$/ }, 'masaKapsi': { user: /maruohon$/, host: /kapsi\.fi$/ }, 'masaJumi': { user: /maruohon$/, host: /jumi\.lut\.fi$/ }, 'masaAura': { user: /masa$/, host: /(.*)\.ssp\.fi$/ } }; var irc = require('irc'); var penaapi = require('./penaapi')(); var owmapi = require('./owmapi')(); var owmCityIds = { Tampere: 634963, Karinainen: 654059, Lappeenranta: 648900 }; 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' }; 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; }; 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 = ''; // 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; }; 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 text = 'Hei! https://youtu.be/TLW6eTV8F_I hienoo'; var text = 'Hei! https://www.youtube.com/watch?v=AMlf-KXVOsQ hienoo'; var text = 'Hei! https://www.youtu.be/TLW6eTV8F_I https://www.youtube.com/watch?v=AMlf-KXVOsQ hienoo'; var regExp = /http(s)?:\/\/(www\.)?((youtu\.be\/([a-zA-Z0-9_-]*))|(youtube\.com\/watch\?v=([a-zA-Z0-9_-]*)))/; var res = text.match(regExp); console.log(res[5]); console.log(res[7]); /* var getYoutubeTitle = function(query) { var google = require('googleapis'); var apikey = 'AIzaSyBCLnmbmjjHvfScHNj8PBqINjoyLTAXEjg'; var youtube = google.youtube('v3'); var params = { part: 'snippet, statistics', q: 'https://www.youtube.com/watch?v=XNwKqvCZ4kU', type: 'video', key: apikey }; var result = youtube.search.list(params, function(err, resp) { if (err) { console.log('An error occured', err); return; } console.log(resp.items[0].snippet.title); console.log(resp.items[0].snippet.channelTitle); console.log(resp); }); console.log(result); //var resultString = 'Title: '+; }; */ //console.log('YouTube!', getYoutubeTitle()); /* getTemperatureString().then(function(result) { console.log(result); }); */ /* 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 country = result.sys.country; var temp = (result.main.temp-273.15).toFixed(1); 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; console.log(city, temp, country, sunrise, sunset, humidity, pressure); } }, 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 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; //console.log(city, temp, code); 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...'); }); } 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 (res = message.match(/^\.luohahmo$/i)) { var character = getDnDCharacter(); client.say(to, ''+character); } if (message.match(/^\.heit(a|ä) voltti homo$/i)) { client.say(to, 'steam://run/1250'); } if (message.match(/^\.op$/i)) { client.send('NAMES', to); } if (res = message.match(/http(s)?:\/\/(www\.)?((youtu\.be\/([a-zA-Z0-9_-]*))|(youtube\.com\/watch\?v=([a-zA-Z0-9_-]*)))/)) { var googletin = require('./googleapi.js')(); console.log(res); var id = res[7]; if(typeof id === 'undefined') { id = res[5]; } if(typeof id !== 'undefined') { googletin.getYoutubeVideoById(id).then(function(result) { var resultString = 'Youtube: '; resultString += result.title; resultString += ', esittäjänä ' + result.channelTitle; var rotten = (Number(result.statistics.likeCount)/(Number(result.statistics.likeCount)+Number(result.statistics.dislikeCount))).toFixed(2); //resultString += ' ('+result.duration+')'; resultString += ' ('+result.duration+', '+ result.statistics.viewCount +' katselukertaa, '+rotten*100+'% tykätty, '+result.url+' )'; //console.log(resultString); client.say(to, ''+resultString); }, function(err) { console.log('error', err); client.say(to, 'Youtube: jokin meni pieleen...'); }); } } if (res = message.match(/^\.youtube (.+)$/i)) { var googletin = require('./googleapi.js')(); console.log(res); googletin.getYoutubeVideo(res[1]).then(function(result) { var resultString = 'Youtube: '; resultString += result.title; resultString += ', esittäjänä ' + result.channelTitle; var rotten = (Number(result.statistics.likeCount)/(Number(result.statistics.likeCount)+Number(result.statistics.dislikeCount))).toFixed(2); //resultString += ' ('+result.duration+')'; resultString += ' ('+result.duration+', '+ result.statistics.viewCount +' katselukertaa, '+rotten*100+'% tykätty, '+result.url+' )'; //console.log(resultString); client.say(to, ''+resultString); }, function(err) { console.log('error'); client.say(to, 'Youtube: jokin meni pieleen...'); }); } } }); client.addListener('join', function(channel, who) { client.whois(who, function(result) { console.log(result); for(var memberNick in members) { var member = members[memberNick]; if(result.host.match(member.host) && result.user.match(member.user)) { client.send('MODE', channel, '+o', result.nick); } } }); setTimeout(function() { // client.say(channel, 'Moi ' + who + '!'); }, 4000); }); client.addListener('error', function(message) { console.log('error: ', message); }); client.addListener('names', function(channel, nicks) { console.log('names', channel, nicks); if(typeof nicks[config.irc.nick] !== 'undefined' && nicks[config.irc.nick] === '@') { // go through nicks for(var nick in nicks) { if(nicks[nick] !== '@') { client.whois(nick, function(result) { for(var memberNick in members) { var member = members[memberNick]; if(result.host.match(member.host) && result.user.match(member.user)) { console.log('testing', channel, result.nick); client.send('MODE', channel, '+o', result.nick); } } }); } } } });