0

我正在尝试制作一个音乐机器人,我昨天才开始(使用 discord.js 的语音方面,但我编写普通机器人的时间要长得多)所以我对语音真的很陌生,我不知道我是否遗漏了一些简单的东西,但无论如何我做了这个代码:

var voiceChannel = message.member.voice.channel;
        if (!voiceChannel) return message.channel.send("You have to join a voice channel!")

        voiceChannel.join()
        console.log(`joined channel: ${message.channel.name}`);

        const videoFinder = async (query) => {
            const videoResult = await ytSearch(query);

            return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;

        }

        const streamOptions = { seek: 0, volume: 1 };
        const video = await videoFinder(args.join(' '));

        if (!video) return message.channel.send("The video you searched wasn't found!")

        voiceChannel.join().then(async connection => {

            const stream = ytdl(video.url, { filter: 'audioonly' });
            const dispatcher = await connection.play(stream, streamOptions)

            dispatcher.on('start', () => {
                console.log('audio is now playing!');
            });
            
            dispatcher.on('finish', () => {
                console.log('audio has finished playing!');
            });

            dispatcher.on('error', console.error);

            const videoEmbed = new Discord.MessageEmbed()
                .setColor("RANDOM")
                .setDescription(`Now playing: [${video.title}](${video.url})`)
                .addFields(

                    { name: 'Author:', value: `${video.author.name}`, inline: true },
                    { name: 'Duration:', value: `${video.timestamp}`, inline: true },
                    { name: 'Released:', value: `${video.ago}`, inline: true },

                )
                .setImage(video.image)
                .setFooter(`Total views: ${video.views}`)
                .setTimestamp()

            message.channel.send(videoEmbed)

它只是在 youtube 上搜索视频(使用 ytdl-core 和 ytdl-search),但音频会随机中断,这是它给出的错误: 在此处输入图像描述

我认为可能是错误的部分,const dispatcher = await connection.play(stream, streamOptions)但我不确定。

4

0 回答 0