import { Logger } from './LoggerHelper'; import { Config } from './ConfigHelper'; import * as redis from 'redis'; import * as bluebird from 'bluebird'; import { ZenithClient } from '../Client'; export class Redis { private Log: Logger; private Config: Config; readonly Redis: redis.RedisClient; public get: Function; public set: Function; public getAsync: Function; public setAsync: Function; private Client: ZenithClient; constructor(client) { this.Config = new Config(); this.Log = new Logger('Redis'); this.Client = client; bluebird.promisifyAll(redis.RedisClient.prototype); bluebird.promisifyAll(redis.Multi.prototype); this.Redis = redis.createClient({ host: this.Config.get('REDIS.HOST', '127.0.0.1'), port: this.Config.get('REDIS.PORT', 6379), password: this.Config.get('REDIS.PASSWORD', ''), prefix: this.Config.get('REDIS.PREFIX', 'zenith'), }); this.Redis.on('ready', this.onReady.bind(this)); this.Redis.on('error', this.onError.bind(this)); this.Redis.on('end', this.onEnd.bind(this)); this.Redis.on('warning', this.onWarning.bind(this)); return this; } onReady(): void { this.Log.info('Connected.'); } onWarning(warning): void { this.Log.warn(warning); } onError(err): void { this.Log.error(err); } onEnd(): void { this.Log.info('Disconnected.'); } quit(): void { this.Redis.quit(); } async getPrefix(guildID) { return await this.Redis.getAsync(`${guildID}.prefix`) .then((res, error) => { if (error) throw error; if (res === null || res === 'null') return this.Client.prefix; return res; }); } async getMaintenance() { return await this.Redis.getAsync('maintenance') .then((res, error) => { if (error) throw error; if (res === null || res === 'null') return 0; return res; }); } }