<分区>
我正在使用 discord.js 开发一个机器人。我的目标要求我利用 SQL 来存储用户信息及其积分。当我尝试使用 !points 命令时,我从控制台收到了正确的响应。然而,我的 !points case 语句返回了一个未定义的值。在没有 if 语句的情况下进行测试时,我收到这样的消息:有未定义的点。
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;
}
});
}