Listen Class
The Listen class allows you to receive voice events from users in a voice channel. It is used to create features like voice detection, speech transcription, and other interactive voice-based features.
player.listen
The Listen functionality requires a compatible NodeLink server. It does not work with standard Lavalink servers.
Overview
Properties
Core properties of the Listen class.
Available Properties
Property | Type | Description |
---|---|---|
player | Player | Reference to the Player instance |
voiceReceiverWs | WebSocket | WebSocket connection to the NodeLink server |
Methods
Methods
Available methods in the Listen class.
Available Methods
start
Start Voice Reception
Starts receiving voice events for the current player. Throws an error if the connected node is not a NodeLink.
Returns & Example
Returns
• EventEmitter
— Event emitter that emits voice-related eventsEmitted Events
Event | Description | Data |
---|---|---|
start | Emitted when a user starts speaking. | { userId: string, guildId: string, ssrc: number, audio: boolean, video: boolean, rtx: boolean } |
end | Emitted when a user stops speaking. | { userId: string, guildId: string, ssrc: number, data: Buffer } (audio data in Buffer format) |
close | Emitted when the WebSocket connection is closed. | void |
error | Emitted when an error occurs. | Error object |
unknown | Emitted when an unknown event is received. | any (full payload) |
// Start voice reception
const listener = player.listen.start();
// Listen for voice events
listener.on('start', (data) => {
console.log(`User ${data.userId} started speaking`);
});
listener.on('end', (data) => {
console.log(`User ${data.userId} stopped speaking`);
console.log('Audio data received:', data.data); // Buffer containing audio data
});
::
stop
Stop Voice Reception
Stops receiving voice events for the current player.
Returns & Example
Returns
• boolean
— true
if reception was successfully stopped, undefined
if there was no active reception
// Stop voice reception
const stopped = player.listen.stop();
console.log('Reception stopped:', stopped);
::
Usage Examples
Basic Examples
Practical examples of how to use the Listen class.
Voice Activity Detector
// Create a map to track who is speaking
const speakingUsers = new Map();
// Start receiving voice events
const listener = player.listen.start();
// Listen for when a user starts speaking
listener.on('start', (data) => {
speakingUsers.set(data.userId, Date.now());
console.log(`User ${data.userId} started speaking`);
});
// Listen for when a user stops speaking
listener.on('end', (data) => {
const startTime = speakingUsers.get(data.userId);
if (startTime) {
const duration = Date.now() - startTime;
speakingUsers.delete(data.userId);
console.log(`User ${data.userId} spoke for ${duration}ms`);
}
});
// Stop reception when the player is destroyed
player.on('destroy', () => {
player.listen.stop();
speakingUsers.clear();
});
Saving User Audio
const fs = require('fs');
const path = require('path');
// Create directory to save audio if it doesn't exist
const audioDir = path.join(__dirname, 'user_audio');
if (!fs.existsSync(audioDir)) {
fs.mkdirSync(audioDir);
}
// Start receiving voice events
const listener = player.listen.start();
// Listen for when a user stops speaking
listener.on('end', (data) => {
// Create filename based on user ID and timestamp
const fileName = path.join(audioDir, `${data.userId}_${Date.now()}.pcm`);
// Save the audio data to a file
fs.writeFileSync(fileName, data.data);
console.log(`Audio saved to ${fileName}`);
});