aboutsummaryrefslogtreecommitdiff
path: root/src/tasks/cache/updateCache.ts
diff options
context:
space:
mode:
authorIRONM00N <64110067+IRONM00N@users.noreply.github.com>2022-07-11 18:44:09 +0200
committerIRONM00N <64110067+IRONM00N@users.noreply.github.com>2022-07-11 18:44:09 +0200
commitd2a020837267bf024f508b402f596f8e242672ed (patch)
tree7e371015fa1451bda993e44aa591b3cc24c3209d /src/tasks/cache/updateCache.ts
parent7d1dede862339c439830a0306e2b7f3a273f967f (diff)
downloadtanzanite-d2a020837267bf024f508b402f596f8e242672ed.tar.gz
tanzanite-d2a020837267bf024f508b402f596f8e242672ed.tar.bz2
tanzanite-d2a020837267bf024f508b402f596f8e242672ed.zip
create task categories
Diffstat (limited to 'src/tasks/cache/updateCache.ts')
-rw-r--r--src/tasks/cache/updateCache.ts60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/tasks/cache/updateCache.ts b/src/tasks/cache/updateCache.ts
new file mode 100644
index 0000000..87636e8
--- /dev/null
+++ b/src/tasks/cache/updateCache.ts
@@ -0,0 +1,60 @@
+import { Time } from '#constants';
+import { Global, Guild, Shared, type BushClient } from '#lib';
+import type { Client } from 'discord.js';
+import config from '../../../config/options.js';
+import { BushTask } from '../../lib/extensions/discord-akairo/BushTask.js';
+
+export default class UpdateCacheTask extends BushTask {
+ public constructor() {
+ super('updateCache', {
+ delay: 5 * Time.Minute,
+ runOnStart: false // done in preinit task
+ });
+ }
+
+ public async exec() {
+ await Promise.all([
+ UpdateCacheTask.#updateGlobalCache(this.client),
+ UpdateCacheTask.#updateSharedCache(this.client),
+ UpdateCacheTask.#updateGuildCache(this.client)
+ ]);
+ void this.client.logger.verbose(`UpdateCache`, `Updated cache.`);
+ }
+
+ public static async init(client: BushClient) {
+ await Promise.all([
+ UpdateCacheTask.#updateGlobalCache(client),
+ UpdateCacheTask.#updateSharedCache(client),
+ UpdateCacheTask.#updateGuildCache(client)
+ ]);
+ }
+
+ static async #updateGlobalCache(client: Client) {
+ const environment = config.environment;
+ const row: { [x: string]: any } = ((await Global.findByPk(environment)) ?? (await Global.create({ environment }))).toJSON();
+
+ for (const option in row) {
+ if (Object.keys(client.cache.global).includes(option)) {
+ client.cache.global[option as keyof typeof client.cache.global] = row[option];
+ }
+ }
+ }
+
+ static async #updateSharedCache(client: Client) {
+ const row: { [x: string]: any } = ((await Shared.findByPk(0)) ?? (await Shared.create())).toJSON();
+
+ for (const option in row) {
+ if (Object.keys(client.cache.shared).includes(option)) {
+ client.cache.shared[option as keyof typeof client.cache.shared] = row[option];
+ if (option === 'superUsers') client.superUserID = row[option];
+ }
+ }
+ }
+
+ static async #updateGuildCache(client: Client) {
+ const rows = await Guild.findAll();
+ for (const row of rows) {
+ client.cache.guilds.set(row.id, row.toJSON() as Guild);
+ }
+ }
+}