The Track class represents a single audio track with metadata like title, author, duration, and URL.
Properties
Unique track identifier (Snowflake)
Sanitized track title with special characters removed
Formatted duration (e.g., “3:45”)
The Discord user who requested this track
The playlist this track belongs to (if any)
Track source: “youtube”, “soundcloud”, “spotify”, “apple_music”, or “arbitrary”
The audio engine/extractor that provided this track
Whether this is a live stream
The extractor that fetched this track
The query type used to find this track
Additional metadata attached to this track
Raw data from the extractor
Methods
toJSON()
Convert track to JSON:
const json = track.toJSON();
console.log(json.title, json.durationMS);
Whether to exclude playlist data
JSON representation of the track
toString()
Get string representation:
const str = track.toString();
// "Track: Despacito by Luis Fonsi (3:45)"
toHyperlink()
Create a markdown hyperlink:
const link = track.toHyperlink();
// "[Despacito](https://youtube.com/watch?v=...)"
serialize()
Serialize for storage:
const serialized = track.serialize();
// Store in database
await db.tracks.save(serialized);
deserialize()
Deserialize from storage:
const data = await db.tracks.get(trackId);
const track = Track.fromSerialized(player, data);
Reconstructed track instance
play()
Play this track:
await track.play(voiceChannel, {
nodeOptions: { metadata: interaction }
});
channel
GuildVoiceChannelResolvable
required
Voice channel to play in
options
PlayerNodeInitializerOptions
Play options
returns
Promise<PlayerNodeInitializationResult>
Play result with queue info
Creating Tracks
From Search
const result = await player.search('despacito', {
requestedBy: interaction.user
});
const track = result.tracks[0];
From Raw Data
const track = new Track(player, {
title: 'My Track',
author: 'Artist Name',
url: 'https://example.com/track',
thumbnail: 'https://example.com/thumb.jpg',
duration: '3:45',
views: 1000,
requestedBy: interaction.user,
source: 'arbitrary'
});
Access and modify metadata:
// Set metadata
track.metadata = {
channelId: interaction.channelId,
requestedAt: Date.now()
};
// Access metadata
const channelId = track.metadata.channelId;
Example Usage
import { useQueue } from 'discord-player';
const queue = useQueue(interaction.guildId);
const track = queue.currentTrack;
if (!track) {
return interaction.reply('No track is playing!');
}
// Display track info
const embed = new EmbedBuilder()
.setTitle(track.title)
.setDescription(track.description)
.setAuthor({ name: track.author })
.setURL(track.url)
.setThumbnail(track.thumbnail)
.addFields(
{ name: 'Duration', value: track.duration, inline: true },
{ name: 'Source', value: track.source, inline: true },
{ name: 'Views', value: track.views.toLocaleString(), inline: true },
{ name: 'Live', value: track.live ? 'Yes' : 'No', inline: true },
{ name: 'Requested By', value: track.requestedBy?.tag || 'Unknown', inline: true }
);
if (track.playlist) {
embed.addFields({
name: 'Playlist',
value: `[${track.playlist.title}](${track.playlist.url})`
});
}
await interaction.reply({ embeds: [embed] });
// Create hyperlink for text
const link = track.toHyperlink();
await interaction.reply(`Now playing: ${link}`);
// Serialize for favorites
const serialized = track.serialize();
await db.userFavorites.add(interaction.user.id, serialized);
// Later, load favorite
const data = await db.userFavorites.get(interaction.user.id, favoriteId);
const favoriteTrack = Track.fromSerialized(player, data);
await favoriteTrack.play(voiceChannel);
TypeScript Types
type TrackSource = 'soundcloud' | 'youtube' | 'spotify' | 'apple_music' | 'arbitrary';
type TrackResolvable = Track | string | number;
interface TrackJSON {
id: string;
title: string;
description: string;
author: string;
url: string;
thumbnail: string;
duration: string;
durationMS: number;
views: number;
requestedBy: string | null;
playlist: PlaylistJSON | null;
}
interface RawTrackData {
title: string;
description: string;
author: string;
url: string;
thumbnail: string;
duration: string;
views: number;
requestedBy?: User | null;
playlist?: Playlist;
source?: TrackSource;
engine?: any;
live?: boolean;
raw?: any;
queryType?: SearchQueryType;
cleanTitle?: string;
}