fuga
Version:
A comprehensive, feature-rich, and modern Lavalink v4 client for Node.js
79 lines • 3.62 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.RestManager = void 0;
const node_fetch_1 = __importDefault(require("node-fetch"));
const endpoints_1 = require("./endpoints");
class RestManager {
node;
baseUrl;
constructor(node) {
this.node = node;
this.baseUrl = `http${node.options.secure ? 's' : ''}://${node.options.host}:${node.options.port}/v4`;
}
async request(endpoint, method, data) {
console.log(`[RaftLink] [RestManager] Making request to ${endpoint} with method ${method}`);
const res = await (0, node_fetch_1.default)(`${this.baseUrl}${endpoint}`, {
method,
headers: { Authorization: this.node.options.password, 'Content-Type': 'application/json' },
body: data ? JSON.stringify(data) : undefined,
});
if (!res.ok) {
let errorBody = { message: `Request failed with status ${res.status}` };
try {
const text = await res.text();
try {
errorBody = JSON.parse(text);
}
catch (e) {
errorBody.message = text;
}
}
catch (e) {
console.error('[RaftLink] [RestManager] Failed to read error body', e);
}
console.error(`[RaftLink] [RestManager] Request to ${endpoint} failed with status ${res.status}:`, errorBody.message || 'No error message');
throw new Error(`Lavalink request to ${endpoint} failed with status ${res.status}: ${errorBody.message || 'No error message'}`);
}
if (res.status === 204)
return undefined;
const responseData = await res.json();
console.log(`[RaftLink] [RestManager] Successfully received response from ${endpoint}`);
return responseData;
}
loadTracks(identifier) {
const searchPrefixes = [
"ytsearch:", "ytmsearch:", "scsearch:", "spsearch:", "amsearch:", "dzsearch:", "dzisrc:",
"spshortsearch:", "apple:", "deezer:", "spotify:", "soundcloud:", "bandcamp:",
"youtube:", "yandexmusic:"
];
const hasPrefix = searchPrefixes.some(prefix => identifier.startsWith(prefix));
const finalIdentifier = hasPrefix ? identifier : `ytsearch:${identifier}`;
return this.request(endpoints_1.Endpoints.loadTracks(finalIdentifier), 'GET');
}
decodeTrack(encodedTrack) {
return this.request(endpoints_1.Endpoints.decodeTrack(encodedTrack), 'GET');
}
updatePlayer(guildId, data) {
if (!this.node.sessionId)
throw new Error('No session ID available for player updates.');
const endpoint = endpoints_1.Endpoints.player(this.node.sessionId, guildId);
return this.request(endpoint, 'PATCH', data);
}
destroyPlayer(guildId) {
if (!this.node.sessionId)
return Promise.resolve();
const endpoint = endpoints_1.Endpoints.player(this.node.sessionId, guildId);
return this.request(endpoint, 'DELETE');
}
updateSession(resuming, timeout) {
if (!this.node.sessionId)
throw new Error('No session ID available to update.');
const endpoint = endpoints_1.Endpoints.sessions(this.node.sessionId);
return this.request(endpoint, 'PATCH', { resuming, timeout });
}
}
exports.RestManager = RestManager;
//# sourceMappingURL=RestManager.js.map