Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

155
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!