0

I'm working on a discord bot using discord.js, and I have a bot.on arrow function that contains a keyv.get arrow function within it. I want to use return; to return out of both functions, but I can't get that to work.

Just using the normal return; at the end just causes it to continue through with the command.

bot.on('message', message=> {
let args = message.content.substring(prefix.length).split(" ");
    switch(args[0]) {
        case 'startgame':
        if (!args[1]) {
                message.channel.send('You did not specify a player for the game!');
                return;
            }

            const startingamount = args[2];
            user1 = message.mentions.users.first();
            user2 = message.author;

            if (typeof startingamount == "undefined") {
                message.channel.send("You did not specify an amount to bet!");
                return;
            }

            keyv.get(user1.id).then(next => {
                console.log(`next is ${next}`);
                if (next <= startingamount) {
                    console.log("Your opponent does not have enough money to start the game!");
                    // I want to end the function here
                    return;
                    // This function stops
                }
                
                // Other Function continues here
            })
})

Then, I tried to do something like what's below, which also didn't work.

bot.on('message', message=> {
    
    
    let args = message.content.substring(prefix.length).split(" ");
    switch(args[0]) {
        case 'startgame':
            if (!args[1]) {
                message.channel.send('You did not specify a player for the game!');
                return;
            }
            
            returnvar = 0;
            const startingamount = args[2];
            user1 = message.mentions.users.first();
            user2 = message.author;

            if (typeof startingamount == "undefined") {
                message.channel.send("You did not specify an amount to bet!");
                return;
            }

            keyv.get(user1.id, 'user').then(next => {
                console.log(`next is ${next}`);
                if (next <= startingamount) {
                    return console.log("Your opponent does not have enough money to start the game!");
                    // want to return out of both functions here
                    returnvar = 1;
                    return;
                }
            })
            
            if (returnvar == 1) {
              return;
              // should return out of both functions
            }

Now, I thought that would work but it doesn't because for some reason returnvar still equals 0, so the if statement doesn't work.

Alex
  • 1

1 Answers1

-1

Do not use return inside a switch to break continuation, instead, use break.

Check out this page on how to use the switch statement.

Redgren Grumbholdt
  • 1,115
  • 1
  • 19
  • 36
  • i return inside `switch` on the daily. this is a non-answer and should have been a comment on the question. – Mulan Jan 01 '21 at 06:19