UNPKG

discord-tictactoe

Version:

A modern Discord Bot for playing Tic-Tac-Toe

76 lines (75 loc) 2.77 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const node_fs_1 = __importDefault(require("node:fs")); const node_path_1 = __importDefault(require("node:path")); class ConfigProvider { constructor() { this.token = ''; this.language = 'en'; this.command = 'tictactoe'; this.commandOptionName = 'opponent'; this.textCommand = undefined; this.allowedChannelIds = []; this.allowedRoleIds = []; this.embedColor = 2719929; this.requestExpireTime = 60; this.requestCooldownTime = 0; this.simultaneousGames = false; this.aiDifficulty = 'Unbeatable'; this.gameExpireTime = 30; this.gameBoardDelete = false; this.gameBoardDisableButtons = false; this.gameBoardEmbed = false; this.gameBoardEmojies = []; this.gameBoardPlayerEmoji = false; this.gameBoardReactions = false; this.CONFIG_PATH = node_path_1.default.join(process.cwd(), 'config', 'config.json'); this.initializeFromFile(); this.initializeFromEnv(); } initializeFromFile() { if (node_fs_1.default.existsSync(this.CONFIG_PATH)) { const savedConfig = require(this.CONFIG_PATH); Object.keys(savedConfig).forEach(field => { this[field] = savedConfig[field]; }); } } initializeFromEnv() { Object.keys(process.env) .filter(key => this[ConfigProvider.camelCase(key)] !== undefined) .forEach(key => { const camelCaseKey = ConfigProvider.camelCase(key); const value = process.env[key]; let newValue; if (camelCaseKey === 'language') return; switch (typeof this[camelCaseKey]) { case 'number': newValue = Number.parseFloat(value); break; case 'boolean': newValue = value.toLowerCase() === 'true'; break; case 'string': case 'object': default: newValue = value.toString(); if (Array.isArray(this[camelCaseKey])) { newValue = newValue.split(','); } break; } if (newValue) { this[camelCaseKey] = newValue; } }); } static camelCase(str) { return str.toLowerCase().replaceAll(/_([a-z])/g, g => g[1].toUpperCase()); } } exports.default = ConfigProvider;