Documentation
Guides
Voice Recording

Voice Recording

Note
Recording should not be done without user's permission and is against the TOS. We are not responsible for the consequences caused by this action. In addition, since the voice receiving API is unofficial and is not fully supported by Discord you may not get perfect results.

Since Discord Player v6, you are able to use voice receiving functionality of your bot. Discord Player provides high level API to @discordjs/voice's experimental voice receiving feature.

// you need to create guild queue first
const queue = player.nodes.create(interaction.guildId);
 
try {
  // make sure to connect to a voice channel which you want to record audio from
  await queue.connect(interaction.member.voice.channelId, {
    deaf: false // make sure self deaf is false otherwise bot wont hear your audio
  });
} catch {
  return interaction.followUp('Failed to connect to your channel');
}
 
// initialize receiver stream
const stream = queue.voiceReceiver.recordUser(interaction.member.id, {
  mode: 'pcm', // record in pcm format
  end: EndBehaviorType.AfterSilence // stop recording once user stops talking
});
 
const writer = stream.pipe(createWriteStream(`./recording-${interaction.member.id}.pcm`)); // write the stream to a file
 
writer.once('finish', () => {
  if (interaction.isRepliable()) interaction.followUp(`Finished writing audio!`);
  queue.delete(); // cleanup
});

You can also convert the recorded audio from PCM to any format you'd like.

ffmpeg -i "./recording-USER_ID.pcm" -f s16le -ac 2 -ar 48000 "./recording.mp3"

Recorded PCM audio can also be played using queue.node.playRaw(audioResource) method.