diff options
Diffstat (limited to 'lib/utils/UpdateCache.ts')
-rw-r--r-- | lib/utils/UpdateCache.ts | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/lib/utils/UpdateCache.ts b/lib/utils/UpdateCache.ts new file mode 100644 index 0000000..2f96d9d --- /dev/null +++ b/lib/utils/UpdateCache.ts @@ -0,0 +1,36 @@ +import config from '#config'; +import { Client } from 'discord.js'; +import { Global, Guild, Shared } from '../models/index.js'; + +export async function 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]; + } + } +} + +export async function 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]; + } + } +} + +export async function updateGuildCache(client: Client) { + const rows = await Guild.findAll(); + for (const row of rows) { + client.cache.guilds.set(row.id, row.toJSON() as Guild); + } +} + +export async function updateEveryCache(client: Client) { + await Promise.all([updateGlobalCache(client), updateSharedCache(client), updateGuildCache(client)]); +} |