Skip to main content
The Track class represents a single audio track with metadata like title, author, duration, and URL.

Properties

id
string
Unique track identifier (Snowflake)
title
string
The track title
cleanTitle
string
Sanitized track title with special characters removed
description
string
Track description
author
string
Track author/artist name
url
string
Track URL
thumbnail
string
Track thumbnail URL
duration
string
Formatted duration (e.g., “3:45”)
durationMS
number
Duration in milliseconds
views
number
View count
requestedBy
User | null
The Discord user who requested this track
playlist
Playlist | null
The playlist this track belongs to (if any)
source
TrackSource
Track source: “youtube”, “soundcloud”, “spotify”, “apple_music”, or “arbitrary”
engine
any
The audio engine/extractor that provided this track
live
boolean
Whether this is a live stream
extractor
BaseExtractor | null
The extractor that fetched this track
queryType
SearchQueryType | null
The query type used to find this track
metadata
any
Additional metadata attached to this track
raw
any
Raw data from the extractor

Methods

toJSON()

Convert track to JSON:
const json = track.toJSON();
console.log(json.title, json.durationMS);
hidePlaylist
boolean
default:"false"
Whether to exclude playlist data
returns
TrackJSON
JSON representation of the track

toString()

Get string representation:
const str = track.toString();
// "Track: Despacito by Luis Fonsi (3:45)"
returns
string
Formatted track string
Create a markdown hyperlink:
const link = track.toHyperlink();
// "[Despacito](https://youtube.com/watch?v=...)"
returns
string
Markdown hyperlink

serialize()

Serialize for storage:
const serialized = track.serialize();
// Store in database
await db.tracks.save(serialized);
returns
SerializedTrack
Serialized track data

deserialize()

Deserialize from storage:
const data = await db.tracks.get(trackId);
const track = Track.fromSerialized(player, data);
player
Player
required
Player instance
data
SerializedTrack
required
Serialized track data
returns
Track
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

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'
});

Track Metadata

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;
}

Build docs developers (and LLMs) love