首页 > 解决方案 > connection.playStream 不是函数

问题描述

我知道关于这个主题有很多问题,但我浏览了所有这些问题,但似乎没有一个答案能解决我的问题。我只是为了好玩而创建了一个 Discord 机器人,我几乎从未使用过 JavaScript 编程,但我想尝试一下。

我的代码如下所示:

    var url = "https://www.youtube.com/watch?v=HHv-s2OqYpw"

    if(!message.member.voice.channel){
        message.channel.send("Join a voice channel.");
        return
    } else {
        vc = message.member.voice.channel;
        connection = await vc.join();
        isValid = ytdl.validateURL(url);
        if(!isValid){
            message.channel.send("The url you gave doesn't exist");
        } else {
            const stream = ytdl(url, {filter: "audioonly"});
            const dispatcher = connection.playStream(stream)

            dispatcher.on("end", function() {
                vc.leave()
                message.channel.send("Done playing the only music I can play lol")
            })

我可以这样做,它可以播放更多歌曲,但我只希望它播放这首歌曲,并且在终端中我收到此错误:

connection.playStream is not a function

我确实安装了 ytdl-core 和 opusscript,它们都有最新版本。我尝试了很多不同的方式来做这件事,这真的让我很恼火,因为我无法弄清楚问题是什么。抱歉英语不好,感谢帮助我的人。祝你今天过得愉快!

标签: javascriptdiscord.js

解决方案


https://discord.js.org/#/docs/main/stable/class/VoiceConnection?scrollTo=play
正如你所看到的,有一个VoiceConnection#play()方法,因为你很可能在 v12 上,这就是你的方法重新寻找。

const stream = ytdl(url, options);
const dispatcher = connection.play(stream);

推荐阅读