I have this code:
function start(client) {
client.onMessage(async message => {
db.serialize(function() {
var cnt = 0
db.each(`SELECT phone as phone, xp as xp, admin as admin, lastmsg as lastmsg, msgcount as msgcount FROM users`, (err, row) => {
if (err) {
console.error(err.message);
}
if (Object.is(row.phone, message.author)) {
cnt++;
}
console.log(message.author + " - " + row.phone + " - " + cnt)
})
if (cnt > 0) {
console.log(`User ${message.author} exists in database.`);
}
else
{
console.log(`User ${message.author} doesn't exist in database.`);
db.serialize(function() {
db.run(`INSERT INTO users (phone,xp,admin,lastmsg,msgcount) VALUES ("${message.author}",0,0,0,0)`);
console.log(`User ${message.author} has been added to database.`);
})
}
});
});
}
When it recieves a new message, checks if the messsage author's phone number is in the database, if not it adds it. I am using a counter, if the database matches are greater than 0, the phone number is already in the database. The problem is: that if statement doesn't work. I already tried to use an array, adding all numbers to it then using 'array.includes(message.author)', didn't work, I also tried 'if (+cnt > +1)', same, and all questions I found related to this topic were from people that were trying to compare strings.
Edit: I organized the code and provided the whole function.
Aucun commentaire:
Enregistrer un commentaire