I want to record audio from Discord's audio channel in Node.js.

I just want to get voice data for now. Current code
const { Client, GatewayIntentBits, Partials, } = require('discord.js');
const { joinVoiceChannel, EndBehaviorType } = require('@discordjs/voice');
const fs = require('node:fs');
const stream = require('node:stream');
const client = new Client({
intents: Object.values(GatewayIntentBits),
partials: [Partials.Message, Partials.Channel, Partials.Reaction], rest: 60000
});
const prefix = '!';
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});

client.on('messageCreate', async (message) => {
if (message.content.startsWith(prefix + 'join')) {
const voiceChannel = message.member?.voice.channel;
if (!voiceChannel) {
return message.reply('このコマンドを使用するには、音声チャンネルに参加している必要があります。');
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});
connection.receiver.speaking.on('start', (userId) => {
const audio = connection.receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 100,
},
});
var filename = './rec/'.concat(Date.now(), '-').concat(userId, '.pcm');
var out = fs.createWriteStream(filename);
stream.pipeline(audio, out, function (err) {
if (err) {
console.warn(`❌ Error recording file ${filename} - ${err.message}`);
} else {
console.log(`✅ Recorded ${filename}`);
}
});
});
}
});
const { Client, GatewayIntentBits, Partials, } = require('discord.js');
const { joinVoiceChannel, EndBehaviorType } = require('@discordjs/voice');
const fs = require('node:fs');
const stream = require('node:stream');
const client = new Client({
intents: Object.values(GatewayIntentBits),
partials: [Partials.Message, Partials.Channel, Partials.Reaction], rest: 60000
});
const prefix = '!';
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});

client.on('messageCreate', async (message) => {
if (message.content.startsWith(prefix + 'join')) {
const voiceChannel = message.member?.voice.channel;
if (!voiceChannel) {
return message.reply('このコマンドを使用するには、音声チャンネルに参加している必要があります。');
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
});
connection.receiver.speaking.on('start', (userId) => {
const audio = connection.receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 100,
},
});
var filename = './rec/'.concat(Date.now(), '-').concat(userId, '.pcm');
var out = fs.createWriteStream(filename);
stream.pipeline(audio, out, function (err) {
if (err) {
console.warn(`❌ Error recording file ${filename} - ${err.message}`);
} else {
console.log(`✅ Recorded ${filename}`);
}
});
});
}
});
Problem: File is saved, but when played back in Adobe's Audition, it is silent (only +0-2 seconds) Supplementation: No specific errors
7 Replies
d.js toolkit
d.js toolkit17mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Post the full error stack trace, not just the top part! - Show your code! - Explain what exactly your issue is. - Not a discord.js issue? Check out #useful-servers. - Issue solved? Press the button!
gosso8567
gosso8567OP17mo ago
Version:
{
"dependencies": {
"@discordjs/voice": "^0.16.0",
"@google-cloud/speech": "^5.6.0",
"discord.js": "^14.11.0",
"ffmpeg-static": "^5.1.0",
"libsodium-wrappers": "^0.7.11",
"node-opus": "^0.3.3",
"prism-media": "^1.3.5",
"wav-encoder": "^1.3.0"
}
}
{
"dependencies": {
"@discordjs/voice": "^0.16.0",
"@google-cloud/speech": "^5.6.0",
"discord.js": "^14.11.0",
"ffmpeg-static": "^5.1.0",
"libsodium-wrappers": "^0.7.11",
"node-opus": "^0.3.3",
"prism-media": "^1.3.5",
"wav-encoder": "^1.3.0"
}
}
NodeJS:v19.2.0 OS:Windows11
gosso8567
gosso8567OP17mo ago
No description
No description
gosso8567
gosso8567OP17mo ago
I did this, but still only noise is recorded.
const { Client, GatewayIntentBits, Partials, } = require('discord.js');
const { joinVoiceChannel, EndBehaviorType } = require('@discordjs/voice');
const fs = require('node:fs');
const stream = require('node:stream');
const client = new Client({
intents: Object.values(GatewayIntentBits),
partials: [Partials.Message, Partials.Channel, Partials.Reaction], rest: 60000
});
const prefix = '!';
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});

client.on('messageCreate', async (message) => {
if (message.content.startsWith(prefix + 'join')) {
const voiceChannel = message.member?.voice.channel;
if (!voiceChannel) {
return message.reply('このコマンドを使用するには、音声チャンネルに参加している必要があります。');
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
selfDeaf:false
});
connection.receiver.speaking.on('start', (userId) => {
const audio = connection.receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 100,
},
});
var filename = './rec/'.concat(Date.now(), '-').concat(userId, '.pcm');
var out = fs.createWriteStream(filename);
stream.pipeline(audio, out, function (err) {
if (err) {
console.warn(`❌ Error recording file ${filename} - ${err.message}`);
} else {
console.log(`✅ Recorded ${filename}`);
}
});
});
}
});
const { Client, GatewayIntentBits, Partials, } = require('discord.js');
const { joinVoiceChannel, EndBehaviorType } = require('@discordjs/voice');
const fs = require('node:fs');
const stream = require('node:stream');
const client = new Client({
intents: Object.values(GatewayIntentBits),
partials: [Partials.Message, Partials.Channel, Partials.Reaction], rest: 60000
});
const prefix = '!';
client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});

client.on('messageCreate', async (message) => {
if (message.content.startsWith(prefix + 'join')) {
const voiceChannel = message.member?.voice.channel;
if (!voiceChannel) {
return message.reply('このコマンドを使用するには、音声チャンネルに参加している必要があります。');
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
selfDeaf:false
});
connection.receiver.speaking.on('start', (userId) => {
const audio = connection.receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 100,
},
});
var filename = './rec/'.concat(Date.now(), '-').concat(userId, '.pcm');
var out = fs.createWriteStream(filename);
stream.pipeline(audio, out, function (err) {
if (err) {
console.warn(`❌ Error recording file ${filename} - ${err.message}`);
} else {
console.log(`✅ Recorded ${filename}`);
}
});
});
}
});
gosso8567
gosso8567OP17mo ago
Converted to mp3
gosso8567
gosso8567OP17mo ago
No description
gosso8567
gosso8567OP17mo ago
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const { joinVoiceChannel, EndBehaviorType } = require('@discordjs/voice');
const fs = require('fs');
const { opus } = require('prism-media');
const wav = require('node-wav');

const client = new Client({
intents: Object.values(GatewayIntentBits),
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
rest: 60000
});
const prefix = '!';

client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});

client.on('messageCreate', async (message) => {
if (message.content.startsWith(prefix + 'join')) {
const voiceChannel = message.member?.voice.channel;
if (!voiceChannel) {
return message.reply('このコマンドを使用するには、音声チャンネルに参加している必要があります。');
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
selfDeaf: false
});

connection.receiver.speaking.on('start', (userId) => {
const audio = connection.receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 100,
},
});

const opusDecoder = new opus.Decoder({ channels: 1, rate: 48000, frameSize: 960 });
const pcmData = [];

opusDecoder.on('data', (chunk) => {
pcmData.push(chunk);
});

opusDecoder.on('end', () => {
const wavFileName = `./rec/${Date.now()}-${userId}.wav`;
const wavData = wav.encode(pcmData, {
sampleRate: 48000,
float: false,
bitDepth: 16,
channels: 1,
});
fs.writeFileSync(wavFileName, wavData);
console.log(`✅ Recorded ${wavFileName}`);
});

audio.pipe(opusDecoder);
});
}
});
const { Client, GatewayIntentBits, Partials } = require('discord.js');
const { joinVoiceChannel, EndBehaviorType } = require('@discordjs/voice');
const fs = require('fs');
const { opus } = require('prism-media');
const wav = require('node-wav');

const client = new Client({
intents: Object.values(GatewayIntentBits),
partials: [Partials.Message, Partials.Channel, Partials.Reaction],
rest: 60000
});
const prefix = '!';

client.once('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});

client.on('messageCreate', async (message) => {
if (message.content.startsWith(prefix + 'join')) {
const voiceChannel = message.member?.voice.channel;
if (!voiceChannel) {
return message.reply('このコマンドを使用するには、音声チャンネルに参加している必要があります。');
}

const connection = joinVoiceChannel({
channelId: voiceChannel.id,
guildId: voiceChannel.guild.id,
adapterCreator: voiceChannel.guild.voiceAdapterCreator,
selfDeaf: false
});

connection.receiver.speaking.on('start', (userId) => {
const audio = connection.receiver.subscribe(userId, {
end: {
behavior: EndBehaviorType.AfterSilence,
duration: 100,
},
});

const opusDecoder = new opus.Decoder({ channels: 1, rate: 48000, frameSize: 960 });
const pcmData = [];

opusDecoder.on('data', (chunk) => {
pcmData.push(chunk);
});

opusDecoder.on('end', () => {
const wavFileName = `./rec/${Date.now()}-${userId}.wav`;
const wavData = wav.encode(pcmData, {
sampleRate: 48000,
float: false,
bitDepth: 16,
channels: 1,
});
fs.writeFileSync(wavFileName, wavData);
console.log(`✅ Recorded ${wavFileName}`);
});

audio.pipe(opusDecoder);
});
}
});
I did this but it saves a corrupt wav file. What do you think I should do?
Want results from more Discord servers?
Add your server