Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

discord.js returning variables #132

Closed
jerkdavi opened this issue Dec 9, 2020 · 1 comment
Closed

discord.js returning variables #132

jerkdavi opened this issue Dec 9, 2020 · 1 comment

Comments

@jerkdavi
Copy link

jerkdavi commented Dec 9, 2020

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
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants