2024-05-12 01:52:39 -07:00

40 lines
1.0 KiB
TypeScript

import { configs } from "@prisma/client";
import { defaultConfig } from "../../config/DefaultConfig";
import { ExtendedClient } from "../../interfaces/ExtendedClient";
import { errorHandler } from "../../utils/errorHandler";
/**
* Module to get the server config from the cache, database, or default.
*
* @param {ExtendedClient} bot The bot's Discord instance.
* @param {string} serverId The ID of the server to get the config for.
* @returns {ExtendedClient["config"]} The server config.
*/
export const getConfig = async (
bot: ExtendedClient,
serverId: string
): Promise<Omit<configs, "id">> => {
try {
const exists = bot.configs[serverId];
if (exists) {
return exists;
}
const record = await bot.db.configs.upsert({
where: {
serverId
},
create: {
...defaultConfig,
serverId
},
update: {}
});
bot.configs[serverId] = record;
return record;
} catch (err) {
await errorHandler(bot, "get config", err);
return defaultConfig;
}
};