Structure Class
Abstract class that provides methods to extend and customize Moonlink.js structures.
Structure.extend(name, extender)
The Structure class is a core utility in Moonlink.js that allows developers to extend and customize the behavior of various components of the library.
Overview
Properties
Core properties of the Structure class.
Available Properties
| Property | Type | Description |
|---|---|---|
manager | Manager | Static reference to the Manager instance |
Static Methods
Methods available on the Structure class.
Available Methods
| Method | Description |
|---|---|
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
| Class | Description | Reference |
|---|---|---|
Database | Handles data persistence | Database |
NodeManager | Manages Lavalink nodes | NodeManager |
PlayerManager | Manages player instances | PlayerManager |
SearchResult | Handles search results | SearchResult |
Player | Controls music playback | Player |
Queue | Manages track queue | Queue |
Node | Represents a Lavalink node | Node |
Rest | Handles REST API calls | Rest |
Filters | Audio filter controls | Filters |
Track | Represents a track | Track |
Lyrics | Handles track lyrics | Lyrics |
Listen | Event listener handling | Listen |
Methods
setManager
Set Manager
Sets the Manager instance reference.
Parameters
Returns & Example
Returns
• void
Structure.setManager(manager);
getManager
Get Manager
Gets the current Manager instance.
Returns & Example
Returns
• Manager - The current Manager instance
const manager = Structure.getManager();
get
Get Structure
Gets a structure by name.
Parameters
Returns & Example
Returns
• Extendable[K] - The requested structure
const PlayerStructure = Structure.get('Player');
Throws an error if the structure doesn't exist.
:: :: ::
extend
Extend Structure
Extends a structure with custom functionality.
Parameters
Returns & Example
Returns
• void
// 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();
}
};
});
References
References
Related documentation and resources.
Related Documentation
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
};