discordjs / discord.js

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

Voice receiver stream error #8778

Open int0x80-sys opened 2 years ago

int0x80-sys commented 2 years ago

Which package is this bug report for?

voice

Issue description

When i susbcribe to the receiver, and add an event listener to the "finish" event of the opusStream, the bot crashes with the follow:

node:events:491
      throw er; // Unhandled 'error' event
      ^

Error [ERR_STREAM_PUSH_AFTER_EOF]: stream.push() after EOF
    at new NodeError (node:internal/errors:387:5)
    at readableAddChunk (node:internal/streams/readable:277:30)
    at AudioReceiveStream.Readable.push (node:internal/streams/readable:228:10)
    at AudioReceiveStream.push (file:///app/node_modules/@discordjs/voice/dist/index.mjs:1102:18)
    at VoiceReceiver.onUdpMessage (file:///app/node_modules/@discordjs/voice/dist/index.mjs:1267:16)
    at VoiceUDPSocket.emit (node:events:513:28)
    at VoiceUDPSocket.onMessage (file:///app/node_modules/@discordjs/voice/dist/index.mjs:265:10)
    at Socket.<anonymous> (file:///app/node_modules/@discordjs/voice/dist/index.mjs:247:48)
    at Socket.emit (node:events:513:28)
    at UDP.onMessage [as onmessage] (node:dgram:922:8)
Emitted 'error' event on AudioReceiveStream instance at:
    at emitErrorNT (node:internal/streams/destroy:157:8)
    at emitErrorCloseNT (node:internal/streams/destroy:122:3)
    at processTicksAndRejections (node:internal/process/task_queues:83:21) {
  code: 'ERR_STREAM_PUSH_AFTER_EOF'
}

Code sample

import { token } from "./config";

import { Client, GatewayIntentBits } from "discord.js";
import {
    EndBehaviorType,
    getVoiceConnection,
    joinVoiceChannel,
    VoiceConnectionStatus
} from "@discordjs/voice";

export const bot = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.GuildVoiceStates
    ]
});

bot.on("messageCreate", (msg) => {
    const { guild, member } = msg;
    if(!guild || !member?.voice.channelId)
        return;

    const channelId = member.voice.channelId;

    if(msg.content !== "?join")
        return;

    let voiceConnection = getVoiceConnection(guild.id);
    if(voiceConnection)
        return;

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

    if(!voiceConnection)
        return;

    voiceConnection.once(VoiceConnectionStatus.Disconnected, () => voiceConnection?.destroy());

    const { receiver } = voiceConnection;
    receiver.speaking.on("start", async (userId) => {
        const opusStream = receiver.subscribe(userId, {
            end: {
                behavior: EndBehaviorType.AfterSilence,
                duration: 100
            }
        });

        opusStream.on("finish", () => console.log("End"));
    });
});

bot.once("ready", () => console.log("Bot Ready..."));

bot.login(token);

Package version

@discordjs/voice@0.11.0

Node.js version

v16.17.1

Operating system

Ubuntu 20.04

Priority this issue should have

Low (slightly annoying)

Which partials do you have configured?

Not applicable (subpackage bug)

Which gateway intents are you subscribing to?

Guilds, GuildVoiceStates, GuildMessages, MessageContent

I have tested this issue on a development release

No response

Asertus1 commented 1 year ago

@discordjs/voice@0.14.0 and node v19.6.1, still got the error.

Error [ERR_STREAM_PUSH_AFTER_EOF]: stream.push() after EOF
    at new NodeError (node:internal/errors:399:5)
    at readableAddChunk (node:internal/streams/readable:285:30)
    at Readable.push (node:internal/streams/readable:234:10)
    at AudioReceiveStream.push (C:\Users\Maxime\Documents\Programmation\_temp\DiscordGPT\node_modules\@discordjs\voice\dist\index.js:1069:18)
    at VoiceReceiver.onUdpMessage (C:\Users\Maxime\Documents\Programmation\_temp\DiscordGPT\node_modules\@discordjs\voice\dist\index.js:1242:16)
    at VoiceUDPSocket.emit (node:events:512:28)
    at VoiceUDPSocket.onMessage (C:\Users\Maxime\Documents\Programmation\_temp\DiscordGPT\node_modules\@discordjs\voice\dist\index.js:293:10)
    at Socket.<anonymous> (C:\Users\Maxime\Documents\Programmation\_temp\DiscordGPT\node_modules\@discordjs\voice\dist\index.js:275:48)
    at Socket.emit (node:events:512:28)
    at UDP.onMessage [as onmessage] (node:dgram:933:8)
Emitted 'error' event on AudioReceiveStream instance at:
    at emitErrorNT (node:internal/streams/destroy:151:8)
    at emitErrorCloseNT (node:internal/streams/destroy:116:3)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21) {
  code: 'ERR_STREAM_PUSH_AFTER_EOF'
}

Could someone take a look at this please ? I don't think it is hard to fix, but I don't have enough knowledge to do it. I am sorry if I am wrong.

nyapat commented 2 months ago

Looks like it's related to the subscribing to the user, not listening to 'finish' (this doesn't exist on Readable btw, it's "end") or destroying the connection