Initial commit

This commit is contained in:
Keroosha 2023-08-10 21:32:34 +03:00
commit 99d823e15a
23 changed files with 927 additions and 0 deletions

View file

@ -0,0 +1,32 @@
import { readFileSync, existsSync } from "fs";
import { isObject, isString, isEmpty } from "lodash";
export type BotConfig = {
token: string;
channelId: string;
adminChatId: string;
};
export type BotConfigRaw = Partial<BotConfig>;
const DEFAULT_CONFIG_PATH = "config.json";
const isValidString = (x: unknown) => isString(x) && !isEmpty(x);
const ensureIsObject = (x?: any): x is BotConfigRaw => isObject(x);
const ValidateConfig = (config?: any): config is BotConfig => {
if (!ensureIsObject(config)) return false;
const { token, channelId, adminChatId } = config;
return isValidString(token) && isValidString(channelId) && isValidString(adminChatId);
};
export const loadBotConfig = (configPath = DEFAULT_CONFIG_PATH): BotConfig => {
if (!existsSync(configPath)) throw new Error(`Missing ${configPath} file`);
const configFileString = readFileSync(configPath).toString();
const parsedConfig: any = JSON.parse(configFileString);
if (ValidateConfig(parsedConfig)) return parsedConfig;
throw new Error("Config is invalid");
};