import { Helper } from '../Core'; const fs = require("fs"); const redis = require("redis"); const Rx = require("rx").Rx; var client = redis.createClient(); client.on("error", function(err) { console.log("Error " + err); }); // class ServerManager{ // static config: any; // static update() { // ServerManager.config = JSON.parse(fs.readFileSync(`data/redis.json`)); // } // static init() { // var config = { // servers: [ // { // name: "example", // host: "localhost", // port: 1000, // auth: "JKHGJKG@HJB*HJBJDHB" // } // ] // }; // fs.writeFileSync( // "data/redis.json", // JSON.stringify(config, null, 2), // function(err) { // if (err) throw new Error(err); // } // ); // } // } Rx.Observable.prototype.redis = function(command) { var source = this; return Rx.Observable.create(observer => { return source.subscribe( msg => { try { var c = Helper.getValue(msg, command).split(' ')[0]; var a = Helper.getValue(msg, command).split(' ').splice(1); client.send_command(c, a, (err, res) => { msg.redis = "Result: " + res; observer.onNext(msg); }); } catch (e) { observer.onError(e); } }, observer.onError.bind(observer), observer.onCompleted.bind(observer) ); }); }; export class Redis { static pluginName: string = "redis"; static description: string = "Redis"; static config: any; static apply(bot: Bot) { bot .hear("redis init") .do(m => ServerManager.init()) .reply(m => "Done. Now add your servers to /data/redis.json") .start(); bot .hear("redis {command}") .redis(m => m.command) .reply(m => m.redis) .start(); } }