'DiscordJS v13 Music Bot
I'm having a problem trying to get my discord bot to play songs. It is able to enter the voice channel and search for the song, but when it enters it does not play anything, I look at the status of the player and it comes out 'buffering'.
const { joinVoiceChannel, createAudioPlayer, createAudioResource, StreamType, AudioPlayerStatus, NoSubscriberBehavior } = require('@discordjs/voice');
const { Client, Intents, Interaction } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });
const ytdl = require('ytdl-core');
const yrS = require('yt-search');
async function vdSearch(cancion){
const vdE = await yrS(cancion);
return vdE.videos.length > 0 ? vdE.videos[0] : null;
}
client.on('messageCreate', async(message) =>{
switch(args[0]){
case 'play':
const vdRepro = await vdSearch(args[1]);
if(!vdRepro){
message.channel.send("No se ha encontrado esa cancion");
}else{
message.channel.send("Reproduciendo: "+ vdRepro.title);
}
//CONEXION
const stream = ytdl(vdRepro.url, { filter:'audioonly' });
const connection = joinVoiceChannel({
channelId: message.member.voice.channel.id,
guildId: message.guild.id,
adapterCreator: message.guild.voiceAdapterCreator
})
var resource = createAudioResource(stream, {inputType:StreamType.Arbitrary,});
const player = createAudioPlayer({
behaviors: {
noSubscriber: NoSubscriberBehavior.Pause,
},
});
player.play(resource);
connection.subscribe(player);
console.log(player.state);
break;
}
})
Does anyone know how I could solve it? Thanks for taking the time to read
Solution 1:[1]
I would recommend that you use "Distube". Its really easy to use and great for Musik Bots.
https://distube.js.org/#/docs/JS-DisTube/v2/class/DisTube
For example the play() command in Distube is distube.play(message, "searchQuery")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Felix Platzer |
