无法读取未定义的属性“播放”

时间:2020-06-14 12:37:41

标签: discord.js

所以,我有这样的代码:

        const voiceid = message.member.voice.channelID;
        const channel = client.channels.cache.get(voiceid);
        channel.join().then(connection => {
            function loopa(connection) {
                const dispatcher = connection.play('./audio/1.mp3');
                dispatcher.on('finish', loopa(connection));
            }
            loopa(connection);
        }).catch(e => {
            console.error(e);
        });

,并在第一次运行(播放音频)后,而不是循环播放,而是返回错误:

TypeError: Cannot read property 'play' of undefined
at StreamDispatcher.loopppp (E:\discordbot\index.js:144:36)
at StreamDispatcher.emit (events.js:327:22)
at finishMaybe (_stream_writable.js:639:14)
at _stream_writable.js:616:5
at StreamDispatcher._final (E:\discordbot\node_modules\discord.js\src\client\voice\dispatcher\StreamDispatcher.js:248:5)
at callFinal (_stream_writable.js:609:10)
at processTicksAndRejections (internal/process/task_queues.js:84:21)

我尝试在play内用const定义参数,但这没有用。 编辑:谷歌并没有太大帮助

1 个答案:

答案 0 :(得分:0)

嗯,两个连接变量之间可能有冲突。试试这个:

        channel.join().then(connection => {
            function loopa(c) {
                const dispatcher = c.play('./audio/1.mp3');
                dispatcher.on('finish', loopa(c));
            }
            loopa(connection);
        }).catch(e => {
            console.error(e);
        });

希望这会有所帮助。