Uwev2/src/inc/wrapper.js
2017-11-08 22:30:47 +01:00

67 lines
1.6 KiB
JavaScript

import { cfg, read } from './cfg.js';
import { loadEvents } from './lib.js';
let irclib = require('./irc.js');
let tglib = 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 clients = [];
const wrapper = function() {
for(let srv in cfg.client) {
switch(cfg.client[srv].type) {
case "irc":
clients.push({
name: cfg.client[srv].network,
type: "irc",
client: new irclib(cfg.client[srv])
});
break;
case "tg":
clients.push({
name: "tg",
type: "tg",
client: new tglib(cfg.client[srv].token, { polling: cfg.client[srv].polling })
});
break;
}
}
clients.forEach(client => {
client.client.on("message", e => {
if(client.type === "tg")
e = replytg(client.client, e);
this.emit('message', e);
});
});
};
const replytg = (bot, tmp) => { // muss noch hier raus
return {
type: "tg",
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(tmp.chat.id, msg);
},
replyAction: function(msg) {
bot.sendMessage(tmp.chat.id, msg);
},
replyNotice: function(msg) {
bot.sendMessage(tmp.chat.id, msg);
}
};
};
util.inherits(wrapper, EventEmitter);
export default { wrapper, clients };