Skip to content
Advertisement

UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘voice’ of undefined. In play.js when trying to run a command

I get the error UnhandledPromiseRejectionWarning: TypeError: Cannot read property ‘voice’ of undefined in my code. Is it a problem with dependencies or is it an error in the code. This is my code.

JavaScript

This is my bot.js code

JavaScript

This is a music command which i am trying to make an requires you to be in a voice channel in discord to work.

Advertisement

Answer

The issue you have is a misplacement of the variables you are passing to execute the command. On your /play command file, you must change this line:

JavaScript

To

JavaScript

And you can get rid of the

JavaScript

Since you will now be passing the Discord variable from your command fetcher. But to actually pass in the variable, you gotta go to your bot.js file and change the following lines:

JavaScript

To this:

JavaScript

The variables you will be passing are (client, message, args, Discord), meaning you just need to add them for every single command you will create, otherwise the command won’t work.

The reason why your current command was not working was that you were not calling the client variable after executing the command, meaning the variable message was on the spot of your client variable, having said this, you always got to keep in mind to place these variables (client, message, args, Discord) exactly on the same order as they are in your bot.js file, otherwise, the command will always throw an issue, since they must all be stated, and in the same order.

I hope this helped! Best of luck with your project.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement