redis module finished!
This commit is contained in:
parent
a41173f441
commit
faa168d07c
1 changed files with 85 additions and 80 deletions
|
@ -1,94 +1,110 @@
|
||||||
const redis = require('redis')
|
/*
|
||||||
|
* - All get/set functions will return nothing if the key is not found in the database.
|
||||||
|
* - If you want to delete a key, use hdelAsync. Writing extended functions for deleting keys is pointless.
|
||||||
|
* - Member key names are formatted as "guildID-userID". Use this.member.del(guildId + '-' + id) to delete member data.
|
||||||
|
*/
|
||||||
|
|
||||||
|
const { promisifyAll } = require('bluebird');
|
||||||
|
const redis = promisifyAll(require('redis'));
|
||||||
const generators = require('redis-async-gen');
|
const generators = require('redis-async-gen');
|
||||||
const { data } = require('./logger');
|
|
||||||
const { promisify } = require('util');
|
|
||||||
|
|
||||||
class Redis {
|
class Redis {
|
||||||
constructor(client) {
|
constructor(client) {
|
||||||
this.client = client;
|
this.client = client;
|
||||||
}
|
|
||||||
|
|
||||||
init () {
|
|
||||||
const conf = this.client.config.redis
|
|
||||||
|
|
||||||
// Create redis client
|
// Create redis client
|
||||||
this.global = redis.createClient(conf)
|
this.global = redis.createClient(this.client.config.redis)
|
||||||
this.guild = this.global.duplicate({ db: 1 })
|
this.guild = this.global.duplicate({ db: 1 })
|
||||||
this.member = this.global.duplicate({ db: 2 })
|
this.member = this.global.duplicate({ db: 2 })
|
||||||
this.user = this.global.duplicate({ db: 3 })
|
this.user = this.global.duplicate({ db: 3 })
|
||||||
|
|
||||||
// Promises
|
// Monitor for events emitted by Redis client
|
||||||
this.guildGetAsync = promisify(this.guild.hget).bind(this.guild)
|
this.global.on('ready', () => {
|
||||||
this.memberGetAsync = promisify(this.member.hget).bind(this.member)
|
this.client.logger.info('Connected to Redis DB.');
|
||||||
this.userGetASync = promisify(this.user.hget).bind(this.user)
|
});
|
||||||
|
|
||||||
this.guildGetAllAsync = promisify(this.guild.hgetall).bind(this.guild)
|
this.global.on('reconnecting', () => {
|
||||||
this.memberGetAllAsync = promisify(this.member.hgetall).bind(this.member)
|
this.client.logger.info('Attempting to reconnect to Redis DB...');
|
||||||
this.userGetAllAsync = promisify(this.user.hgetall).bind(this.user)
|
});
|
||||||
};
|
|
||||||
|
|
||||||
async guildGet (id, key) {
|
this.global.on('error', (err) => {
|
||||||
let result = await this.client.db.guildGetAsync(id, key);
|
this.client.logger.error('Redis error: ' + err);
|
||||||
|
});
|
||||||
|
|
||||||
if(result === null) {
|
this.global.on('warning', (warn) => {
|
||||||
result = this.client.config.defaultGuildData[key]
|
this.client.logger.warn('Redis warning: ' + warn);
|
||||||
|
});
|
||||||
if (!result) {
|
|
||||||
throw new Error('The key provided is invalid. Please check for typing errors.')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async guildSet (id, key, newValue) {
|
async getGuildKey (id, key) {
|
||||||
const oldValue = this.client.db.guildGetAsync(id, key);
|
let result = await this.guild.hgetAsync(id, key);
|
||||||
|
|
||||||
if (oldValue === newValue) {
|
// Return value in config if no override is found in the database
|
||||||
return 'This setting already has that value!'
|
if(result === null) result = this.client.config.defaultGuildData[key];
|
||||||
};
|
|
||||||
|
|
||||||
if (!this.client.config.defaultGuildData[key]) {
|
return result;
|
||||||
return 'I couldn\'t find this setting, so it probably doesn\'t exist. Check for typos!'
|
};
|
||||||
};
|
|
||||||
|
|
||||||
if (newValue === this.client.config.defaultGuildData[key]) {
|
async getMemberKey (id, key) {
|
||||||
this.client.db.guildDeleteKey(id, key); // Delete duplicates and use defaults in config file
|
let result = await this.member.hgetAsync(id, key);
|
||||||
|
|
||||||
|
if(result === null) result = this.client.config.defaultMemberData[key];
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
async getUserKey (id, key) {
|
||||||
|
let result = await this.user.hgetAsync(id, key);
|
||||||
|
|
||||||
|
if(result === null) result = this.client.config.defaultUserData[key];
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
|
||||||
|
async setGuildKey (id, key, newValue) {
|
||||||
|
const def = this.client.config.defaultGuildData[key];
|
||||||
|
|
||||||
|
if (!def) return;
|
||||||
|
|
||||||
|
if(def === newValue) {
|
||||||
|
await this.guild.hdel(id, key); // If new value matches the default, delete the override.
|
||||||
} else {
|
} else {
|
||||||
this.client.db.guild.hset(id, key, newValue);
|
await this.guild.hsetAsync(id, key, newValue);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
async memberGet (id, key) {
|
async setMemberKey (id, key, newValue) {
|
||||||
let result = await this.client.db.memberGetAsync(id, key);
|
const def = this.client.config.defaultMemberData[key];
|
||||||
|
|
||||||
if(result === null) {
|
if (!def) return;
|
||||||
result = this.client.config.defaultMemberData[key]
|
|
||||||
|
if(def === newValue) {
|
||||||
|
await this.member.hdel(id, key);
|
||||||
|
} else {
|
||||||
|
await this.member.hsetAsync(id, key, newValue);
|
||||||
|
};
|
||||||
|
|
||||||
if (!result) {
|
return true;
|
||||||
throw new Error('The key provided is invalid. Please check for typing errors.')
|
};
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
async setUserKey (id, key, newValue) {
|
||||||
}
|
const def = this.client.config.defaultUserData[key];
|
||||||
|
|
||||||
async userGet (id, key) {
|
if (!def) return;
|
||||||
let result = await this.client.db.userGetAsync(id, key);
|
|
||||||
|
if(def === newValue) {
|
||||||
|
await this.user.hdel(id, key);
|
||||||
|
} else {
|
||||||
|
await this.user.hsetAsync(id, key, newValue);
|
||||||
|
};
|
||||||
|
|
||||||
if(result === null) {
|
return true;
|
||||||
result = this.client.config.defaultUserData[key]
|
};
|
||||||
|
|
||||||
if (!result) {
|
// Deletes all data associated with a guild, INCLUDING MEMBER DATA
|
||||||
throw new Error('The key provided is invalid. Please check for typing errors.')
|
async purgeGuild (id) {
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deletes all data associated with a guild
|
|
||||||
async guildDelete (id) {
|
|
||||||
this.guild.del(id)
|
this.guild.del(id)
|
||||||
var { keysMatching } = await generators.using(this.member)
|
var { keysMatching } = await generators.using(this.member)
|
||||||
// eslint-disable-next-line no-unused-vars
|
// eslint-disable-next-line no-unused-vars
|
||||||
|
@ -96,27 +112,16 @@ class Redis {
|
||||||
this.member.del(key)
|
this.member.del(key)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async guildDeleteKey (id, key) {
|
|
||||||
this.guild.hdel(id, key)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deletes specified user. If deleteAll, also delete their member entries in guilds
|
// Deletes all data associated with a user, INCLUDING MEMBER DATA
|
||||||
async userDelete (id, deleteAll) {
|
async purgeUser (id) {
|
||||||
this.user.del(id)
|
this.user.del(id)
|
||||||
if (deleteAll) {
|
var { keysMatching } = await generators.using(this.member)
|
||||||
var { keysMatching } = await generators.using(this.member)
|
// eslint-disable-next-line no-unused-vars
|
||||||
// eslint-disable-next-line no-unused-vars
|
for await (const key of keysMatching('*-' + id)) {
|
||||||
for await (const key of keysMatching('*-' + id)) {
|
this.member.del(key)
|
||||||
this.member.del(key)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deletes member of user in specified guild
|
|
||||||
async memberDelete (guildId, id) {
|
|
||||||
this.member.del(guildId + '-' + id)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = Redis;
|
module.exports = Redis;
|
Loading…
Reference in a new issue