discord.js returning variables
jerkdavi opened this issue · 1 comments
jerkdavi commented
I'm having problems with returning variables from functions, which I run in the external files, to the main part of the program.
The variable in question is joinstatus.
Here is the main part of the program:
bot.on('message', function(message){
**console.log('Main-message; joinstatus='+joinstatus);**
...
if(cmd) {
cmd.run(bot, message, args, servers, server, joinstatus);
}
else{
//console.log('Error! Else passed!');
return;
}
});
Here is the function that runs in the external file:
module.exports.run = async (bot, message, args, servers, server, joinstatus) => {
...
function play(connection, message){
...
server.dispatcher.on('finish', () => {
if(server.queue[0]){
play(connection, message);
} else {
connection.disconnect();
joinstatus='waiting';
console.log('Play-Else; joinstatus='+joinstatus);
servers[message.guild.id] = '';
}
});
**console.log('Play1; joinstatus='+joinstatus);**
**console.log('Play; returning servers, server and joinstatus');**
return servers, server, joinstatus;
}
if(joinstatus==='waiting'){
message.member.voice.channel.join().then(function(connection){
joinstatus='joined';
**console.log('Play2; joinstatus='+joinstatus);**
play(connection, message);
});
**console.log('Play3; joinstatus='+joinstatus);**
**console.log('Play; returning joinstatus');**
return joinstatus;
}
}
module.exports.config = {
command:'PLAY'
}
And this is the result of calling the play function
Main-message; joinstatus=waiting
Play3; joinstatus=waiting
Play; returning joinstatus
Main-message; joinstatus=waiting
Play2; joinstatus=joined
Play1; joinstatus=joined
Play; returning servers, server and joinstatus
Main-message; joinstatus=waiting
Main-message; joinstatus=waiting
What am I doing wrong?
eslachance commented
You seem to be missing some knowledge on how to use promises. Please read this guide on promises which can explain to you why what you're trying to do won't work. If you need further help on the subject, please join our discord server.