Star ✨ on GitHub

SearchResult

API reference for the SearchResult class in Moonlink.js

SearchResult Class

Class responsible for processing and representing search results returned by Lavalink. It provides a consistent interface for accessing tracks, playlists, and error information, including extended data from LavaSearch.

new SearchResult(req, options)

Overview

Properties

Core properties of the SearchResult class.

Available Properties

PropertyTypeDescription
querystringThe search query used to obtain the results
sourcestringThe source platform of the search (youtube, soundcloud, etc.)
loadTypestringThe type of search result (track, playlist, search, empty, error, short)
tracksTrack[]Array of tracks found in the search
playlistInfoIPlaylistInfoInformation about the playlist (if loadType is playlist)
errorstringError message if the search failed
albumsILavaSearchAlbum[]Array of albums found in the search (LavaSearch only).
artistsILavaSearchArtist[]Array of artists found in the search (LavaSearch only).
playlistsILavaSearchPlaylist[]Array of playlists found in the search (LavaSearch only).
textsILavaSearchText[]Array of text results found in the search (LavaSearch only).
lavasearchPluginInfoObjectPlugin-specific information for LavaSearch results.
isLavaSearchResultbooleanIndicates if the result is from a LavaSearch query.

Result Types

Result Types

The loadType property can have the following values, including those specific to LavaSearch.

Available Types

track

trackstring
Indicates that a single track was loaded. The tracks array will contain only one track.
Example
{
  loadType: "track",
  tracks: [
    {
      track: "base64-encoded-track",
      info: {
        identifier: "dQw4w9WgXcQ",
        title: "Rick Astley - Never Gonna Give You Up",
        author: "Rick Astley",
        length: 213000,
        isStream: false,
        uri: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
        sourceName: "youtube"
      }
    }
  ],
  playlistInfo: {
    name: null,
    selectedTrack: -1
  }
}

short

shortstring
Indicates that a short track was loaded. The tracks array will contain only one track.
Example
{
  loadType: "short",
  tracks: [
    {
      track: "base64-encoded-track",
      info: {
        identifier: "dQw4w9WgXcQ",
        title: "Rick Astley - Never Gonna Give You Up",
        author: "Rick Astley",
        length: 213000,
        isStream: false,
        uri: "https://www.youtube.com/watch?v=dQw4w9WgXcQ",
        sourceName: "youtube"
      }
    }
  ],
  playlistInfo: {
    name: null,
    selectedTrack: -1
  }
}

playlist

playliststring
Indicates that a playlist was loaded. The tracks array will contain all tracks and playlistInfo will have playlist details.
Example
{
  loadType: "playlist",
  tracks: [
    {
      track: "base64-encoded-track-1",
      info: { /* track 1 information */ }
    },
    {
      track: "base64-encoded-track-2",
      info: { /* track 2 information */ }
    }
  ],
  playlistInfo: {
    name: "My Playlist",
    selectedTrack: 0
  }
}
searchstring
Indicates that the search returned multiple results. The tracks array will contain all found tracks. For LavaSearch, this can also include albums, artists, and playlists.
Example
{
  loadType: "search",
  tracks: [
    { /* track 1 information */ },
    { /* track 2 information */ }
  ],
  albums: [
    { /* album 1 information */ }
  ],
  artists: [
    { /* artist 1 information */ }
  ],
  playlists: [
    { /* playlist 1 information */ }
  ],
  texts: [
    { /* text result 1 */ }
  ],
  playlistInfo: {
    name: null,
    selectedTrack: -1
  }
}

empty

emptystring
Indicates that no results were found. The tracks array will be empty.
Example
{
  loadType: "empty",
  tracks: [],
  playlistInfo: {
    name: null,
    selectedTrack: -1
  }
}

error

errorstring
Indicates that an error occurred during the search. The error property will contain the message.
Example
{
  loadType: "error",
  tracks: [],
  playlistInfo: {
    name: null,
    selectedTrack: -1
  },
  error: "Could not load track: 404 Not Found"
}

Methods

Methods

Methods available in the SearchResult class.

Available Methods

getFirst

getFirstmethod

Get First Track

Returns the first track from the search results, if available.

Returns & Example

ReturnsTrack | undefined — The first track or undefined if no tracks are found.

const firstTrack = searchResult.getFirst();
if (firstTrack) {
  console.log(`First track: ${firstTrack.title}`);
}

::

getTotalDuration

getTotalDurationmethod

Get Total Duration

Calculates the total duration of all tracks in the search results.

Returns & Example

Returnsnumber — The total duration in milliseconds.

const totalDuration = searchResult.getTotalDuration();
console.log(`Total duration: ${totalDuration / 1000} seconds`);

::

Usage Examples

Basic Usage

Example of using the SearchResult class.

// Create a search result instance
const searchResult = new SearchResult(response, {
  query: "never gonna give you up",
  source: "youtube",
  requester: message.author
});

// Access the search results
console.log(`Search query: ${searchResult.query}`);
console.log(`Source: ${searchResult.source}`);
console.log(`Load type: ${searchResult.loadType}`);

// Process tracks based on load type
switch (searchResult.loadType) {
  case "track":
  case "search":
    console.log(`Found ${searchResult.tracks.length} tracks`);
    break;
    
  case "playlist":
    console.log(`Found playlist: ${searchResult.playlistInfo.name}`);
    console.log(`Total tracks: ${searchResult.tracks.length}`);
    break;
    
  case "empty":
    console.log("No results found");
    break;
    
  case "error":
    console.error(`Error: ${searchResult.error}`);
    break;
}

Search Result