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)
The SearchResult class is returned by the Manager's search() method and contains all information about the search results.
Overview
Properties
Core properties of the SearchResult class.
Available Properties
Property | Type | Description |
---|---|---|
query | string | The search query used to obtain the results |
source | string | The source platform of the search (youtube, soundcloud, etc.) |
loadType | string | The type of search result (track , playlist , search , empty , error , short ) |
tracks | Track[] | Array of tracks found in the search |
playlistInfo | IPlaylistInfo | Information about the playlist (if loadType is playlist ) |
error | string | Error message if the search failed |
albums | ILavaSearchAlbum[] | Array of albums found in the search (LavaSearch only). |
artists | ILavaSearchArtist[] | Array of artists found in the search (LavaSearch only). |
playlists | ILavaSearchPlaylist[] | Array of playlists found in the search (LavaSearch only). |
texts | ILavaSearchText[] | Array of text results found in the search (LavaSearch only). |
lavasearchPluginInfo | Object | Plugin-specific information for LavaSearch results. |
isLavaSearchResult | boolean | Indicates 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
tracks
array will contain only one track. {
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
tracks
array will contain only one track. {
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
tracks
array will contain all tracks and playlistInfo
will have playlist details. {
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
}
}
search
tracks
array will contain all found tracks. For LavaSearch, this can also include albums
, artists
, and playlists
. {
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
tracks
array will be empty. {
loadType: "empty",
tracks: [],
playlistInfo: {
name: null,
selectedTrack: -1
}
}
error
error
property will contain the message. {
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
Get First Track
Returns the first track from the search results, if available.
Returns & Example
Returns
• Track | undefined
— The first track or undefined
if no tracks are found.
const firstTrack = searchResult.getFirst();
if (firstTrack) {
console.log(`First track: ${firstTrack.title}`);
}
::
getTotalDuration
Get Total Duration
Calculates the total duration of all tracks in the search results.
Returns & Example
Returns
• number
— 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;
}