Discord Player
API reference

Plugins

Plugin definitions, the registry, and the built-in operations.

You are reading the v8 docsDev

Define a plugin with definePlugin<Options>(definition) and register it with player.plugins or the plugins option.

import { definePlugin } from 'discord-player';

const source = definePlugin({
  id: 'example.source',

  setup(ctx) {
    ctx.on('track.stream', ({ track }) => ({ url: track.url }));
  },
});

Definition

FieldTypeDescription
idstringProvider ID
setup?(ctx)void | Promise<void>Register handlers
destroy?(ctx)void | Promise<void>Optional teardown
priority?numberDefault handler priority, or an override of a remote host's priorities
remoteHost?{ hostId, url, token?, timeoutMs? }Use a remote source host instead of local handlers

A plugin that only provides lyrics needs no source operations.

Setup context

MemberDescription
ctx.optionsOptions for this registration
ctx.signalAborted when the plugin is removed
ctx.playerThe hosting runtime. See runtime narrowing
ctx.on(operation, handler, options?)Registers a handler. Returns a function that removes it
ctx.provide('playback.backend', backend)Supplies the Player's custom playback backend
ctx.onCleanup(fn)Registers cleanup that runs in reverse order

Handler options

OptionTypeDescription
prioritynumberHigher priorities run first. Equal priorities keep registration order. Defaults to the plugin's priority
protocolsreadonly string[]Only selected when the input names one of these protocols, such as example for example:query. Inputs without a protocol match
guildIdsreadonly string[]Only selected when the invocation is routed for one of these guilds

guildIds uses the guild identity passed by the caller, never a guildId field inside the operation input. An invocation without a guild, such as player.search(query) without guildId, skips guild-restricted handlers. Standalone source hosts never route for a guild, so guild-restricted handlers registered there are never selected.

Operation context

Every handler receives the operation input and an operation context:

ctx.on('track.stream', async ({ track }, { queue, guildId, signal }) => {
  if (queue) console.log(guildId, queue.currentTrack?.title, queue.history.size);

  return { url: await resolveUrl(track, { signal }) };
});
MemberTypeDescription
signalAbortSignalAborts when the caller cancels or the plugin is removed
guildIdstring | nullThe guild this invocation belongs to, or null when it has none
queueGuildQueue | nullThe owning Player's queue for guildId, or null when none exists or is unscoped

Queue playback, related-track lookup for autoplay, stream resolution, lifecycle capabilities, and player.play() searches are routed for their guild. Pass guildId to player.search() or player.plugins.execute() to route other calls. The queue is looked up on the Player that owns the registry, so two Players with the same guild ID stay separate.

The context is local. Remote source hosts receive only portable input and never see guildId or queue.

Runtime narrowing

Plugins registered on a Player receive the actual DiscordPlayer as ctx.player. Standalone source hosts created with @discord-player/plugin-host provide a partial SourcePluginRuntime instead. ctx.player.isPartial() narrows between them:

setup(ctx) {
  if (!ctx.player.isPartial()) {
    ctx.player.nodes; // DiscordPlayer
    ctx.player.events;
  }
}

Programs that only install @discord-player/plugin-host see ctx.player as SourcePluginRuntime. Installing discord-player adds DiscordPlayer to the union without a runtime dependency between the packages.

Registry

player.plugins:

MethodDescription
register(registration)Registers a plugin and waits for setup()
unregister(id, hostId?)Removes a plugin and returns true if it existed
has(id, hostId?)Whether a plugin is registered
hasHost(hostId)Whether a source host is registered
registerHost(options)Registers every provider a source host advertises
ready()Waits for pending registrations
execute(operation, input, options?)Runs an operation. options.guildId routes it for a guild
resolveStream(track, options?)Returns the media source for a track
defineOperation(name, policy)Adds an operation. See custom operations
destroy()Releases every registration

Operations

search.query.match, search.query.resolve, track.stream, track.related, track.bridge, stream.transform, lyrics.search, and track.stream.resolved. Their dispatch policies are in operations.

v7 extractors

player.extractors.register(MyExtractor) adapts a BaseExtractor class to this registry. There is no separate extractor pipeline. See v7 extractors.

Custom playback providers

A modern plugin can supply a Player-local playback implementation with ctx.provide('playback.backend', backend). Register it before player.initialize(). Remedia remains the default when no backend is provided.

The playback backend guide covers registration, capabilities, the session contract, events, and cleanup.

ExportPurpose
PlaybackBackendBackend initialization, Gateway dispatches, session creation, and destruction.
PlaybackBackendCapabilitiesSupported source kinds, crossfade, filters, and source deployments.
PlaybackBackendContextBot identity, lifetime signal, and shard-aware Gateway sender.
PlaybackSessionSession state, controls, typed event subscriptions, and disposal.
PlaybackSessionOptionsShared session creation settings.
PlaybackSourceEncoded media descriptors passed to playback.
PlaybackBackendEventsTyped session lifecycle event tuples.

Continue reading

Models

On this page