Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

142
Vistas
keep playing the song even if someone else joined the channel

Hello I'm trying to make the bot play specific songs then I want the bot to leave the channel and I made it, but the problem is when someone else joins the channel while the bot is playing it restart playing the songs

client.on('voiceStateUpdate', async (oldState, newState) => {
    const channel = client.channels.cache.get('921118985876017217')
    if (oldState.channel == null && newState.channel == channel) {
  await new Promise(function (resolve , reject) {
            resolve(
                joinVoiceChannel({
                    channelId: channel.id,
                    guildId: newState.guild.id,
                    adapterCreator: newState.guild.voiceAdapterCreator
                }))
            let stream = ytdl('https://www.youtube.com/watch?v=TU1i6I1ms6s', {
                filter: "audioonly",
                fmt: "mp3",
                encoderArgs: ['-af', 'bass=g=10'],
            })

            let secStream = ytdl('https://www.youtube.com/watch?v=wHqKkiHlvJc&list=RDwHqKkiHlvJc&start_radio=1', {
                filter: "audioonly",
                fmt: "mp3",
                encoderArgs: ['-af', 'bass=g=10'],
            })
         
            const resource = createAudioResource(stream, {
                inputType: StreamType.Arbitrary,
                inlineVolume: true,
            });
            // resource.volume.setVolume(0.5);
            const secReasorce = createAudioResource(secStream, {
                inputType: StreamType.Arbitrary, /* StreamType.Arbitrary type of the song like mpe */
                inlineVolume: true,
            });
            const player = createAudioPlayer();
            player.play(resource)
            const connection = getVoiceConnection(oldState.guild.id)
            connection.subscribe(player)
            player.on(AudioPlayerStatus.Idle, () =>setTimeout(() => {
                try{
                    {player.play(secReasorce)}
                }catch{
                    connection.destroy()
                }
            }, 3000))
            player.on( AudioPlayerStatus.Playing , ()=>{
                //I think there will be the solution but I don't know any functions does what I want
            } )
        })

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You have to keep track of whether it's already playing audio or not. Since you're getting the channel based off of an ID; I'm assuming you're only using it in one voice channel. So you could just have a boolean outside of the lambda expression's scope that keeps track of whether it's playing.

If you implement that you'd have something like this:

let isPlaying = false; // Create a variable to keep track of whether it's playing or not.

client.on('voiceStateUpdate', async (oldState, newState) => {

    // If it's already playing, return and do nothing.
    if (isPlaying) return;

    const channel = client.channels.cache.get('921118985876017217')
    if (oldState.channel == null && newState.channel == channel) {
            await new Promise(function (resolve , reject) {
            resolve(
                joinVoiceChannel({
                    channelId: channel.id,
                    guildId: newState.guild.id,
                    adapterCreator: newState.guild.voiceAdapterCreator
                }))

            isPlaying = true; // Bot successfully joined vc, it now begins to play audio.

            // Removed code unrelated to the answer so you can more easily see the changes.

            player.on(AudioPlayerStatus.Idle, () =>setTimeout(() => {
                try{
                    {player.play(secReasorce)}
                }catch{
                    // I assume this is where you make it disconnect? 
                    // If so, retain the integrity of `isPlaying` by setting it back to false.
                    isPlaying = false;
                    connection.destroy()
                }
            }, 3000))
        })```
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda