From 68b467ac716b8d50bbdf6194f9c2dd0641a3dec9 Mon Sep 17 00:00:00 2001 From: Emily J Date: Tue, 18 Aug 2020 21:28:28 +1000 Subject: [PATCH] redis stuff --- bot/index.js | 59 ++++++++++++++++++++++++++++++----------------- bot/util/redis.js | 39 +++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 21 deletions(-) create mode 100644 bot/util/redis.js diff --git a/bot/index.js b/bot/index.js index 4004831..c83a9d3 100644 --- a/bot/index.js +++ b/bot/index.js @@ -13,17 +13,19 @@ class Custom extends Client { constructor (options) { super(options); - this.path = __dirname; this.config = require("../config.json"); + this.dev = true; + if (this.config.devmode === false) { + this.dev = false; + //sentry.init({ dsn: this.config.keys.sentry }); + } + + this.path = __dirname; this.logger = require("./util/logger"); this.util = new (require("./util/util"))(this); this.messageUtil = new (require("./util/messageUtil"))(this); - this.dev = false; - - if (this.config.devmode === true) { - this.dev = true; - } - + this.db = new (require("./util/redis"))(this) + // Create collections to store loaded commands and aliases in this.commands = new Collection(); this.aliases = new Collection(); @@ -37,20 +39,35 @@ class Custom extends Client { } } -// Initialize client -const client = new Custom(); -/* Enable this later -if (client.dev !== true) { - sentry.init({ dsn: client.config.keys.sentry }); -} -*/ +// Initialization function, so we can use async/await +const init = async () => { + // Initialize client + const client = new Custom(); -client.commandHandler.loadAll(); -client.eventHandler.loadAll(); + // Initialize Redis database + await client.db.init(); -if (client.dev === true) { - client.logger.warn("Development mode is on. Some features (such as Sentry) are disabled."); - client.login(client.config.devtoken); -} else { - client.login(client.config.token); + await client.commandHandler.loadAll(); + await client.eventHandler.loadAll(); + + if (client.dev === true) { + client.logger.warn("Development mode is on. Some features (such as Sentry) are disabled."); + client.login(client.config.devtoken); + } else { + client.login(client.config.token); + } } + +init(); + +// Catch exceptions/rejections and give more details on the stack trace +process.on("uncaughtException", (err) => { + const errorMsg = err.stack.replace(new RegExp(`${__dirname}/`, "g"), "./") + console.error("Uncaught Exception: ", errorMsg) + process.exit(1) +}); + +process.on("unhandledRejection", err => { + console.error("Uncaught Promise Error: ", err) +}); + diff --git a/bot/util/redis.js b/bot/util/redis.js new file mode 100644 index 0000000..c741381 --- /dev/null +++ b/bot/util/redis.js @@ -0,0 +1,39 @@ +const redis = require('redis') +const generators = require('redis-async-gen') + +class Redis { + constructor(client) { + // Create redis client + this.global = redis.createClient(this.client.config.redis) + this.server = this.global.duplicate({ db: 1 }) + this.member = this.global.duplicate({ db: 2 }) + this.user = this.global.duplicate({ db: 3 }) + + // Deletes specified guild entry + this.deleteGuild = async function (id) { + this.server.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) + } + } + + // Deletes specified user and their member entries in guilds + this.deleteUser = async function (id) { + this.user.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) + } + } + + // Deletes member of user in specified guild + this.deleteMember = async function (guildId, id) { + this.member.del(guildId + '-' + id) + } + } +} + +module.exports = Redis \ No newline at end of file