Hello there,
I am trying to make a discord bot that uses SQLite that stores user id. In order to keep code clean, I have made a file called db.js where I store all my db functions
Mainly focusing on checkUser function. I am trying to make it so that it will return the ID if the user is found in the database. I am using it in my main bot file like so:
When the user registers first time, it works and add's them to database. However, when they register again, it still add's them to database. So I tried to
How can I solve this issue?
Thanks,
salm2s
I am trying to make a discord bot that uses SQLite that stores user id. In order to keep code clean, I have made a file called db.js where I store all my db functions
JavaScript:
const sql = require('sqlite3')
let db = new sql.Database('./userdb.db', sql.OPEN_READWRITE, (err) => {
if (err) console.error(err.message)
})
module.exports = {
addUser: function(id) {
db.run(`INSERT INTO user(id) VALUES(?)`, [id], (err) => {
if (err) console.error(err.message)
console.log("User added")
})
},
removeUser: function(id) {
db.run(`DELETE FROM user WHERE id = ${id}`)
},
checkUser: function (userid) {
var ids = undefined;
db.all(`SELECT id FROM user WHERE id = ${userid}`, (err, rows) => {
if (err) console.log(err.message)
if (rows[0].id != undefined) {
ids = rows[0].id
}
else {
return
}
})
return ids
}
}
Mainly focusing on checkUser function. I am trying to make it so that it will return the ID if the user is found in the database. I am using it in my main bot file like so:
JavaScript:
const query = require('./db')
....
if (query.checkUser(member.user.id) == undefined){
// Add the role
member.roles.add(role)
// And add them to database
query.addUser(member.user.id)
}
console.log(query.checkUser(member.user.id))
and it kept returning undefined
.How can I solve this issue?
Thanks,
salm2s