UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'voice' of undefined

51
November 21, 2020, at 10:30 PM

So I'm trying to make a command to play music in a vc, but here's my problem whenever I run the command it gives me this error (node:11) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'voice' of undefined whenever I try to do anything with this command it doesn't work. It just gives me that error over and over and over again. I haven't found any fix to this yet but I'll keep looking. If any of you find out how to fix this please answer my post.

code:

const { Util, voice, Client } = require('discord.js');
const bot = new Client();
module.exports.run = async(message, args) => {
const channel = message.member.voice.channel.join
    if (!channel) return message.channel.send('I\'m sorry but you need to be in a voice channel to play music!');
    const permissions = channel.permissionsFor(message.client.user);
    if (!permissions.has('CONNECT')) return message.channel.send('I cannot connect to your voice channel, make sure I have the proper permissions!');
    if (!permissions.has('SPEAK')) return message.channel.send('I cannot speak in this voice channel, make sure I have the proper permissions!');
    const serverQueue = message.client.queue.get(message.guild.id);
    const songInfo = await ytdl.getInfo(args[0].replace(/<(.+)>/g, '$1'));
    const song = {
        id: songInfo.videoDetails.video_id,
        title: Util.escapeMarkdown(songInfo.videoDetails.title),
        url: songInfo.videoDetails.video_url
    };
    if (serverQueue) {
        serverQueue.songs.push(song);
        console.log(serverQueue.songs);
        return message.channel.send(`✅ **${song.title}** has been added to the queue!`);
    }
    const queueConstruct = {
        textChannel: message.channel,
        voiceChannel: channel,
        connection: null,
        songs: [],
        volume: 2,
        playing: true
    };
    message.client.queue.set(message.guild.id, queueConstruct);
    queueConstruct.songs.push(song);
    const play = async song => {
        const queue = message.client.queue.get(message.guild.id);
        if (!song) {
            queue.voiceChannel.leave();
            message.client.queue.delete(message.guild.id);
            return;
        }
        const dispatcher = queue.connection.play(ytdl(song.url))
            .on('finish', () => {
                queue.songs.shift();
                play(queue.songs[0]);
            })
            .on('error', error => console.error(error));
        dispatcher.setVolumeLogarithmic(queue.volume / 5);
        queue.textChannel.send(`	
Answer 1
const channel = message.member.voice.channel.join

does not exist...

Try using:

const channel = message.member.voice.channel

It checks if the user is in any voice channel. If it does not work then your message.member returns undefined. Try logging message.member to se if it actually returns undefined and find out why since that part of code should work fine.

READ ALSO
Encountered comments is undefined error in react

Encountered comments is undefined error in react

I'm trying to learn react I keep encountering this error while rendering the post dialog box in the website

33
How to find duplicates in mongoDB with two conditions

How to find duplicates in mongoDB with two conditions

I want to find duplicates in the MongoDB atlasI have seen examples for finding duplicates but they didn't take two conditions as I understand it

38
How to create a NPM command which executes a Go program?

How to create a NPM command which executes a Go program?

I have an NPM project (a React App) and want to execute a local go program by defining a command in the NPM packagejson

43
gulp.pipe() and src() not a function

gulp.pipe() and src() not a function

New to Gulp not sure what to do here I installed every dependency gulp, reactify and browserify

35