Uwev2/src/inc/wrapper.js

100 lines
2.7 KiB
JavaScript
Raw Normal View History

2017-11-08 11:56:04 +00:00
import { cfg, read } from './cfg.js';
import { loadEvents } from './lib.js';
//const TelegramBot = require('node-telegram-bot-api');
const safeEval = require('safe-eval');
const util = require('util');
const fs = require('fs');
const ytdl = util.promisify(require('youtube-dl').getInfo);
var EventEmitter = require('events').EventEmitter;
let irc = (new (require(`${__dirname}/irc/api`)).Api()).connect({
events: 31920,
rpc: 31930,
automaticSetup: true
});
const wrapper = function() {
//loadIRC();
//loadEvents();
//rpc.emit('call', 'n0xy', 'privmsg', [ '#kbot-dev', 'blah' ]);
for(let srv in cfg.irc) {
irc.rpc.emit('createClient', srv, cfg.irc[srv]);
console.log('Loading server', srv);
}
console.log('All servers have been loaded successfully');
const tg = new (require('node-telegram-bot-api'))('381368731:AAFalG-LknIbtBDuOvRXcxHUEK9Jg_o1UCw', { polling: true });
irc.events.on('message', msg => {
this.emit('message', msg);
//if( msg.event[1] === "privmsg" ) {
// let e = reply( msg );
// console.log( `IRC: (${e.time}) ${e.network} -> ${e.channel} -> ${e.user.nick}: ${e.message}` );
//}
});
tg.on('message', msg => {
this.emit('message', msg);
//let e = replytg(tg, msg);
//console.log( `TG: (${e.time}) ${e.channel} -> ${e.user.nick}: ${e.message}` );
});
};
const reply = tmp => {
return {
network: tmp.event[0],
channel: tmp.message.target,
user: {
nick: tmp.message.nickname,
username: tmp.message.username,
hostname: tmp.message.hostname
},
message: tmp.message.message,
time: tmp.message.time,
reply: msg => {
rpc.emit('call', this.network, 'privmsg', [ this.channel, msg ]);
},
replyAction: msg => {
rpc.emit('call', this.network, 'privmsg', [ this.channel, '\u0001' + 'ACTION ' + msg + '\u0001' ]);
},
replyNotice: msg => {
rpc.emit('call', this.network, 'notice', [ this.channel, msg ]);
}
};
};
const replytg = (bot, tmp) => {
return {
channel: tmp.chat.title,
channelid: tmp.chat.id,
user: {
nick: tmp.from.first_name,
username: tmp.from.username
},
message: tmp.text,
time: tmp.date,
reply: function(msg) {
bot.sendMessage(this.channelid, msg);
}.bind(this),
replyAction: function(msg) {
bot.sendMessage(this.channelid, msg);
},
replyNotice: function(msg) {
bot.sendMessage(this.channelid, msg);
}
};
};
const loadIRC = () => {
for(let srv in cfg.irc) {
rpc.emit('createClient', srv, cfg.irc[srv]);
console.log('Loading server', srv);
}
console.log('All servers have been loaded successfully');
};
util.inherits(wrapper, EventEmitter);
export default { wrapper };