Star ✨ on GitHub

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.

on(event, listener, options)Function

Adds a listener. Options can include once and priority (higher runs first). Returns a remove function.

once(event, listener, priority)Function

Adds a one-time listener.

off(event, listener)void

Removes a specific listener.

emit(event, ...args)void

Synchronously calls each of the listeners registered for the event.

createChild()EventEmitter

Creates a child emitter that propagates events to the parent.

Example
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.

static get(name)any

Retrieves a registered structure class.

static extend(name, extender)void

Extends an existing structure with a new class.

Example
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)#

A robust HTTP/HTTPS request wrapper with built-in retry logic, timeout, and redirect handling. Used for all REST communications.

urlstring required

The URL to fetch.

optionsobject

Node.js http.RequestOptions plus optional body.

Example
const { makeRequest } = require('moonlink.js');

const data = await makeRequest('https://api.example.com/data', {
    method: 'GET',
    headers: { 'Authorization': '...' }
});

decodeTrack(encoded)

Decodes a Lavalink base64 track string into a usable object without making an API call. Useful for offline processing.

encodedstring required

The base64 encoded string.

Example
const { decodeTrack } = require('moonlink.js');
const track = decodeTrack("QAA...");
console.log(track.info.title);

encodeTrack(track)

Encodes a track info object into a Lavalink base64 string.

trackITrackInfo required

The track info object.

Example
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.

Example
const { validate } = require('moonlink.js');

validate(volume, (v) => v >= 0 && v <= 100, "Volume must be between 0 and 100");

delay(ms)

delay(ms)#

Returns a promise that resolves after the specified milliseconds.

Example
const { delay } = require('moonlink.js');
await delay(5000);