aboutsummaryrefslogtreecommitdiff
path: root/src/tasks/updateCache.ts
blob: cfadeb5201e6b58d5a8e17e35f7440e9b4b89763 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import { BushClient } from '../lib/extensions/discord-akairo/BushClient';
import { BushTask } from '../lib/extensions/discord-akairo/BushTask';
import { Global } from '../lib/models/Global';
import config from './../config/options';

export class UpdateCacheTask extends BushTask {
	public constructor() {
		super('updateCache', {
			delay: 300_000, // 5 minutes
			runOnStart: false // done in preinit task
		});
	}
	public async exec(): Promise<void> {
		await UpdateCacheTask.updateGlobalCache(this.client);
		// await UpdateCacheTask.updateGuildCache(this.client);
		await this.client.logger.verbose(`UpdateCache`, `Updated cache.`);
	}

	public static async init(client: BushClient): Promise<void> {
		await UpdateCacheTask.updateGlobalCache(client);
		// await UpdateCacheTask.updateGuildCache(client);
	}

	private static async updateGlobalCache(client: BushClient): Promise<void> {
		const environment = config.environment;
		const row =
			(await Global.findByPk(environment)) ||
			(await Global.create({
				environment,
				superUsers: [],
				blacklistedChannels: [],
				blacklistedGuilds: [],
				blacklistedUsers: [],
				disabledCommands: []
			}));

		for (const option in row) {
			if (client.cache[option]) client.cache[option] = row[option];
		}
	}

	private static async updateGuildCache(client: BushClient): Promise<void> {
		// client.db.query(`SELECT * FROM 'Guilds'`)
	}
}