Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Leaves channel as soon as the bot starts to play any music - or will skip the song then leave. #848

Closed
Phoenix557 opened this issue Oct 13, 2021 · 18 comments
Labels
bug Something isn't working

Comments

@Phoenix557
Copy link

Phoenix557 commented Oct 13, 2021

Describe the bug

Leaves channel as soon as the bot starts to play any music - or will skip the song then leave.
To Reproduce
Steps to reproduce the behavior:

Expected behavior

Shouldn't do this
Screenshots

Please complete the following information:

  • Node Version: x.x.x
  • Discord Player Version: x.x.x
  • Discord.js Version: x.x.x
    v 16.11 - Discord Player v MOST RECENT - DJS MOST RECENT
    Additional context
@Phoenix557 Phoenix557 added the bug Something isn't working label Oct 13, 2021
@Instinzts
Copy link

I'm running to the same issue when i restart my bot and try to play some music , it joins the vc and then leaves... Not sure if this is a bug.

@thnhmai06
Copy link

thnhmai06 commented Oct 14, 2021

I think it's FFmpeg or youtube-dl-core error. Do you try this?

@dane-thomas
Copy link

dane-thomas commented Oct 16, 2021

I was facing this issue too but was able to get around it by adding these ytdl options to the player.

ytdlOptions: {
    quality: 'highestaudio',
    highWaterMark: 1 << 25,
},

Haven't tested this extensively, but it seems to stop the bot from stopping after playing for a few seconds.

[Edit]: Inspired by the config values in MusicBot by ZerioDev

GetUsernameFromDatabase added a commit to GetUsernameFromDatabase/discord-server-bot that referenced this issue Oct 16, 2021
GetUsernameFromDatabase added a commit to GetUsernameFromDatabase/discord-server-bot that referenced this issue Oct 16, 2021
@Amir-HSD
Copy link

I also have this problem, please help me

@Mr-Leonerrr
Copy link

I was facing this issue too but was able to get around it by adding these ytdl options to the player.

ytdlOptions: {
    quality: 'highestaudio',
    highWaterMark: 1 << 25,
},

Haven't tested this extensively, but it seems to stop the bot from stopping after playing for a few seconds.

[Edit]: Inspired by the config values in MusicBot by ZerioDev

This configuration works perfectly, in the same way, if you have videoonly or audioonly options, it would be good to add the following option:

ytdlOptions: {
    dlChunkSize: 0,
},

@prithvi2k2
Copy link

Had same problem😢, but not gonna fix now, labs + exam overhead.. i didn't prep for any!

@ZEINTRY11
Copy link

Node Version: 16.9.1
Discord Player Version: 5.1.0
Discord.js Version: 13.0.0-dev.2675b08.1627603447
pleas help

@kamaki-mp3
Copy link

I still have the same problem...

@Instinzts
Copy link

**This seems to work ** try it out 👍🏻
const queue = client.musicplayer.createQueue(message.guild, {
ytdlOptions: {
quality: "highest",
filter: "audioonly",
highWaterMark: 1 << 25,
dlChunkSize: 0,
},
metadata: {
channel: message.channel,
}
});

@Arburich
Copy link

Arburich commented Oct 20, 2021

those of you coming from music-bot
Instinzts had it right!

const queue = await player.createQueue(message.guild, {
			ytdlOptions: {
				quality: "highest",
				filter: "audioonly",
				highWaterMark: 1 << 25,
				dlChunkSize: 0,
			},
			metadata: message.channel
		});```

@phxgg
Copy link
Contributor

phxgg commented Oct 26, 2021

Ok so @DevAndromeda said that we can now handle streams ourselves by installing the dev version
npm install discord-player@dev

I used play-dl to handle the streams, and it has solved the problem for me.

First off, I initialize the player like this

client.player = new Player(client, {
    ytdlOptions: {
        quality: 'highestaudio',
        hightWaterMark: 1 << 25,
    }
});

And when I'm creating a queue:

const playdl = require('play-dl');

const queue = await client.player.createQueue(guild, {
    metadata: channel,
    async onBeforeCreateStream(track, source, _queue) {
        // console.log({track: track, source: source});

        if (isSpotify(track.url)) {
            if (playdl.is_expired()) await playdl.refreshToken();
            let spotify_data = await playdl.spotify(track.url);
            
            let yt_query = `${spotify_data.name} by `;
            spotify_data.artists.forEach((key) => yt_query += key.name + ' ');
            console.log(`[${guild.name}] Spotify -> YouTube: ${yt_query}`);

            let search_yt = await playdl.search(yt_query, { limit: 1 });
            return (await playdl.stream(search_yt[0].url)).stream;
        } else {
            return (await playdl.stream(track.url)).stream;
        }
    }
});

I know I could have used if (source === 'spotify') to check the where the track is coming from, but for some reason the Spotify tracks would give me a Spotify URL with a 'youtube' source (is it because of the spotifyBridge option I guess? seems like it indeed is because of the spotifyBridge option 😋 ). Anyways, I made this function:

function isSpotify (url) {
    let re = new RegExp('^(spotify:|https://[a-z]+\.spotify\.com/)');
    return re.test(url);
}

If you would like to try this method with play-dl you should read the Spotify Authorization steps in the package's discussions page; otherwise, playing from Spotify will probably not work.

@dotnize
Copy link
Contributor

dotnize commented Oct 30, 2021

const playdl = require('play-dl');

const queue = await client.player.createQueue(guild, {
    metadata: channel,
    async onBeforeCreateStream(track, source, _queue) {
        // console.log({track: track, source: source});

        if (isSpotify(track.url)) {
            if (playdl.is_expired()) await playdl.refreshToken();
            let spotify_data = await playdl.spotify(track.url);
            
            let yt_query = `${spotify_data.name} by `;
            spotify_data.artists.forEach((key) => yt_query += key.name + ' ');
            console.log(`[${guild.name}] Spotify -> YouTube: ${yt_query}`);

            let search_yt = await playdl.search(yt_query, { limit: 1 });
            return (await playdl.stream(search_yt[0].url)).stream;
        } else {
            return (await playdl.stream(track.url)).stream;
        }
    }
});
function isSpotify (url) {
    let re = new RegExp('^(spotify:|https://[a-z]+\.spotify\.com/)');
    return re.test(url);
}

afaik play-dl can only play youtube links so you're probably gonna get an error if you try to play non-youtube/spotify links (e.g. soundcloud) with that
also is it really better to use regex? i simply used .includes() to check if it's a youtube track and it works perfectly fine for me

const queue = await client.player.createQueue(message.guild,{ metadata: { channel: message.channel },
    async onBeforeCreateStream(track, source, _queue) {
        if (track.url.includes("youtube.com")) {
            // play directly if it's a youtube track
            return (await playdl.stream(track.url)).stream;
        }
        else {
            // search for the track on youtube with the track author & title using playdl.search()
            // i added "lyric" to the search query to avoid playing music video streams
            return (await playdl.stream(await playdl.search(`${track.author} ${track.title} lyric`, { limit : 1, source : { youtube : "video" } }).then(x => x[0].url))).stream;
        }
    }
});

@phxgg
Copy link
Contributor

phxgg commented Oct 30, 2021

@nizeic My code currently is the following:

async onBeforeCreateStream(track, source, _queue) {
  // console.log({track: track, source: source});
  
  try {
      if (isSpotify(track.url)) {
          // Spotify
          if (play_dl.is_expired()) await play_dl.refreshToken();
          let spotify_data = await play_dl.spotify(track.url);
          
          let yt_query = `${spotify_data.name} by `;
          spotify_data.artists.forEach((key) => yt_query += key.name + ' ');
          console.log(`[${guild.name}] Spotify -> YouTube: ${yt_query}`);
  
          let search_yt = await play_dl.search(yt_query, { limit: 1 });
          return (await play_dl.stream(search_yt[0].url)).stream;
      } else {
          // YouTube & SoundCloud
          return (await play_dl.stream(track.url)).stream;
      }
  } catch (err) {
      console.error(`[${guild.name}] Error: onBeforeCreateStream() -> ${err.message}`);
  }
}

I'm actually checking if the track comes from spotify, then it'll search for the track name and artists' names and play it via youtube. Otherwise, if the track comes from either youtube or soundcloud it'll just play the stream. This implementation currently works perfectly for me. The only thing that will not work is soundcloud private links, but I have not had the time yet to look into why this is happening.

About using regex, it's just my way of doing things 😋 A YouTube link doesn't necessarily include youtube.com, but could also be youtu.be. That's why I mostly use regex.

We're kinda doing the same thing, just different implementation.

@dotnize
Copy link
Contributor

dotnize commented Oct 30, 2021

my bad. i thought play-dl could only play directly from youtube
thanks for sharing!

@twlite
Copy link
Collaborator

twlite commented Oct 30, 2021

You can just check the source parameter. Using spotifyBridge: false should give you spotify source no matter what

@Tejas12972
Copy link

here is a lil other fix that I have found

Androz2091/discord-music-bot#24 (comment)

@Yuichiro1019
Copy link

I was facing this issue too but was able to get around it by adding these ytdl options to the player.

ytdlOptions: {
    quality: 'highestaudio',
    highWaterMark: 1 << 25,
},

Haven't tested this extensively, but it seems to stop the bot from stopping after playing for a few seconds.

[Edit]: Inspired by the config values in MusicBot by ZerioDev

thanks man saved my day

@twlite
Copy link
Collaborator

twlite commented Nov 29, 2021

Hello, you can use some stable stream sources/alternatives of ytdl-core with onBeforeCreateStream to fix this issue.

@twlite twlite closed this as completed Nov 29, 2021
GetUsernameFromDatabase added a commit to GetUsernameFromDatabase/discord-server-bot that referenced this issue Jan 11, 2024
GetUsernameFromDatabase added a commit to GetUsernameFromDatabase/discord-server-bot that referenced this issue Jan 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests