UNPKG

discord-player-music

Version:

A powerful music module for your Discord bot with lots of features

120 lines (119 loc) 3.48 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.DatabaseManager = void 0; const fs_1 = require("fs"); const ms_1 = __importDefault(require("../modules/ms")); const PlayerError_1 = require("../PlayerError"); const errors_json_1 = __importDefault(require("../data/errors.json")); /** * Class that controls Player Database Manager * * @class * @classdesc Player Database Manager Class */ class DatabaseManager { player; storage; config; /** * @constructor * * @param {Player} player Player Class */ constructor(player) { if (!player) throw new PlayerError_1.PlayerError(errors_json_1.default.default.requireParam.replace('{param}', 'player').replace('{data}', '<DatabaseManager>')); /** * Player Class * * @type {Player} * @private */ this.player = player; /** * Player Database Configuration * * @type {DatabaseManagerConfiguration} */ this.config = { path: player.options.databaseConfig.path, checkInterval: player.options.databaseConfig.checkInterval }; /** * Player Playlists Storage * * @type {object} * @private */ this.storage = null; this.init(); } /** * Method to get database object * * @example * const storage = client.player.database.get(); * const guildPlaylists = storage['GuildID']; * * console.log(guildPlaylists); * * @returns {object} Database object */ get() { return this.storage; } /** * Method for writing data to the database * * @example * const storage = client.player.database.get(); * storage['GuildID']['example'] = {}; * * client.player.database.write(storage); * * @param {object} [data] Database object */ write(data) { (0, fs_1.writeFileSync)(this.config.path, JSON.stringify(data ? data : this.storage, null, '\t')); } /** * Allows you to initialize data for a single server * * @example * function getGuildStorage('GuildID') { * // If there is no data, it will appear * client.player.database.initGuild('GuildID); * * const storage = client.player.database.get(); * * console.log(storage['GuildID']); * } * * getGuildStorage('GuildID'); * * @param {string} guildID Discord Guild ID */ initGuild(guildID) { if (!this.storage[guildID]) this.storage[guildID] = { playlists: [] }; return this.write(); } /** * Method for module database initialization * * @private */ init() { setInterval(async () => { if (!(0, fs_1.existsSync)(this.config.path)) (0, fs_1.writeFileSync)(this.config.path, '{}', 'utf-8'); this.storage = JSON.parse((0, fs_1.readFileSync)(this.config.path, 'utf-8')); }, (0, ms_1.default)(this.config.checkInterval)); } } exports.DatabaseManager = DatabaseManager;