Using hooks
Learn how to use discord-player hooks like a pro to manipulate the queue and its components
Discord-Player provides a powerful hooks system that lets you interact with various aspects of the player, queues, and audio streams. These hooks can be used anywhere in your application after initializing the Player instance.
General Hooks Overview
Discord-Player offers two distinct approaches to using hooks, giving you flexibility in how you structure your code:
Method 1: Context-Based Hook Usage
This approach uses the HooksContextProvider, eliminating the need to pass node identifiers repeatedly:
Method 2: Direct Hook Usage with NodeResolvable
This approach allows you to use hooks anywhere by providing a node identifier (typically a guild ID or guild object). This is the most straightforward method:
Then in your commands, you can use hooks without arguments:
Available Hooks
useMainPlayer
Provides access to the main player instance, which manages all guild queues and global player settings.
usePlayer
Retrieves the GuildQueuePlayerNode for a specific guild, giving you control over playback and track information.
useQueue
Access and manage the audio queue for a specific guild.
useHistory
Manage and access the playback history for a guild.
useMetadata
Store and retrieve custom data associated with a guild's queue.
useTimeline
Control and monitor the current track's playback state.
Stream Hooks
Stream hooks provide powerful middleware capabilities for customizing audio stream handling:
onBeforeCreateStream
Intercept and modify the audio stream before it's created:
onAfterCreateStream
Process the PCM stream before it's used as an audio resource: