#Voice player sometimes doesn't start playing music

73 messages · Page 1 of 1 (latest)

fair zinc
#

Hi, like the title says, the voice player randomly doesn't start playing music

The code responsible for it is the boilerplate from https://discordjs.guide/voice/audio-player.html#playing-audio modified only by a 20sec timeout which just disconnects the bot:

const connection = getVoiceConnection(this.guild.id);
connection?.destroy();```

Relevant package versions:
     "@discordjs/opus": "^0.8.0",
    "@discordjs/voice": "^0.13.0",
    "ffmpeg-static": "^5.1.0",
    "libsodium-wrappers": "^0.7.10",
    "node-opus": "^0.3.3",
    "opusscript": "^0.0.8",

Any leads would be appreciated and in advance I'm sorry for the incomplete info, i can't run it in debug mode right now
mellow berryBOT
#
  • What's your exact discord.js npm list discord.js and node node -v version?
  • Not a discord.js issue? Check out #1081585952654360687.
  • 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!
spiral spindle
#

Do you clear the timeout if someone makes the bot play sound before it stopped?

#

Else it might destroy the connection before the player had a chance to start

fair zinc
#

i thought about that overlap being the case yeah

#

but it doesn't happen often enough as far as i know

#

it would send a message twice within that 20 seconds for example if that timeout was destroying new connections i think

spiral spindle
#

Clear the timeout before the joinVoiceChannel call to mitigate that issue. Store the Timeouts keyed by guildId in a collection and remove them when you clear or the timeout happens

spiral spindle
fair zinc
#

yeah, sorry about that

spiral spindle
#

Because the guide code you referred to doesn’t send messages

fair zinc
#

as in if you called all of it twice within 20 seconds, the first timeout would destroy the second connection

spiral spindle
#

There is only one connection per guild. You get literally the same connection with a joinVoiceChannel call if one exists already

#

So yes, the first timeout would destroy the connection to that guild

fair zinc
#

i'll look through the bot's messages but i doubt that ever happens

spiral spindle
#

20 seconds are a long time. So unless your bot is only on one server with very few members chances are high it will or did already happen

fair zinc
#

is there something else that could be bricking it?

#

there's a few opus libraries installed for example, i'm not sure if they interfere with each other

spiral spindle
#

I would love to help you. But without any code I‘m at the end of my capabilities here

fair zinc
#

I can share it but there's nothing past the boilerplate and what I sent as far as voice goes

#

one sec

#
async makeVote(channel, playersToVote, inGame = true, time = 20000, showResults = true, customTitle = '') {
        playersToVote = this.shuffleArray(playersToVote);
        customTitle = 'x';
        let roleAlive = this.#bot.guild.roles.cache.find((x) => x.id === config.roles.alive.id);
        const playerVoteListIds = playersToVote.map((player) => player.id);

        const pool = new Pool(playerVoteListIds);

        const voteEmbedContent = this.getVoteEmbedContent(pool);
        const voteSelectMenu = new ActionRowBuilder().addComponents(
            new SelectMenuBuilder()
                .setCustomId('x')
                .setPlaceholder('x')
                .addOptions(
                    playersToVote.map((player) => {
                        return {
                            label: `${player.friendlyName}#${player.discordTag}`,
                            value: String(player.id),
                        };
                    })
                )
        );

        const voteEmbed = this.#bot.createEmbed('vote', customTitle, voteEmbedContent);

        const voteMessage = await channel.send({
            content: roleAlive?.toString() ?? '',
            embeds: [voteEmbed],
            components: [voteSelectMenu],
        });
        this.#bot.playVotingMusic(config.channels.rozgrywkaVoice.id);
        pool.messageId = voteMessage.id;
        this.pools.push(pool);
        setTimeout(async () => {
            this.pools = this.pools.filter((p) => p.messageId !== pool.messageId);
            await voteMessage.edit({ embeds: [this.getVoteEmbedResult(pool)], components: [] });
            this.#bot.stopVotingMusic();
        }, time);
    }
    shuffleArray(array) {
        for (let i = array.length - 1; i > 0; i--) {
            let j = Math.floor(Math.random() * (i + 1));
            let temp = array[i];
            array[i] = array[j];
            array[j] = temp;
        }

        return array;
    }```

```js
playVotingMusic(voiceChannelId : string) {

        const connection = joinVoiceChannel({
            channelId: voiceChannelId,
            guildId: this.guild.id,
            adapterCreator: this.guild.voiceAdapterCreator
        });

        const player = createAudioPlayer({
            behaviors: {
                noSubscriber: NoSubscriberBehavior.Pause
            }
        });

        const resource = createAudioResource('./audio/elevator.mp3', { inlineVolume: true });
        resource.volume?.setVolume(0.1);
        player.play(resource);

        connection.subscribe(player);
    }```

```js
stopVotingMusic() {
        const connection = getVoiceConnection(this.guild.id);
        connection?.destroy();
    }```
#

I just looked through its logs and the shortest period between these calls was like 3 minutes

spiral spindle
#

Do I see that right that you have one single VoiceChannel hardcoded in the config? Why make your bot leave the VC/destroy the connection at all then?

fair zinc
#

yes it's only a part of one server. there just wasn't much point in having the bot there throughout the game

#

it could just sit there and play/pause each time but does that change anything?

spiral spindle
#

Since I don’t know yet what your issue is related to I can’t answer that for certain

fair zinc
#

oh, and it doesn't leave the channel, just joins and doesn't play anything so that rules out the overlap

spiral spindle
#

Then it‘s even worse. You destroy the connection without it leaving the VC?

fair zinc
#

i'm waiting for log access, would be a bit easier 😐

fair zinc
#

or in other words it's coded to join, play, and then leave in more or less 20 seconds

#

but it sometimes just joins and doesn't play music

spiral spindle
#

Don’t destroy the connection. Instead do <Guild>.members.me.voice.setChannel(null)

#

To make it leave

fair zinc
spiral spindle
#

Ah yeah, works both ways indeed. My bad

fair zinc
#

from what i understand, disconnect is very similar

#

just lets you rejoin without having to instantiate a new connection

#

there's a new player being created too so it's not just the song ending (the mp3 file is an hour long i think) ppFrown

spiral spindle
#

Why would you play a 1h mp3 but only for 20 seconds each time? Just use a 20s mp3 then

fair zinc
#

they probably didn't want to bother with cutting it

spiral spindle
#

They? Isn’t that your bot?

fair zinc
#

i'm a co-dev on it

spiral spindle
#

Well, cutting the mp3 to 20s is way easier than your setTimeout destroy you have… just have the bot leave on stateChange from playing to idle of the player

fair zinc
#

that's true, but if something else is already scheduled there, may as well

#

do you know if any errors would be logged without the debug mode enabled for both?

spiral spindle
#

Errors are only logged if you log them. In the code you showed you don’t so probably not

fair zinc
#

rip

#

i've also been told just now that the bot only does that whenever there's more people playing (more people on the voice channel)

#

there wouldn't be any extra intents, verifications or anything like that needed by default right?

spiral spindle
spiral spindle
fair zinc
#

oh yeah you can't, we have that

spiral spindle
#

Because you have multiple people using the bot?

#

As Stated before, first one destroying the connection for the second one

fair zinc
#

wait, how come

#

it's only playing in one voice channel

#

and the initial play is triggered from a web panel which doesn't happen often enough

spiral spindle
#

And how was I supposed to know that? You gave me only snippets of code and without context I of course assumed it was code of your bot, not a web panel

fair zinc
#

it is the bot's code, just called elsewhere

spiral spindle
#

Elsewhere as in you have that code running in two processes?

fair zinc
fair zinc
#

i.e. the function i sent doesn't get called anywhere within the bot, it's just exposed

spiral spindle
#

Does that panel run in the same process? Or is it an API running on the bot process? Or do you actually import the bot code in the separate process the panel runs from?

fair zinc
#

it's all 1 process as far as i know, the bot being imported to an express server

limber heron
#

hopefully unrelated, but I noticed some of your deps, including @discordjs/voice, appear to be outdated
even if it doesn't fix this particular issue, I'd recommend updating to avoid some other issues

fair zinc
#

yep, will do. thanks

fair zinc
#

okay, a bit more context this time