Uwev2/src/inc/wrapper.js

67 lines
1.6 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';
2017-11-08 18:43:08 +00:00
let irclib = require('./irc/irc.js');
2017-11-08 20:40:59 +00:00
let tglib = require('node-telegram-bot-api');
2017-11-08 18:43:08 +00:00
//const safeEval = require('safe-eval');
2017-11-08 11:56:04 +00:00
const util = require('util');
2017-11-08 18:43:08 +00:00
//const fs = require('fs');
//const ytdl = util.promisify(require('youtube-dl').getInfo);
2017-11-08 11:56:04 +00:00
var EventEmitter = require('events').EventEmitter;
2017-11-08 19:30:21 +00:00
let clients = [];
2017-11-08 11:56:04 +00:00
const wrapper = function() {
2017-11-08 20:40:59 +00:00
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;
}
}
2017-11-08 11:56:04 +00:00
2017-11-08 19:30:21 +00:00
clients.forEach(client => {
client.client.on("message", e => {
if(client.type === "tg")
e = replytg(client.client, e);
this.emit('message', e);
});
});
2017-11-08 11:56:04 +00:00
};
2017-11-08 19:30:21 +00:00
const replytg = (bot, tmp) => { // muss noch hier raus
2017-11-08 11:56:04 +00:00
return {
2017-11-08 19:30:21 +00:00
type: "tg",
2017-11-08 11:56:04 +00:00
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) {
2017-11-08 19:30:21 +00:00
bot.sendMessage(tmp.chat.id, msg);
},
2017-11-08 11:56:04 +00:00
replyAction: function(msg) {
2017-11-08 19:30:21 +00:00
bot.sendMessage(tmp.chat.id, msg);
2017-11-08 11:56:04 +00:00
},
replyNotice: function(msg) {
2017-11-08 19:30:21 +00:00
bot.sendMessage(tmp.chat.id, msg);
2017-11-08 11:56:04 +00:00
}
};
};
util.inherits(wrapper, EventEmitter);
2017-11-08 21:29:43 +00:00
export default { wrapper, clients };