Star ✨ on GitHub

Types

API reference for TypeScript types in Moonlink.js

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.

Search Types

TSearchSources

TSearchSourcestype
Defines the supported search sources.
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
Defines the types of loading results.
type TLoadResultType =
  | "track"
  | "playlist"
  | "search"
  | "empty"
  | "error"
  | TLoadResultNodeLinkType;

This type defines the possible result types when loading tracks. The default values are:
ValueDescription
trackA single track was loaded.
playlistA playlist was loaded.
searchSearch results were loaded.
emptyNo results were found.
errorAn error occurred while loading.
Additionally, the type also includes the values from TLoadResultNodeLinkType for NodeLink support.

TLoadResultNodeLinkType

TLoadResultNodeLinkTypetype
Defines the NodeLink-specific loading result types.
type TLoadResultNodeLinkType = 
  | "short"
  | "album"
  | "artist"
  | "playlist"
  | "station"
  | "podcast"
  | "show";

This type defines the possible result types when loading tracks using NodeLink. The values are:
ValueDescription
shortA short video was loaded.
albumAn album was loaded.
artistAn artist was loaded.
playlistA playlist was loaded.
stationA station was loaded.
podcastA podcast was loaded.
showA show was loaded.

Node Types

TSortTypeNode

TSortTypeNodetype
Defines the sorting criteria for nodes.
type TSortTypeNode =
  | "players"
  | "playingPlayers"
  | "memory"
  | "cpuLavalink"
  | "cpuSystem"
  | "uptime"
  | "random";

This type defines the possible sorting criteria for nodes. The values are:
ValueDescription
playersSort by number of players.
playingPlayersSort by number of playing players.
memorySort by memory usage.
cpuLavalinkSort by Lavalink CPU usage.
cpuSystemSort by system CPU usage.
uptimeSort by uptime.
randomSort 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
Defines the loop modes for players.
type TPlayerLoop = "off" | "track" | "queue";

This type defines the possible loop modes for players. The values are:
ValueDescription
offLoop disabled.
trackSingle track loop.
queueQueue 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
Defines the track end types.
type TTrackEndType =
  | "queueEnd"
  | "loadFailed"
  | "stopped"
  | "replaced"
  | "cleanup"
  | "finished";

This type defines the possible reasons for a track ending. The values are:
ValueDescription
queueEndThe queue has ended.
loadFailedFailed to load the track.
stoppedPlayback was stopped.
replacedThe track was replaced.
cleanupPlayer cleanup.
finishedThe 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
Defines the properties that can be partially loaded for tracks.
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:
ValueDescription
urlTrack URL
durationTrack duration
positionTrack position
identifierUnique track identifier
isSeekableWhether the track allows seeking
isStreamWhether the track is a live stream
artworkUrlURL of the track artwork
isrcISRC code of the track
sourceNameName 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[]
};