Intento reproducir una URL de Youtube, pero cuando comienza a reproducirse, se detiene después de 1 segundo. Los archivos mp3 funcionan bien, simplemente no funciona cuando intento reproducir las URL de Youtube.
Aquí está mi código:
case 'play': { const Channel = ['903334978199388271']; for (const channelId of Channel) { joinChannel(channelId); } function joinChannel(channelId) { Client.channels .fetch(channelId) .then((channel) => { const VoiceConnection = joinVoiceChannel({ channelId: channel.id, guildId: channel.guild.id, adapterCreator: channel.guild.voiceAdapterCreator, }); const player = createAudioPlayer(); VoiceConnection.subscribe(player); player.play( createAudioResource( ytdl('https://www.youtube.com/watch?v=sPPsOmQh76A'), ), ); }) .catch(console.error); } } break;Tuve el mismo problema antes, el problema es ytdl-core , por lo que puede usar ytdl-core-discord su lugar. Aquí hay un ejemplo:
const { Client, Intents, MessageEmbed } = require('discord.js') const ytdl = require('ytdl-core-discord') const { joinVoiceChannel, createAudioPlayer, createAudioResource, StreamType } = require('@discordjs/voice') const client = new Client({ intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_VOICE_STATES /// <= Don't miss this :) ] }); var prefix = '!' client.on('ready', async () => { console.log('Client is Ready...'); }); client.on('messageCreate', async (message) => { if (message.content.trim().toLocaleLowerCase() === prefix + 'play') { const channel = client.channels.cache.get('Channel ID Here') const connection = joinVoiceChannel({ channelId: channel.id, guildId: channel.guildId, adapterCreator: channel.guild.voiceAdapterCreator }); const player = createAudioPlayer(); const resource = createAudioResource(await ytdl('https://www.youtube.com/watch?v=sPPsOmQh76A'), { inputType: StreamType.Opus }); player.play(resource); connection.subscribe(player); } }); client.login('Bot Token Here!');