discordjs / discord.js

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

playFile(): StreamDispatcher buffer overflowing #1195

Closed PhantomPhire closed 7 years ago

PhantomPhire commented 7 years ago

I've been working on making Discord bots and learning node.js concurrently (so I apologize in advance for any lack of general node.js/javascript conventions/standards) and one of the bots I've made is a simple "follow user into VoiceChannel and play sound, then leave" application. On run-time, it works pretty well for 20-30 iterations of sounds, but then it begins skipping sound playback and instead generating the error: "RangeError: Source is too large". Given the evidence, this definitely sounds like some sort of buffer overflow, and upon debugging, I traced the error to OpusScriptEngine line 18 (encode()), being called by StreamDispatcher's _sendBuffer() function.

It is worth noting that upon each of my successful playbacks, the stream is terminated with the message "Stream is not generating quickly enough."

I am not sure if this an issue with the library or user error, but if it is the latter, I do apologize. Here is the code I am using the call the function for refernce:

function playSound(filePath, memberToFollow)
{
    if(memberToFollow.voiceChannelID != null)
    {
        if(semaphor == 0)
        {
            memberToFollow.voiceChannel.join()
                .then(connection =>
                    {
                        semaphor = 1;
                        console.log('Connected!');
                        const dispatcher = connection.playFile('C:\\DiscordBots\\Sounds\\soundFiles\\' + filePath);

                                                dispatcher.once('end', (reason) =>
                                                {
                                                    console.log("Disconnect reason: " + reason);
                                                    memberToFollow.voiceChannel.leave();
                                                    semaphor = 0;
                                                });
                    })
                .catch(console.error);
        }
    }
}
aemino commented 7 years ago

Could you specify the version of discord.js you're on? Are you on #indev-prism?

PhantomPhire commented 7 years ago

From what I can gather, it seems I am on 11.0.0 (at least from what I can see on the package.json). To be clear, I deleted the discord.js directory in node-modules this morning and reissued "npm install discord.js --save" this morning and retested to make sure I had the latest version before submitting an issue.

I am not familiar with #indev-prism, so I don't believe so. What arguments do I need to issue on npm to get that?

SpaceEEC commented 7 years ago

For installing #indev-prism you'd do: npm i -S hydrabolt/discord.js#indev-prism

PhantomPhire commented 7 years ago

After installing #indev-prism, I no longer encountered the "Stream is not generating quickly enough." status and instead all stream terminations ended with "stream." After testing for approximately four times the length that it took to achieve the "RangeError: Source is too large" that I was getting consistently, I could not achieve the same error at all. It seems this error was resolved with the latest #indev-prism release. Apologies for my ignorance to this release, but it has resolved my issue either way.

aemino commented 7 years ago

No worries. Glad this fixed your issue, and hopefully we can release #indev-prism soon so that everyone can enjoy the fixes it brings. :P (@hydrabolt)