woomy-v2/bot/util/redis.js

172 lines
4.2 KiB
JavaScript
Raw Normal View History

2020-10-10 00:38:18 +00:00
/*
* - 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'));
2020-10-09 05:04:28 +00:00
const generators = require('redis-async-gen');
class Redis {
constructor(client) {
this.client = client;
// Create redis client
2020-10-10 00:38:18 +00:00
this.global = redis.createClient(this.client.config.redis)
2020-10-09 05:04:28 +00:00
this.guild = this.global.duplicate({ db: 1 })
this.member = this.global.duplicate({ db: 2 })
this.user = this.global.duplicate({ db: 3 })
2020-10-10 00:38:18 +00:00
// Monitor for events emitted by Redis client
this.global.on('ready', () => {
this.client.logger.info('Connected to Redis DB.');
});
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
this.global.on('reconnecting', () => {
this.client.logger.info('Attempting to reconnect to Redis DB...');
});
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
this.global.on('error', (err) => {
this.client.logger.error('Redis error: ' + err);
});
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
this.global.on('warning', (warn) => {
this.client.logger.warn('Redis warning: ' + warn);
});
}
2020-10-09 05:04:28 +00:00
async getGuild (id) {
let result = await this.guild.hgetallAsync(id);
let defaults = this.client.config.defaultGuildData;
if (result === null) return defaults;
for (const defaultValue in defaults) {
if (!result[defaultValue]) {
result[defaultValue] = defaults[defaultValue];
};
};
return result;
};
async getMember (id) {
let result = await this.member.hgetallAsync(id);
let defaults = this.client.config.defaultMemberData;
if (result === null) return defaults;
for (const defaultValue in defaults) {
if (!result[defaultValue]) {
result[defaultValue] = defaults[defaultValue];
};
};
return result;
};
async getUser (id) {
let result = await this.user.hgetallAsync(id);
let defaults = this.client.config.defaultUserData;
if (result === null) return defaults;
for (const defaultValue in defaults) {
if (!result[defaultValue]) {
result[defaultValue] = defaults[defaultValue];
};
};
return result;
};
2020-10-10 00:38:18 +00:00
async getGuildKey (id, key) {
let result = await this.guild.hgetAsync(id, key);
// Return value in config if no override is found in the database
if(result === null) result = this.client.config.defaultGuildData[key];
2020-10-09 05:04:28 +00:00
return result;
2020-10-10 00:38:18 +00:00
};
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
async getMemberKey (id, key) {
let result = await this.member.hgetAsync(id, key);
2020-10-09 08:31:37 +00:00
2020-10-10 00:38:18 +00:00
if(result === null) result = this.client.config.defaultMemberData[key];
2020-10-09 08:31:37 +00:00
2020-10-10 00:38:18 +00:00
return result;
};
2020-10-09 08:31:37 +00:00
2020-10-10 00:38:18 +00:00
async getUserKey (id, key) {
let result = await this.user.hgetAsync(id, key);
if(result === null) result = this.client.config.defaultUserData[key];
return result;
2020-10-09 08:31:37 +00:00
};
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
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 {
await this.guild.hsetAsync(id, key, newValue);
};
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
return true;
};
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
async setMemberKey (id, key, newValue) {
const def = this.client.config.defaultMemberData[key];
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
if (!def) return;
if(def === newValue) {
await this.member.hdel(id, key);
} else {
await this.member.hsetAsync(id, key, newValue);
};
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
return true;
};
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
async setUserKey (id, key, newValue) {
const def = this.client.config.defaultUserData[key];
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
if (!def) return;
if(def === newValue) {
await this.user.hdel(id, key);
} else {
await this.user.hsetAsync(id, key, newValue);
};
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
return true;
};
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
// Deletes all data associated with a guild, INCLUDING MEMBER DATA
async purgeGuild (id) {
2020-10-09 05:04:28 +00:00
this.guild.del(id)
var { keysMatching } = await generators.using(this.member)
// eslint-disable-next-line no-unused-vars
for await (const key of keysMatching(id + '-*')) {
this.member.del(key)
2020-10-08 02:09:41 +00:00
}
2020-10-09 08:31:37 +00:00
}
2020-10-09 05:04:28 +00:00
2020-10-10 00:38:18 +00:00
// Deletes all data associated with a user, INCLUDING MEMBER DATA
async purgeUser (id) {
2020-10-09 05:04:28 +00:00
this.user.del(id)
2020-10-10 00:38:18 +00:00
var { keysMatching } = await generators.using(this.member)
// eslint-disable-next-line no-unused-vars
for await (const key of keysMatching('*-' + id)) {
this.member.del(key)
2020-10-09 05:04:28 +00:00
}
2020-10-10 00:38:18 +00:00
}
2020-08-18 11:28:28 +00:00
}
2020-10-09 05:04:28 +00:00
module.exports = Redis;