Type Definitions
Moonlink.js uses several TypeScript types to define specific values and ensure consistency throughout the library. These types are essential for development with Moonlink.js, providing value validation and documentation.
All Moonlink.js types are available through the main module and can be imported directly.
Search Types
TSearchSources
TSearchSourcestype
export enum SearchSources {
YouTube = "ytsearch",
YouTubeMusic = "ytmsearch",
SoundCloud = "scsearch",
Local = "local",
}
export enum NodeState {
CONNECTING = "CONNECTING",
CONNECTED = "CONNECTED",
READY = "READY",
RESUMING = "RESUMING",
RESUMED = "RESUMED",
DISCONNECTED = "DISCONNECTED",
DESTROYED = "DESTROYED",
}
export type TNativeSearchSources = "youtube" | "youtubemusic" | "soundcloud" | "local";
export type TLavaSrcSearchSources =
| "spsearch"
| "sprec"
| "amsearch"
| "dzsearch"
| "dzisrc"
| "dzrec"
| "ymsearch"
| "ymrec"
| "ftts"
| "vksearch"
| "vkrec"
| "tdsearch"
| "tdrec"
| "qbsearch"
| "qbisrc"
| "qbrec"
| "phsearch"
| "speak"
| "mixcloud"
| "ocremix"
| "clypit"
| "reddit"
| "getyarn"
| "tiktok"
| "soundgasm"
| "pixeldrain"
| "streamdeck";
export type TDirectSources =
| "mixcloud"
| "ocremix"
| "clypit"
| "reddit"
| "getyarn"
| "tiktok"
| "soundgasm"
| "pixeldrain"
| "streamdeck";
export type TSearchSources = TNativeSearchSources | TLavaSrcSearchSources | TDirectSources | string;
This type defines the search sources supported by Moonlink.js. The default sources are "youtube", "youtubemusic", and "soundcloud", but it can also be any string for custom sources.
Example
// Search on YouTube
const result = await manager.search({
query: "Never Gonna Give You Up",
source: "youtube"
});
// Search on SoundCloud
const result = await manager.search({
query: "Never Gonna Give You Up",
source: "soundcloud"
});
// Search on YouTube Music
const result = await manager.search({
query: "Never Gonna Give You Up",
source: "youtubemusic"
});
Result Types
TLoadResultType
TLoadResultTypetype
type TLoadResultType =
| "track"
| "playlist"
| "search"
| "empty"
| "error"
| TLoadResultNodeLinkType;
This type defines the possible result types when loading tracks. The default values are:
Additionally, the type also includes the values from
Value | Description |
---|---|
track | A single track was loaded. |
playlist | A playlist was loaded. |
search | Search results were loaded. |
empty | No results were found. |
error | An error occurred while loading. |
TLoadResultNodeLinkType
for NodeLink support. TLoadResultNodeLinkType
TLoadResultNodeLinkTypetype
type TLoadResultNodeLinkType =
| "short"
| "album"
| "artist"
| "playlist"
| "station"
| "podcast"
| "show";
This type defines the possible result types when loading tracks using NodeLink. The values are:
Value | Description |
---|---|
short | A short video was loaded. |
album | An album was loaded. |
artist | An artist was loaded. |
playlist | A playlist was loaded. |
station | A station was loaded. |
podcast | A podcast was loaded. |
show | A show was loaded. |
Node Types
TSortTypeNode
TSortTypeNodetype
type TSortTypeNode =
| "players"
| "playingPlayers"
| "memory"
| "cpuLavalink"
| "cpuSystem"
| "uptime"
| "random";
This type defines the possible sorting criteria for nodes. The values are:
Value | Description |
---|---|
players | Sort by number of players. |
playingPlayers | Sort by number of playing players. |
memory | Sort by memory usage. |
cpuLavalink | Sort by Lavalink CPU usage. |
cpuSystem | Sort by system CPU usage. |
uptime | Sort by uptime. |
random | Sort randomly. |
Example
// Configure the Manager to sort nodes by CPU usage
const manager = new Manager({
nodes: [
// ... node configurations
],
options: {
sortTypeNode: "cpuLavalink"
}
});
Player Types
TPlayerLoop
TPlayerLooptype
type TPlayerLoop = "off" | "track" | "queue";
This type defines the possible loop modes for players. The values are:
Value | Description |
---|---|
off | Loop disabled. |
track | Single track loop. |
queue | Queue loop. |
Example
// Configure a player with track loop
const player = manager.createPlayer({
guildId: "123456789012345678",
voiceChannelId: "123456789012345678",
textChannelId: "123456789012345678",
loop: "track"
});
// Change the loop mode
player.setLoop("queue");
TTrackEndType
TTrackEndTypetype
type TTrackEndType =
| "queueEnd"
| "loadFailed"
| "stopped"
| "replaced"
| "cleanup"
| "finished";
This type defines the possible reasons for a track ending. The values are:
Value | Description |
---|---|
queueEnd | The queue has ended. |
loadFailed | Failed to load the track. |
stopped | Playback was stopped. |
replaced | The track was replaced. |
cleanup | Player cleanup. |
finished | The track finished normally. |
Example
// Listen for the track end event
manager.on("trackEnd", (player, track, reason) => {
if (reason === "finished") {
console.log(`Track ${track.title} finished normally.`);
} else if (reason === "stopped") {
console.log(`Track ${track.title} was stopped.`);
}
});
Track Types
TPartialTrackProperties
TPartialTrackPropertiestype
type TPartialTrackProperties =
| "url"
| "duration"
| "position"
| "identifier"
| "isSeekable"
| "isStream"
| "artworkUrl"
| "isrc"
| "sourceName";
This type defines the track properties that can be partially loaded when using the partialTrack option. The available properties are:
Value | Description |
---|---|
url | Track URL |
duration | Track duration |
position | Track position |
identifier | Unique track identifier |
isSeekable | Whether the track allows seeking |
isStream | Whether the track is a live stream |
artworkUrl | URL of the track artwork |
isrc | ISRC code of the track |
sourceName | Name of the track source |
Selective Property Loading
The partialTrack option allows you to specify which track properties to load initially.
// Configure the Manager to load only specific track properties
const manager = new Manager({
nodes: [
// ... node configurations
],
options: {
// Only load URL, duration, and artwork initially
partialTrack: ["url", "duration", "artworkUrl"]
},
sendPayload: // ... payload function
});
// When you need all track data
const track = searchResult.tracks[0];
if (track.isPartialTrack()) {
// This will decode the track and load all properties
track.resolveData();
}
Using with TypeScript
When developing with TypeScript, you can directly import these types from Moonlink.js for static type checking:
import {
TSearchSources,
TLoadResultType,
TSortTypeNode,
TPlayerLoop,
TTrackEndType,
TPartialTrackProperties
} from "moonlink.js";
// Use types in your functions
function createPlayer(guildId: string, loop: TPlayerLoop = "off") {
return manager.createPlayer({
guildId,
voiceChannelId: "123456789012345678",
textChannelId: "123456789012345678",
loop
});
}
// Use types in event handlers
manager.on("trackEnd", (player, track, reason: TTrackEndType) => {
// Logic based on the end reason
});
// Use types for configuration
const searchOptions: { query: string; source: TSearchSources } = {
query: "Never Gonna Give You Up",
source: "youtube"
};
// Configure partial track loading
const managerOptions = {
partialTrack: ["url", "duration", "artworkUrl"] as TPartialTrackProperties[]
};