discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.36k stars 3.97k forks source link

Voice Receiver Only Working Intermittently #5209

Closed KalebMills closed 3 years ago

KalebMills commented 3 years ago

Please describe the problem you are having in as much detail as possible:

I have created a simple bot that is supposed to record audio. To do this, I loop through the users in a channel, and create a read stream for each user. From those read streams, I pipe to a PassThrough so I only have to deal with a single stream at a higher level. The issue that is occurring, is that I am only getting data from any of the streams intermittently. I restart the service 10-15 times, I might get to see data come through once.

Include a reproducible code sample here, if possible:

Here is a demonstration of what I am doing:

const getAllMemberAudioStreams = (voiceChannel: discord.VoiceChannel, conn: discord.VoiceConnection): PassThrough => {
    const topLevelStream = new PassThrough();
    const members = voiceChannel.members;

    members
    .filter((user) => user.displayName !== 'AudioClipper')
    .forEach((user: discord.GuildMember) => {

        //TODO: Need a way to track all of these streams to allow us to end them manually
        console.log(`Created read stream for ${user.displayName} -- emitting data for topLevelStream`)

        conn.receiver.createStream(user, {
            mode: 'opus',
            end: 'manual'
        })
        .pipe(topLevelStream)
        .on('error', err => {
            console.log(`Got Error: ${JSON.stringify(err)}`);
        })
        .on('end', () => {
            console.log('Stream has ended')
        })
    });

    return topLevelStream;
}

Further details:

I haven't been able to see any errors via the .on('error') event. I have tested this with @discordjs/opus and opusscript, installed ffmpeg after I received a bizarre error there. Oddly this works once in a while, I will get data flowing and audio written to a Buffer, then saved to a file. 90% of the time, the bot connects to the voice channel, creates the streams for each user, but nothing comes through.

Relevant client options:

amishshah commented 3 years ago

Hi there,

We're working on a new implementation of Discord's Voice API that has better playback quality and is more reliable than what we currently support in Discord.js v12 - check it out at https://github.com/discordjs/voice!

The new library solves many of the issues that users are facing, and as part of this, we're dropping built-in support for voice in our next major release. We have a PR (https://github.com/discordjs/discord.js/pull/5402) that adds native support for our new voice library - once this PR is merged, this issue will be closed.

You can still use our new voice library before that PR lands - just take a look at our music bot example to see how to get started upgrading your voice code. By using the boilerplate music player in the example, you can make it even easier to upgrade your code.

Note that the PR above only reduces some of the boilerplate code you'd otherwise have to write - you do not have to wait for the PR to be merged to start using the new voice library.


If you have any questions about this, feel free to:


Specific to this issue:

This event relies on the voice receive feature, which is undocumented by Discord and prone to breaking quite a lot. As it's an undocumented feature, we cannot guarantee support for it. The new voice library will still have support for voice receive, but even then it will not be a stable feature.