Star ✨ on GitHub

Listen

API reference for the Listen class in Moonlink.js

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

Overview

Properties

Core properties of the Listen class.

Available Properties

PropertyTypeDescription
playerPlayerReference to the Player instance
voiceReceiverWsWebSocketWebSocket connection to the NodeLink server

Methods

Methods

Available methods in the Listen class.

Available Methods

start

startmethod

Start Voice Reception

Starts receiving voice events for the current player. Throws an error if the connected node is not a NodeLink.

Returns & Example

ReturnsEventEmitter — Event emitter that emits voice-related eventsEmitted Events

EventDescriptionData
startEmitted when a user starts speaking.{ userId: string, guildId: string, ssrc: number, audio: boolean, video: boolean, rtx: boolean }
endEmitted when a user stops speaking.{ userId: string, guildId: string, ssrc: number, data: Buffer } (audio data in Buffer format)
closeEmitted when the WebSocket connection is closed.void
errorEmitted when an error occurs.Error object
unknownEmitted 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

stopmethod

Stop Voice Reception

Stops receiving voice events for the current player.

Returns & Example

Returnsbooleantrue 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}`);
});