Monday 29 May 2017

Error in function after moving code

When I move the function 'send()' within this function, the code stops working.

I didn't get any errors, the program just became unresponsive. I have no idea why and it took me an half hour to figure out how to solve the problem. I'd like to know what this caused and how to prevent it in the future.

Thanks in advance.

Working code:

function warn(channelID, userID, reason){
    if(!userExists(channelID, userID)) return;
    var warnings = players[userID].warnings;

    var today = new Date().getTime();
    var active = 0;
    for (key in warnings){
        date = warnings[key].date;
        if(today - date < 60*1000){
            active++;
        }
    }

    if (active >= 3){
        kick(channelID, userID, "You have been automatically kicked after 3 (active) warnings.");
        log("Bot", userID, "kick", "3 warnings");
    }

    send(channelID, reason); //this function
}

Code stopped working after moving the 'send()' function:

function warn(channelID, userID, reason){
    if(!userExists(channelID, userID)) return;
    var warnings = players[userID].warnings;

    var today = new Date().getTime();
    var active = 0;
    for (key in warnings){
        date = warnings[key].date;
        if(today - date < 60*1000){
            active++;
        }
    }

    send(channelID, reason); //this function

    if (active >= 3){
        kick(channelID, userID, "You have been automatically kicked after 3 (active) warnings.");
        log("Bot", userID, "kick", "3 warnings");
    }
}

The code started working again after I added 'return;' add the end of the function.

function warn(channelID, userID, reason){
    if(!userExists(channelID, userID)) return;
    var warnings = players[userID].warnings;

    var today = new Date().getTime();
    var active = 0;
    for (key in warnings){
        date = warnings[key].date;
        if(today - date < 60*1000){
            active++;
        }
    }

    send(channelID, reason);

    if (active >= 3){
        kick(channelID, userID, "You have been automatically kicked after 3 (active) warnings.");
        log("Bot", userID, "kick", "3 warnings");
    }
    return;
}



via Hedylogos

No comments:

Post a Comment