Skip to content
This repository has been archived by the owner on Mar 25, 2020. It is now read-only.

stream.on('done', function() {...}); executes twice #271

Open
PatiHox opened this issue Jun 3, 2018 · 1 comment
Open

stream.on('done', function() {...}); executes twice #271

PatiHox opened this issue Jun 3, 2018 · 1 comment

Comments

@PatiHox
Copy link

PatiHox commented Jun 3, 2018

So, I tried to do like here in my code. It worked, but now bot tries to leave from voice channel twice.
error
And not only twice! When I repeat '8-request' message for several times It just starts to spam my console with errors:
lotoferrors

So here is the code (It's not full):

var logger = require('winston');

var servers = { };

function play(channelID, message) {
    var server = servers[bot.channels[channelID].guild_id];
    var voiceChannelID = bot.servers[bot.channels[channelID].guild_id].members[bot.id].voice_channel_id;
    if (server.isPlaying != 1)
    server.isPlaying = 1;
    

    bot.getAudioContext(voiceChannelID, function (error, stream) {
        if (error) return console.error(error);

        fs.createReadStream("myFile.mp3"/*ytdl(server.queue[0], { filter: "audioonly" })*/).pipe(stream, { end: false });

        server.queue.shift();
        logger.info("В очереди теперь " + server.queue.length + " песни(ен)");

        stream.on('done', function () {
            if (server.queue[0]) {
                logger.warn("play() calls itself")
                play(channelID, message);
            }
            else if (!server.queue[0]) {
                logger.warn("play() ended it's work")
                server.isPlaying = 0;
                bot.leaveVoiceChannel(voiceChannelID, function (error, stream) {
                    if (error) return console.error(error);
                });
            }

        });
    });

}

bot.on('message', function (user, userID, channelID, message, evt) {
    var args = message.substring(2).split(' ');
    switch(args[0]) {
        //8-request
        case 'request':
            if (!args[1]) {
                bot.sendMessage({
                    to: channelID,
                    message: 'Уважаемий, ' + user + ', сначала выбери что тебе душа изволит.'
                });
            break;
            }
            If (!bot.servers[bot.channels[channelID].guild_id].members[bot.id].voice_channel_id) {
                bot.sendMessage({
                    to: channelID,
                    message: 'Все бы хорошо... Да только я не в голосовом чате :grimacing:'
                });
            break;
            }

            if (!servers[bot.channels[channelID].guild_id])
                servers[bot.channels[channelID].guild_id] = {
                    queue: [],
                    isPlaying: []
                }

            var server = servers[bot.channels[channelID].guild_id];

            if (server.isPlaying == 1) {
                server.queue.push(args[1]);
                logger.info("В очереди теперь " + server.queue.length + " песни(ен)");
            }
            else {
                server.queue.push(args[1]);
                logger.info("В очереди теперь " + server.queue.length + " песни(ен)");
                play(channelID, message);
            }
					
        break;
    }
}

P.S. Feel free to ask me to post any part of my full code
P.P.S I am sorry if my English is not good.

@ghost
Copy link

ghost commented Jan 5, 2019

@PatiHox You do know breakpoints exist, right? That's much better than manual console logging.

I'm not currently fluent in this library, but it looks like calling play for each item yields unnecessary performance degradation by pulling the file twice using the createReadStream function.

I also cleaned up your code a bit, and fixed a syntax error. Did you copy it wrongly? Here it is.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant