Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

150
Visualizações
DiscordJS music bot disconnects immediately after starting to play the song

I try to play a Youtube URL but when it starts playing it stops after 1 second. mp3 files are working fine, it just doesn't work when I try to play Youtube URLs.

Here is my Code:

case 'play':
  {
    const Channel = ['903334978199388271'];

    for (const channelId of Channel) {
      joinChannel(channelId);
    }

    function joinChannel(channelId) {
      Client.channels
        .fetch(channelId)
        .then((channel) => {
          const VoiceConnection = joinVoiceChannel({
            channelId: channel.id,
            guildId: channel.guild.id,
            adapterCreator: channel.guild.voiceAdapterCreator,
          });

          const player = createAudioPlayer();
          VoiceConnection.subscribe(player);
          player.play(
            createAudioResource(
              ytdl('https://www.youtube.com/watch?v=sPPsOmQh76A'),
            ),
          );
        })
        .catch(console.error);
    }
  }
  break;
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I had the same problem before, the problem is ytdl-core, so you can use ytdl-core-discord instead. Here is an example:

const { Client, Intents, MessageEmbed } = require('discord.js')
const ytdl = require('ytdl-core-discord')

const {
    joinVoiceChannel,
    createAudioPlayer,
    createAudioResource,
    StreamType
} = require('@discordjs/voice')

const client = new Client({
    intents: [
        Intents.FLAGS.GUILDS,
        Intents.FLAGS.GUILD_MEMBERS,
        Intents.FLAGS.GUILD_MESSAGES,
        Intents.FLAGS.GUILD_VOICE_STATES /// <= Don't miss this :)
    ]
});

var prefix = '!'

client.on('ready', async () => {
    console.log('Client is Ready...');
});

client.on('messageCreate', async (message) => {

    if (message.content.trim().toLocaleLowerCase() === prefix + 'play') {

        const channel = client.channels.cache.get('Channel ID Here')

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

        const player = createAudioPlayer();
        const resource = createAudioResource(await ytdl('https://www.youtube.com/watch?v=sPPsOmQh76A'), { inputType: StreamType.Opus });

        player.play(resource);

        connection.subscribe(player);
    }
});


client.login('Bot Token Here!');
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda