Utilities
A collection of helper functions and classes used internally by Moonlink.js, including the EventEmitter, HTTP request wrapper, and track encoding/decoding logic.
While mostly used internally, these utilities can be helpful for advanced users building plugins or custom integrations.
Classes
EventEmitter
A strongly-typed, priority-based Event Emitter implementation.
Adds a listener. Options can include once and priority (higher runs first). Returns a remove function.
Adds a one-time listener.
Removes a specific listener.
Synchronously calls each of the listeners registered for the event.
Creates a child emitter that propagates events to the parent.
const { EventEmitter } = require('moonlink.js');
class MyEmitter extends EventEmitter {}
const myEmitter = new MyEmitter();
myEmitter.on('test', (data) => console.log(data));
myEmitter.emit('test', 'Hello World');
Structure
Manages the dependency injection and extension system for Moonlink.js classes.
Retrieves a registered structure class.
Extends an existing structure with a new class.
const { Structure } = require('moonlink.js');
// Extend the Player class
Structure.extend('Player', (BasePlayer) => class MyPlayer extends BasePlayer {
constructor(...args) {
super(...args);
this.customProp = true;
}
});
Functions
makeRequest(url, options)
makeRequest(url, options)#
→Promise<T | undefined>A robust HTTP/HTTPS request wrapper with built-in retry logic, timeout, and redirect handling. Used for all REST communications.
The URL to fetch.
Node.js http.RequestOptions plus optional body.
const { makeRequest } = require('moonlink.js');
const data = await makeRequest('https://api.example.com/data', {
method: 'GET',
headers: { 'Authorization': '...' }
});
decodeTrack(encoded)
decodeTrack(encoded)#
→ITrackDecodes a Lavalink base64 track string into a usable object without making an API call. Useful for offline processing.
The base64 encoded string.
const { decodeTrack } = require('moonlink.js');
const track = decodeTrack("QAA...");
console.log(track.info.title);
encodeTrack(track)
encodeTrack(track)#
→stringEncodes a track info object into a Lavalink base64 string.
The track info object.
const { encodeTrack } = require('moonlink.js');
const base64 = encodeTrack({
title: "Never Gonna Give You Up",
author: "Rick Astley",
length: 212000,
identifier: "dQw4w9WgXcQ",
isSeekable: true,
isStream: false,
uri: "https://youtube.com/...",
sourceName: "youtube",
position: 0
});
validate(prop, validator, error)
Internal validation helper. Throws a TypeError with the Moonlink.js > prefix if validation fails.
const { validate } = require('moonlink.js');
validate(volume, (v) => v >= 0 && v <= 100, "Volume must be between 0 and 100");