我目前正在尝试为我的不和谐机器人创建一个资金系统。 余额本身正在运行,但是我遇到的问题是,如果用户ID尚不存在,它不会将用户ID /任何余额添加到数据库中。
代码:
render_site()
我不太确定自己在做什么错,可悲的是。 希望有人可以帮助我
答案 0 :(得分:0)
问题在于,当您尝试插入新成员时正在调用一个函数。不用了您要做的就是在表中插入一些内容。不需要功能。
con.query(sql1, [message.author.id, '100']);
注:首次查询数据库时,会调用一堆不需要的参数。您需要的是err
和results
。数据到位后,您无需map
。您可以使用它。
此外,您检查用户是否首先存在的方式有点怪异。您尝试调用可能不存在的内容,因为如果用户存在,SELECT * FROM money WHERE userID = '${message.author.id}'
应该返回准确的行,否则返回零行。因此,在检查ID是否匹配之前,应该检查行是否全部存在。
if (!results.length) {
// here we insert the new user
}
如果查询返回了某些内容,则可以检查userID是否匹配。如果不是这样,则您的数据库有问题,但检查毫无问题。 注意:我们需要在此处使用===
,因为我们要检查确切的值。
if (message.author.id === results[0].userID) {
// return the balance here
}
说完所有步骤后,您产生的查询应该看起来像这样。
con.query(sql, function (err, results) { // we don't need the other values
if (err) throw err; // check if something went wrong
if (!results.length) { // check if there are rows and insert if there aren't
var sql1 = `INSERT INTO money (userID, balance) VALUES ('${message.author.id}', '100')`;
con.query(sql1); // no need for a function here
return message.channel.send(`added 100 coins to ${message.member}'s balance!`); // return here so the rest doesn't get executed
}
// check if the IDs match
if (message.author.id === results[0].userID) {
message.reply(`You have **${results[0].balance}** YeetCoins!`); // we reply to the user who calls this command.
return results[0].money;
}
})