Star ✨ on GitHub

Structure

API reference for the Structure class in Moonlink.js

Structure Class

Abstract class that provides methods to extend and customize Moonlink.js structures.

Structure.extend(name, extender)

Overview

Properties

Core properties of the Structure class.

Available Properties

PropertyTypeDescription
managerManagerStatic reference to the Manager instance

Static Methods

Methods available on the Structure class.

Available Methods

MethodDescription
setManager(manager)Sets the Manager instance reference
getManager()Gets the current Manager instance
get(name)Gets a structure by name
extend(name, extender)Extends a structure with custom functionality

Extendable Classes

Classes that can be extended using the Structure class.

Available Classes

ClassDescriptionReference
DatabaseHandles data persistenceDatabase
NodeManagerManages Lavalink nodesNodeManager
PlayerManagerManages player instancesPlayerManager
SearchResultHandles search resultsSearchResult
PlayerControls music playbackPlayer
QueueManages track queueQueue
NodeRepresents a Lavalink nodeNode
RestHandles REST API callsRest
FiltersAudio filter controlsFilters
TrackRepresents a trackTrack
LyricsHandles track lyricsLyrics
ListenEvent listener handlingListen

Methods

setManager

setManagermethod

Set Manager

Sets the Manager instance reference.

Parameters
managerrequiredManager
The Manager instance to set

Returns & Example

Returnsvoid

Structure.setManager(manager);

getManager

getManagermethod

Get Manager

Gets the current Manager instance.

Returns & Example

ReturnsManager - The current Manager instance

const manager = Structure.getManager();

get

getmethod

Get Structure

Gets a structure by name.

Parameters
namerequiredstring
The name of the structure to get

Returns & Example

ReturnsExtendable[K] - The requested structure

const PlayerStructure = Structure.get('Player');

:: :: ::

extend

extendmethod

Extend Structure

Extends a structure with custom functionality.

Parameters
namerequiredstring
The name of the structure to extend
extenderrequiredFunction
Function that returns the extended class

Returns & Example

Returnsvoid

// Extend the Player class
Structure.extend('Player', Player => {
return class CustomPlayer extends Player {
  constructor(manager, options) {
    super(manager, options);
    this.customProperty = 'custom value';
  }
  
  customMethod() {
    return this.customProperty;
  }
};
});

Usage Examples

Complete Examples

Examples of using the Structure class to extend Moonlink.js components.

Basic Extension

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

// Extend the Player class
Structure.extend('Player', Player => {
  return class CustomPlayer extends Player {
    constructor(manager, options) {
      super(manager, options);
      this.announceNowPlaying = true;
    }
    
    // Override the play method
    async play() {
      const result = await super.play();
      
      if (this.announceNowPlaying && this.current) {
        const channel = this.manager.client.channels.cache.get(this.textChannelId);
        if (channel) {
          channel.send(`Now playing: ${this.current.title}`);
        }
      }
      
      return result;
    }
  };
});

// The custom Player class will be used automatically
const player = manager.players.create({
  guildId: '123456789',
  voiceChannelId: '123456789',
  textChannelId: '123456789'
});

Multiple Extensions

// Extend Queue class
Structure.extend('Queue', Queue => {
  return class CustomQueue extends Queue {
    constructor(player) {
      super(player);
      this.maxSize = 100;
    }
    
    add(track) {
      if (this.size >= this.maxSize) {
        throw new Error('Queue size limit reached');
      }
      return super.add(track);
    }
  };
});

// Extend Track class
Structure.extend('Track', Track => {
  return class CustomTrack extends Track {
    constructor(data) {
      super(data);
      this.addedAt = Date.now();
    }
    
    getAddedTime() {
      return new Date(this.addedAt).toLocaleString();
    }
  };
});
These examples demonstrate how to extend different components of Moonlink.js using the Structure class.

References

References

Related documentation and resources.

Code References

// Structure class definition
export abstract class Structure {
  public static manager: Manager;
  public static setManager(manager: Manager): void;
  public static getManager(): Manager;
  public static get<K extends keyof Extendable>(name: K): Extendable[K];
  public static extend<K extends keyof Extendable>(name: K, extender: Extendable[K]): void;
}

// Available structures
export const structures: Extendable = {
  Database,
  NodeManager,
  PlayerManager,
  SearchResult,
  Player,
  Queue,
  Node,
  Rest,
  Filters,
  Track,
  Lyrics,
  Listen
};

Examples Repository

Check out our examples repository for more Structure extension examples.