UNPKG

lavacord

Version:

A simple by design lavalink wrapper made for any discord library.

188 lines (185 loc) 6.24 kB
import { __name } from '../chunk-PAWJFY3S.mjs'; import { URLSearchParams } from 'url'; import { VERSION } from '../index.mjs'; var RestError = class extends Error { constructor(error, data) { super(error.message); this.error = error; this.data = data; this.name = "RestError"; } static { __name(this, "RestError"); } }; var Rest = class _Rest { static { __name(this, "Rest"); } /** * Base request function that handles communication with Lavalink REST API. * * @internal * @param node - The Lavalink node to send the request to. * @param path - The API route path, starting with /. * @param init - Optional request initialization options. * @param requiresSessionId - Whether the request requires a valid session ID. * @returns A promise resolving to the response data. * @throws {@link RestError} If Lavalink returns an error response. */ static async baseRequest(node, path, init, requiresSessionId) { const headers = new Headers(init?.headers); headers.set("Authorization", node.password); headers.set("User-Agent", `Lavacord/${VERSION}`); const fetchConfig = { method: init?.method ?? "GET", headers, body: init?.body }; if (init?.query) path += `?${new URLSearchParams(init.query)}`; if (requiresSessionId && !node.sessionId) { throw new RestError({ timestamp: Date.now(), status: 400, error: "Bad Request", message: `Node ${node.id} requires a session ID for this route. Did you forget to connect?`, path }, fetchConfig); } const response = await fetch(node.restURL + path, fetchConfig).catch((error) => { throw new RestError({ timestamp: Date.now(), status: 503, error: "Network Error", message: `Failed to connect to ${node.id}: ${error.message}`, path }, fetchConfig); }); if (response.status === 204) return void 0; const contentType = response.headers.get("content-type"); const body = await (contentType?.includes("application/json") ? response.json() : response.text()); if (!response.ok || body?.error) throw new RestError(body, fetchConfig); return body; } /** * Loads tracks from various sources using Lavalink's loadtracks endpoint. * * @param node - The Lavalink node to use. * @param identifer - The identifier to load tracks from (URL, search query, etc.) * @returns A promise resolving to the track loading result. * @throws {@link RestError} If Lavalink encounters an error. * * @public */ static load(node, identifer) { return _Rest.baseRequest(node, `/v${node.version}/loadtracks`, { query: { identifier: identifer } }); } static decode(node, tracks) { if (!Array.isArray(tracks)) return _Rest.baseRequest(node, `/v4/decodetrack`, { query: { track: tracks } }); return _Rest.baseRequest(node, `/v${node.version}/decodetracks`, { method: "POST", body: JSON.stringify(tracks), headers: { "Content-Type": "application/json" } }); } /** * Retrieves the version from the Lavalink server. * * @param node - The Lavalink node to query. * @returns A promise resolving to the version of lavalink. * @throws {@link RestError} If Lavalink encounters an error. * * @public */ static version(node) { return _Rest.baseRequest(node, `/version`); } /** * Retrieves the information of the Lavalink server. * * @param node - The Lavalink node to query. * @returns A promise resolving to the information of lavalink. * @throws {@link RestError} If Lavalink encounters an error. * * @public */ static info(node) { return _Rest.baseRequest(node, `/v${node.version}/info`); } /** * Retrieves the statistics of the Lavalink node. * * @param node - The Lavalink node to query. * @returns A promise resolving to the statistics of lavalink. * @throws {@link RestError} If Lavalink encounters an error. * * @public */ static stats(node) { return _Rest.baseRequest(node, `/v${node.version}/stats`); } /** * Updates the session properties of a Lavalink node. * * @param node - The Lavalink node to update. * @returns A promise resolving to the update session result. * @throws {@link RestError} If Lavalink encounters an error. * * @public */ static updateSession(node) { return _Rest.baseRequest( node, `/v${node.version}/sessions/${node.sessionId}`, { method: "PATCH", body: JSON.stringify({ resuming: node.resuming, timeout: node.resumeTimeout }), headers: { "Content-Type": "application/json" } }, true ); } /** * Updates a player on a Lavalink node. * * @param node - The Lavalink node hosting the player. * @param guildId - The guild ID associated with the player. * @param data - The player update data. * @param noReplace - If true, the event will be dropped if there's a currently playing track. * @returns A promise resolving to the updated player information. * @throws {@link RestError} If Lavalink encounters an error. * @public */ static updatePlayer(node, guildId, data, noReplace = false) { return _Rest.baseRequest( node, `/v${node.version}/sessions/${node.sessionId}/players/${guildId}`, { method: "PATCH", body: JSON.stringify(data), headers: { "Content-Type": "application/json" }, query: noReplace ? { noReplace: "true" } : void 0 }, true ); } /** * Destroys a player on a Lavalink node. * * @param node - The Lavalink node hosting the player. * @param guildId - The guild ID associated with the player to destroy. * @returns A promise resolving to the destroy player result. * @throws {@link RestError} If Lavalink encounters an error. * * @public */ static destroyPlayer(node, guildId) { return _Rest.baseRequest(node, `/v${node.version}/sessions/${node.sessionId}/players/${guildId}`, { method: "DELETE" }, true); } }; export { Rest, RestError }; //# sourceMappingURL=Rest.mjs.map //# sourceMappingURL=Rest.mjs.map