discordjs / discord.js

A powerful JavaScript library for interacting with the Discord API
https://discord.js.org
Apache License 2.0
25.25k stars 3.95k forks source link

24/7 Music Bot stops playing Audio after a couple of hours. #5698

Closed NoahDevelops closed 3 years ago

NoahDevelops commented 3 years ago

Please describe the problem you are having in as much detail as possible:

In a nutshell, I have a Discord Bot that plays music 24/7 in a Stage Channel. It works reliably for up to 6-7+ hours but then proceeds to cease playing audio afterward at a random time. There is no signal received (no green circle), no errors logged in the console, and the bot remains in the channel.

The only thing that can fix this, is restarting the bot entirely. I can even restart the bot while it is still in the channel, and it will start right back up again and start sending audio.

Again, no errors are logged to the console, and there are no outputs from a try { } catch { } statement.

Include a reproducible code sample here, if possible:

const Discord = require("discord.js")
const fs = require("fs")
const ytdl = require("ytdl-core")
const ytsr = require("ytsr")

module.exports = async (client) => {

    const urls = [
        "https://www.youtube.com/watch?v=IEJVXXVwcRU", "https://www.youtube.com/watch?v=m5wel03bCk4", "https://www.youtube.com/watch?v=EZizxNqyRwk",
        "https://www.youtube.com/watch?v=B8yD0vNtsto", "https://www.youtube.com/watch?v=ylaPxScRz1U", "https://www.youtube.com/watch?v=GMP3eTMyCRY", "https://www.youtube.com/watch?v=YSO9LwT5Qjo",
        "https://www.youtube.com/watch?v=QAPirD7zXZw", "https://www.youtube.com/watch?v=eSm4_GDmtlw", "https://www.youtube.com/watch?v=lF4pAbjPRH0", "https://www.youtube.com/watch?v=_vS8GMfQJQU"
    ]

    const guild = client.guilds.cache.get("740584509686284308")
    const vc = await client.channels.fetch("827561225708765214")

    const connection = await vc.join()

    play(connection)

    async function play(connection) {

        const random = urls[Math.floor(Math.random() * urls.length)]

        const stream = ytdl(random, { filter: "audioonly" })

        const dispatcher = connection.play(stream)
        dispatcher.on("finish", () => {
            play(connection)
        })
    }
}

Do keep in mind that this is a recursive function

Further details:

Relevant client options:

Additional Notes:

I don't think this can be a network issue, as this same issue occurs on my Main Machine, a host that I own, and 2 other Virtual Private Server Hosting Companies. (That being GalaxyGate and SkySilk)

NoahDevelops commented 3 years ago

Addition: The same issue occurs on the latest Master Branch build.

iim-ayush commented 3 years ago

This is a issue of youtube as it tries to disconnect the stream before the bot is finished reading the file.

Go to npm-modules/discord.js/src/client/voice/player/BasePlayer.js

And add these lines in FFMPEG_ARGUEMENTS array :

'reconnect', '1', 'reconnect_streamed', '1', 'reconnect_delay_max', '4'

Just re-start your bot and this issue is resolved

Like this comment and I will understand that your problem is resolved.

timnikolsky commented 3 years ago

This is a issue of youtube as it tries to disconnect the stream before the bot is finished reading the file.

Go to npm-modules/discord.js/src/client/voice/player/BasePlayer.js

And add these lines in FFMPEG_ARGUEMENTS array :

'reconnect', '1', 'reconnect_streamed', '1', 'reconnect_delay_max', '4'

Just re-start your bot and this issue is resolved

Like this comment and I will understand that your problem is resolved.

Didn't work for me unfortunately

iim-ayush commented 3 years ago

This is a issue of youtube as it tries to disconnect the stream before the bot is finished reading the file. Go to npm-modules/discord.js/src/client/voice/player/BasePlayer.js And add these lines in FFMPEG_ARGUEMENTS array :

'reconnect', '1', 'reconnect_streamed', '1', 'reconnect_delay_max', '4'

Just re-start your bot and this issue is resolved Like this comment and I will understand that your problem is resolved.

Didn't work for me unfortunately

Do you have same issue as that of him ??

timnikolsky commented 3 years ago

This is a issue of youtube as it tries to disconnect the stream before the bot is finished reading the file. Go to npm-modules/discord.js/src/client/voice/player/BasePlayer.js And add these lines in FFMPEG_ARGUEMENTS array :

'reconnect', '1', 'reconnect_streamed', '1', 'reconnect_delay_max', '4'

Just re-start your bot and this issue is resolved Like this comment and I will understand that your problem is resolved.

Didn't work for me unfortunately

Do you have same issue as that of him ??

yes

iim-ayush commented 3 years ago

Do you have same issue as that of him ??

yes

This seems mostly resolved by upping the highWaterMark value. I don't know if that's considered an acceptable solution though.

fent/node-ytdl-core#402

timnikolsky commented 3 years ago

This seems mostly resolved by upping the highWaterMark value. I don't know if that's considered an acceptable solution though. fent/node-ytdl-core#402

Thank you, I'll try

NoahDevelops commented 3 years ago

This is a issue of youtube as it tries to disconnect the stream before the bot is finished reading the file.

Go to npm-modules/discord.js/src/client/voice/player/BasePlayer.js

And add these lines in FFMPEG_ARGUEMENTS array :

'reconnect', '1', 'reconnect_streamed', '1', 'reconnect_delay_max', '4'

Just re-start your bot and this issue is resolved

Like this comment and I will understand that your problem is resolved.

This just breaks the player all together, no signal is sent, no green circle, it just breaks the audio all together.

iim-ayush commented 3 years ago

This is a issue of youtube as it tries to disconnect the stream before the bot is finished reading the file. Go to npm-modules/discord.js/src/client/voice/player/BasePlayer.js And add these lines in FFMPEG_ARGUEMENTS array :

'reconnect', '1', 'reconnect_streamed', '1', 'reconnect_delay_max', '4'

Just re-start your bot and this issue is resolved Like this comment and I will understand that your problem is resolved.

This just breaks the player all together, no signal is sent, no green circle, it just breaks the audio all together.

If you are using ytdl-core as streaming , it will definitely crash. I recommend ytdl-core-discord and use the following command :

const ytdl = require('ytdl-core-discord');

async function play(connection, url) {
  connection.play(await ytdl(url), { type: 'opus' });
}

If you use ytdl-core-discord along with those FFMPEG_ARGUEMENTS, there will be ultimate smooth play of audio along with high quality audio stream.

I personally use ytdl-core-discord over ytdl-core, I recommend you to shift to ytdl-core-discord.

Like this comment if this helps to fix your problem.

NoahDevelops commented 3 years ago
connection.play(await ytdl(url), { type: 'opus' });

Appreciate the response, I'll run this overnight and see how it goes.

Sliden101 commented 3 years ago

I play a local audio file to my discord bot and every few hours it stops playing music but the console still logs music being played.

NoahDevelops commented 3 years ago

This is a issue of youtube as it tries to disconnect the stream before the bot is finished reading the file. Go to npm-modules/discord.js/src/client/voice/player/BasePlayer.js And add these lines in FFMPEG_ARGUEMENTS array :

'reconnect', '1', 'reconnect_streamed', '1', 'reconnect_delay_max', '4'

Just re-start your bot and this issue is resolved Like this comment and I will understand that your problem is resolved.

This just breaks the player all together, no signal is sent, no green circle, it just breaks the audio all together.

If you are using ytdl-core as streaming , it will definitely crash. I recommend ytdl-core-discord and use the following command :

const ytdl = require('ytdl-core-discord');

async function play(connection, url) {
  connection.play(await ytdl(url), { type: 'opus' });
}

If you use ytdl-core-discord along with those FFMPEG_ARGUEMENTS, there will be ultimate smooth play of audio along with high quality audio stream.

I personally use ytdl-core-discord over ytdl-core, I recommend you to shift to ytdl-core-discord.

Like this comment if this helps to fix your problem.

Issue fixed, appreciate it! :D