How can I record voice from a VC using discord.js?
I want to record voice from a specific voice channel, I already tried to do it many times but didn't find the solution, I either get a slowed down .pcm audio, an unaudible audio, an audio that progressively slowes down or a 1 second long static sound audio.
I'm on Node.js v20.12.2, also I'm coding in TypeScript
132 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!I already looked at it and implemented the code to my bot and it just gave me a bunch of .ogg files of short static sounds, i even cloned the "recorder" folder in that repository and still got the same issue
what's your original code for trying to get it in pcm
& fwiw recorder worksonmymachine
make sure you have encryption libraries for decrypting the data i guess? & opus libraries for decoding opus
this was my very original code trying to make the recording happen through a slash command: https://sourceb.in/ySomvmqOES
similar to the voice-example in the discordjs github repository but without creating a different function
in that code the "pipeline" function popped up a type error tho (the link to that error is in the code of the link ive provided you), but i ignored it and still ran the code, and it just created a bunch of pcm files and when i listened to them raw on audacity they were just very short static sounds
how about deps then
what?
this
also didn't understand what you tried to say here
oh yeah im installing them i just have sodium left
& for what its worth recorder works on my machine
you dont need them all - only one
then i have all of them installed except sodium
like it works for you?
yes
and i get proper files that i can open and hear my voice in
no static
but what code have you used? like your own or the one from the voice-examples repo
exactly the one from voice-examples/recorder
damn
works with no changes
with the dependecies installed right
i'd hope they were installed otherwise i'd be decrypting packets from thin air
😭
so its okay if i don't have sodium installed?
yes
sodiums a pain in the ass to install anyways
fr i see that
it told me to change thee version of visual studio and it didn't let me
i still have the same problem, even if im using the same code as in the repository :monkaStop:
i installed the same dependecies in that repo code
it creates a bunch of ogg or pcm files (yes i tried both extensions) and all of them are either static or just some weird sound
are you able to send one here
out of curiosity
one of the audio files?
yes
yeah
wait what
the ogg
and if you play that second one it works for you?
what os are you using
windows 10
and what are you using to open the file
audacity
i think the issue is you are missing a codec to properly play the audio
wdym
like to play the audio on your machine you need a codec to properly understand the data in the files
may i ask, what is a codec? 😭
tldr; translates the audio into something you can listen to
oohh i see
so i should convert it to mp3?
it would be audible
the ogg file you sent
yes
looks like this in audacity for me
is that right?
wait ima check
..
lol
maybe because i imported as raw data
if you just drag it in is it correct
that's what i did
oh yeah i opened as a normal audio file and it looked like it looked to you
and it plays correctly?
yeah plays like on discord
okay so its not a problem with a codec
it's that you are trying to read it incorrectly by opening as raw lol
yeah 💀
and i guess for the file mass creation i should just place the createWriteStream outside of the speaking.on event listener
what
oh you want longer audio files
yeah
increase the
duration
in endthat was just a portion of what i said in the voice call
when u stop speaking it will wait 100 ms before ending the audio stream
oh yeah
if u increase it to like 2000 u have 2 seconds before it cuts off
yeah i see
is it bad if i set the end behavior to manual?
no idea what it entails honestly
because i wanted to do this feature for moderation and i prefer recording the whole voice call until its stopped manually
theoretically the opus stream should never end if you do that
yeah i have to call the .destroy function i suppose
yea just chuck a
opusStream.destroy()
when ur done i guessyea
for this error i should just place "as any" on the needed parameters bc i really dont know what is its problem
thats my bot's source code
not the voice-examples one
if you compile does it still show that error
no its just a type error from typescript
whats your typescript version
the latest one
wait
5.5.4
is that even the latest
weird, i have 5.7.0-dev and 4.9.5 and it doesn't show that
you have two versions?
i will try installing one of them
maybe its something they have fixed on the dev version
well kinda, vscode version and globally installed version
can just change it with ctrl shift p
oh okay
uh also one question
are you using the latest versions of @discordjs/voice and @discordjs/opus?
or the same ones in the repository
if by latest you mean dev version yes
i meant this 🤓
types shouldnt be different either way
yeah i guess
¯\_(ツ)_/¯
i see
tired of type errors
pat is there any way you could send me here an example of your recordings? to see what it would sound like
dont use your voice if u want just do fart sounds with yo mouth
💀
thats what i did
i wanna compare both audios
it can be short
if you don't wanna do it just let me know tho its fine
nevermind.
it now records, but im getting a warning of max listeners and it suddenly stops the recording: https://sourceb.in/Aat4yDAnQv
it happens only when i talk
if (receiver.subscriptions.has(userId)) return;
oh it worked, thank you
uh also, when i stop the recording by myself, i get a premature close error from the pipeline function is there any way to prevent that?
does it look like this
yes
it comes from there
when are you ending it
when i run the "stop" subcommand
wait
are you piping it into the file stream in that stop command
or still in the start speaking part
in the start speaking part
you want the whole command code so you can see?
naw just try the former
piping it after you are done with the stream instead
i have the "pipeline" function in the "start" subcommand part and i destroy the opus, out streams and the voice connection aswell at the "stop" subcommand part
when im done with the ogg stream?
pipe after you destroy the voice stream
sry the file stream part was wrong i meant the ogg stream
audio > ogg > file
wait
so
after running "VoiceConnection.destroy()" i should pipe the file?
the audioreceivestream
opusstream
so i should place the after the connection.destroy() function
audio_stream is the opus stream
output is the write stream
try it, should work but i am doing this from reading code not actual experience
alrighty
nah it still gave me the same error, but its fine thank you for trying
i will look for a solution if i find one
@eman
this looks awful but
yes?
however, some weird side effects; no silence is recorded & it wont console log
so if you speak, wait 5 seconds and then speak again it will be as if you didnt leave any gap at all
i don't understand
what are you trying to tell me?
that code works & doesn't get premature end error
to save w/ manual end behavior
using it with a for loop?
for loop doesn't matter; that would just give you all active subscriptions
the
stream.push(null)
is the difference hereaahh
"v" is the opus stream?
yes
but what end behavior
manual?
yes
alr alr let me try this
should i add the
if (receiver.subscriptions.has(user_id)) return;
to it?well the idea for that one was when a user starts speaking you don't want to save multiple files
what but is that code inside the speaking.on event?
i managed to only save one file
with the event
...edited out bc i was wrong incase you are reading this in the future...
receiver.speaking.on('start', userId => { receiver.subscribe(userId, { end: { behavior: EndBehaviorType.Manual } }); });
like that?
nevermind, you have to expand it, sad
if you have a start+end command, you would subscribe in the start and the for loop in the end command, it will handle ending the streams and creating the files
wait so
this i have to put it in the end command:
and this i have to put it in the start command:
nah reread the message i sent i edited it
because it was wrong
oh yeah i knew that i supposed that you wrote
receiver.speaking.on('start', receiver.subscribe)
to write it faster💀
totally
but yeah I tested using stream.push(null) and it saved to a file when I ran it
so I should do that
and this
yes
ive figured out that i get that error when I manually stop the opus stream i guess because i set the end behavior to aftersilence and it actually worked and didn't give me any error
also i don't think i will be implementing the code you've gave me because it would change the whole structure of mines and i don't really want that, sorry about that, appreciate your help tho
also that error does not really affect the audio so if i don't find a solution for it i would just ignore that specific error tbh
alright
Uh i guess now my original problem is now solved, im now closing this post
Thank you so much for your help pat and qjuh too