首页 > 解决方案 > voiceChannel.join() 不是函数节点 v16 discord.js

问题描述

我制作了一个播放音乐的机器人。我升级到 NodeJS v16.6.1 并且 voiceChannel.join 不再工作了。我已经尝试过使用const { voiceChannel } = require('@discord.js/voice');,但它只是说找不到模块。代码:

const ytdl = require("ytdl-core");
const ytSearch = require("yt-search");

module.exports = {
    name: 'play',
    description: 'Joins and plays a video from youtube',
    async execute(message, args) {
        const voiceChannel = message.member.voice.channel;
        if (!voiceChannel) return message.channel.send('You need to be in a channel to execute this command');
        const permissions = voiceChannel.permissionsFor(message.client.user);
        if (!permissions.has('CONNECT')) return message.channel.send('You dont have the neccesary permissions');
        if (!permissions.has("SPEAK")) return message.channel.send('You dont have the neccesary permissions');
        if (!args.length) return message.channel.send('Define Video');
        const connection = await voiceChannel.join();
        const videoFinder = async (query) => {
            const videoResult = await ytSearch(query);
            return (videoResult.videos.length > 1) ? videoResult.videos[0] : null;
        }
        const video = await videoFinder(args.join('  '));
        if (video) {
            const stream = ytdl(video.url, {
                filter: 'audioonly'
            });
            connection.play(stream, {
                    seek: 0,
                    volume: 1
                })
                .on('finish', () => {
                    voiceChannel.leave();
                });
            await message.reply(`Now Playing **${video.title}**`)
        } else {
            message.channel.send('No videos found');
        }
    }
}```

标签: javascriptnode.jsdiscorddiscord.js

解决方案


正如错误所说,voiceChannel#join实际上不是一个函数。虽然它确实存在于 Discord.js v12 上,我假设您在更新 Node.js 版本之前使用过它,但请注意,在Discord.js v13 文档VoiceChannel中找不到它。相反,您将迁移到@discordjs/voice,其joinVoiceChannel功能可以用作替代品。


推荐阅读