Back

Technologies:

javascriptjavascript
avatar
Tolerim
21 hours ago

How can I create a Discord music bot using JavaScript that doesn't require someone to sing along with the music in the voice channel?

I need assistance with creating a music bot on Discord using JavaScript. I've written a script, and although it seems to be functional, the bot is not playing any music despite joining the voice channel correctly and returning "Played" with the music URL. I have already searched for solutions, but to no avail. I'm hoping someone can assist me in finding my error. You can find the code I've written below:

if (interaction.commandName === "play") {
            const songQuery = interaction.options.getString("name", true);
            try {
                const search = ytSearch(songQuery);
                search.then(musicSearch => {
                    if (!musicSearch.videos || !musicSearch.videos.length) {
                        interaction.reply({
                            content: "No results found for this search."
                        });
                    } else {
                        const searchRes = musicSearch.videos[0].url;
                        const userChannel = interaction.member.voice.channelId;

                        const audioPlayer = createAudioPlayer();
                        const voice = joinVoiceChannel({
                            adapterCreator: interaction.guild.voiceAdapterCreator,
                            channelId: userChannel,
                            guildId: interaction.guild.id,
                            selfDeaf: false
                        });
                        voice.subscribe(audioPlayer);
                        const ytdlProcess = ytdl(searchRes, {
                            filter: "audioonly"
                        });
                        ytdlProcess.on("error", (error) => console.error(error));

                        const isMusic = createAudioResource(ytdlProcess, {
                            inlineVolume: true
                        });
                        isMusic.volume.setVolume(1);
                        audioPlayer.play(isMusic)

                        interaction.reply({
                            content: `Played : ${searchRes}`
                        });
                    }

                })

            } catch (error) {
                console.error(error);
                interaction.reply({
                    content: `Error`
                });
            }
        }
Considering the lack of recent solutions available to me on this issue, I'm hopeful someone can help me out. Thank you!

Answers(1)
avatar
Tolerim
21 hours ago
Verified Answer
It seems like you are trying to make a music bot on Discord using JavaScript, but are facing an issue where the bot successfully joins the voice channel but no sound is heard. Based on the code snippet you provided, it looks like you might be using the ytdl-core library to process the YouTube video URL and create an audio resource. One issue could be that you are not waiting for the audio resource to be created before playing it. You might want to try using the AudioPlayerState.Idle event to wait for the player to become idle before playing the audio resource. Here is an example of how you could update your code to wait for the audio resource to be created before playing it:
if (interaction.commandName === "play") {
    const songQuery = interaction.options.getString("name", true);
    try {
        const search = ytSearch(songQuery);
        search.then(musicSearch => {
            if (!musicSearch.videos || !musicSearch.videos.length) {
                interaction.reply({
                    content: "No results found for this search."
                });
            } else {
                const searchRes = musicSearch.videos[0].url;
                const userChannel = interaction.member.voice.channelId;

                const audioPlayer = createAudioPlayer();
                const voice = joinVoiceChannel({
                    adapterCreator: interaction.guild.voiceAdapterCreator,
                    channelId: userChannel,
                    guildId: interaction.guild.id,
                    selfDeaf: false
                });
                voice.subscribe(audioPlayer);

                const ytdlProcess = ytdl(searchRes, {
                    filter: "audioonly"
                });
                ytdlProcess.on("error", error => console.error(error));

                const isMusic = createAudioResource(ytdlProcess, {
                    inlineVolume: true
                });

                // Wait for the audio resource to be created before playing it
                audioPlayer.on(AudioPlayerStatus.Idle, () => {
                  audioPlayer.play(isMusic);
                  interaction.reply({
                      content: `Played : ${searchRes}`
                  });
                });

                isMusic.volume.setVolume(1);
            }
        });
    } catch (error) {
        console.error(error);
        interaction.reply({
            content: `Error`
        });
    }
}
I hope this helps! Let me know if you have any other questions.
;