I am working on a bot using discord.js. My goal requires me to leverage SQL to store user information and their points. When I try to use the !points command, I receive a correct response from my console. My !points case statement is however getting back an undefined value. Upon testing without the if statement, I get a message back like this: has undefined points.
bot.on("message", function(message) {
if (message.author.equals(bot.user)) return;
var command = message.content.split(" ")[0];
var param = message.content.split(" ")[1];
if (!VALID_CMDS.includes(command)) return;
var GUILD = bot.guilds.values().next().value;
switch (command) {
case "!hello":
if (param) {
message.channel.send("Hello, " + param);
} else {
message.channel.send("Use a second parameter and try again. FORMAT: !hello <name_goes_here>");
}
break;
case "!points":
if (getPoints(param) > -1) {
message.channel.send(param + " has " + points + " points");
}
break;
}
});
function getPoints(username) {
var GUILD = bot.guilds.values().next().value;
var sql = "SELECT points, user_name FROM users WHERE user_name=" + "'" + username + "'";
db.query(sql, function(err, result) {
if (err) throw err;
if (!resultEmpty(result)) { //The following console log outputs an actual numerical value for result[0].points (not undefined)
console.log(result[0].user_name + " has " + result[0].points + " points");
return result[0].points;
} else {
console.log("User with the name " + username.toString() + " not found")
return -1;
}
});
}