UNPKG

magmastream

Version:

A user-friendly Lavalink client designed for NodeJS.

207 lines (206 loc) 10.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = managerCheck; const Enums_1 = require("../structures/Enums"); const MagmastreamError_1 = require("../structures/MagmastreamError"); /** * Validates the provided ManagerOptions object. * @param options - The options to validate. * @throws {MagmaStreamError} Throws if any required option is missing or invalid. */ function managerCheck(options, isWrapper = false) { if (!options) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: "ManagerOptions must not be empty.", context: { option: "options" }, }); } const { playNextOnEnd, clientName, defaultSearchPlatform, autoPlaySearchPlatforms, nodes, enabledPlugins, send, trackPartial, enablePriorityMode, useNode, normalizeYouTubeTitles, lastFmApiKey, maxPreviousTracks, getUser, getGuild, } = options; // Validate playNextOnEnd option if (typeof playNextOnEnd !== "boolean") { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "playNextOnEnd" must be a boolean.', context: { option: "playNextOnEnd", value: playNextOnEnd }, }); } // Validate clientName option if (typeof clientName !== "undefined") { if (typeof clientName !== "string" || clientName.trim().length === 0) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "clientName" must be a non-empty string.', context: { option: "clientName", value: clientName }, }); } } // Validate defaultSearchPlatform option if (typeof defaultSearchPlatform !== "undefined") { if (!Object.values(Enums_1.SearchPlatform).includes(defaultSearchPlatform)) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: `Manager option "defaultSearchPlatform" must be one of: ${Object.values(Enums_1.SearchPlatform).join(", ")}.`, context: { option: "defaultSearchPlatform", value: defaultSearchPlatform }, }); } } // Validate autoPlaySearchPlatforms if (typeof autoPlaySearchPlatforms !== "undefined") { if (!Array.isArray(autoPlaySearchPlatforms)) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "autoPlaySearchPlatforms" must be an array.', context: { option: "autoPlaySearchPlatforms", value: autoPlaySearchPlatforms }, }); } if (!autoPlaySearchPlatforms.every((platform) => Object.values(Enums_1.AutoPlayPlatform).includes(platform))) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "autoPlaySearchPlatforms" must be an array of valid AutoPlayPlatform values.', context: { option: "autoPlaySearchPlatforms", value: autoPlaySearchPlatforms }, }); } } // Validate nodes option if (typeof nodes === "undefined" || !Array.isArray(nodes)) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "nodes" must be an array.', context: { option: "nodes", value: nodes }, }); } // Validate enabledPlugins option if (typeof enabledPlugins !== "undefined" && !Array.isArray(enabledPlugins)) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "enabledPlugins" must be a Plugin array.', context: { option: "enabledPlugins", value: enabledPlugins }, }); } // Validate send option if (typeof send !== "undefined" && typeof send !== "function") { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "send" must be a function.', context: { option: "send", value: send }, }); } // Validate trackPartial option if (typeof trackPartial !== "undefined") { if (!Array.isArray(trackPartial)) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "trackPartial" must be an array.', context: { option: "trackPartial", value: trackPartial }, }); } if (!trackPartial.every((item) => Object.values(Enums_1.TrackPartial).includes(item))) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "trackPartial" must be an array of valid TrackPartial values.', context: { option: "trackPartial", value: trackPartial }, }); } } // Validate enablePriorityMode option if (typeof enablePriorityMode !== "undefined" && typeof enablePriorityMode !== "boolean") { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "enablePriorityMode" must be a boolean.', context: { option: "enablePriorityMode", value: enablePriorityMode }, }); } // Validate node priority if enablePriorityMode is enabled if (enablePriorityMode) { for (let index = 0; index < nodes.length; index++) { if (typeof nodes[index].nodePriority !== "number" || isNaN(nodes[index].nodePriority)) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: `Missing or invalid node option "nodePriority" at position ${index}.`, context: { option: "nodePriority", index, value: nodes[index].nodePriority }, }); } } } // Validate useNode option if (typeof useNode !== "undefined") { if (typeof useNode !== "string") { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "useNode" must be a string "leastLoad" or "leastPlayers".', context: { option: "useNode", value: useNode }, }); } if (!Object.values(Enums_1.UseNodeOptions).includes(useNode)) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "useNode" must be either "leastLoad" or "leastPlayers".', context: { option: "useNode", value: useNode }, }); } } // Validate normalizeYouTubeTitles option if (typeof normalizeYouTubeTitles !== "undefined" && typeof normalizeYouTubeTitles !== "boolean") { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "normalizeYouTubeTitles" must be a boolean.', context: { option: "normalizeYouTubeTitles", value: normalizeYouTubeTitles }, }); } // Validate lastFmApiKey option if (typeof lastFmApiKey !== "undefined" && (typeof lastFmApiKey !== "string" || lastFmApiKey.trim().length === 0)) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "lastFmApiKey" must be a non-empty string.', context: { option: "lastFmApiKey", value: lastFmApiKey }, }); } // Validate maxPreviousTracks option if (typeof maxPreviousTracks !== "undefined") { if (typeof maxPreviousTracks !== "number" || isNaN(maxPreviousTracks)) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "maxPreviousTracks" must be a number.', context: { option: "maxPreviousTracks", value: maxPreviousTracks }, }); } if (maxPreviousTracks <= 0) { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "maxPreviousTracks" must be a positive number.', context: { option: "maxPreviousTracks", value: maxPreviousTracks }, }); } } if (!isWrapper) { if (typeof getUser === "undefined") { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "getUser" is required when not using a built-in wrapper.', context: { option: "getUser" }, }); } if (typeof getGuild === "undefined") { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "getGuild" is required when not using a built-in wrapper.', context: { option: "getGuild" }, }); } } if (typeof getUser !== "undefined" && typeof getUser !== "function") { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "getUser" must be a function.', context: { option: "getUser", value: getUser }, }); } if (typeof getGuild !== "undefined" && typeof getGuild !== "function") { throw new MagmastreamError_1.MagmaStreamError({ code: Enums_1.MagmaStreamErrorCode.MANAGER_INVALID_CONFIG, message: 'Manager option "getGuild" must be a function.', context: { option: "getGuild", value: getGuild }, }); } }