amishshah / ytdl-core-discord

A ytdl-core wrapper focused on efficiency for use in Discord music bots
Apache License 2.0
67 stars 26 forks source link

The music stops randomly regardless of the link given #401

Open ShadowGearTv opened 9 months ago

ShadowGearTv commented 9 months ago

Here is my code

`js const { joinVoiceChannel, createAudioPlayer, createAudioResource, getVoiceConnection, entersState, AudioPlayerStatus } = require('@discordjs/voice'); const ytdlDiscord = require('ytdl-core-discord');

module.exports = { name: "play", description: "→ Lance une musique grâce à un URL", options: [ { name: 'url', type: 'STRING', description: 'L\'URL de la musique à jouer', required: true, }, ], runSlash: async (client, interaction) => { const member = interaction.member; const voiceChannel = member.voice.channel;

    if (!voiceChannel) {
        return interaction.reply("Tu dois être dans un salon vocal pour utiliser cette commande!");
    }

    const url = interaction.options.getString('url');

    console.log('URL:', url);

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

        console.log('Bot connecté au canal vocal');

        const player = createAudioPlayer();

        // Ajuste le volume lors de la création de la ressource audio (ajuste la valeur selon tes besoins)
        const resource = createAudioResource(await ytdlDiscord(url), { 
            filter: "audioonly",
            fmt: "mp3",
            highWaterMark: 1 << 30,
            liveBuffer: 20000,
            dlChunkSize: 4096,
            bitrate: 128,
            quality: "lowestaudio",
            inlineVolume: 0.3,
        });

        console.log('Ressource audio créée et chargée');

        player.play(resource);

        console.log('Lecture lancée');

        if (getVoiceConnection(voiceChannel.guild.id)) {
            getVoiceConnection(voiceChannel.guild.id).subscribe(player);
        } else {
            connection.subscribe(player);
        }

        player.on('stateChange', (oldState, newState) => {
            console.log('Changement d\'état:', oldState.status, '→', newState.status);

            if (newState.status === AudioPlayerStatus.Idle) {
                console.log('La lecture est terminée.');

                try {
                    // Détruit la ressource audio après la fin de la lecture
                    resource.playStream.destroy();
                } catch (error) {
                    console.error('Erreur lors de la gestion de la fin de la lecture:', error);
                }
            }
        });

        // Répond à l'interaction après le démarrage de la lecture
        await interaction.reply(`La musique à l'URL ${url} est en train d'être jouée dans le salon vocal ${voiceChannel.name}!`);
    } catch (error) {
        console.error('Erreur lors de la lecture de la musique:', error);
        interaction.reply(`Une erreur s'est produite lors de la lecture de la musique: ${error.message}`);
    }
}

}; `

Is here are my dependencies :

json { "dependencies": { "@discordjs/opus": "^0.9.0", "@discordjs/voice": "^0.16.1", "discord-reply": "^0.1.2", "discord.js": "^13.6.0", "dotenv": "^16.0.3", "ffmpeg": "^0.0.4", "glob": "^7.2.0", "moment": "^2.29.4", "mongoose": "^7.0.3", "ms": "^2.1.3", "undici": "^5.21.0", "yt-search": "^2.10.4", "ytdl-core": "^3.4.2", "ytdl-core-discord": "^1.3.1" } }

I don't understand why it stops the music, when it does it gives me this error code " Uncaught Exeception :Error: aborted "