所以我正在创建一个不和谐的机器人这个代码就在这里将PM发送给加入服务器的人:
client.on("guildMemberAdd", member => {
let guild = member.guild;
console.log(`${member.user.username} has joined your server`);
member.user.send('Welcome to my server!');
});
我正在尝试用sqlite创建一个数据库来记录加入userID和用户名的人,这就是我所拥有的:
client.on("guildMemberAdd", member => {
let guild = member.guild;
console.log(`${member.user.username} has joined your server`);
sql.run(`UPDATE users SET userId = ${member.id} WHERE userId = ${member.id}`);
member.user.send('Welcome to my server!');
sql.get(`SELECT * FROM users WHERE userId = '${member.id}'`).then(row => {
if (!row) {
sql.run('INSERT INTO users (userId, username) VALUES (?, ?)', [member.id, 1, 0]);
} else {
sql.run(`UPDATE users SET username = ${member.user.username} WHERE userId = ${member.id}`);
}
}).catch(() => {
console.error;
sql.run('CREATE TABLE IF NOT EXISTS users (userId TEXT, username TEXT)').then(() => {
sql.run('INSERT INTO users (userId, username) VALUES (?, ?)', [member.id, 1, 0]);
});
});
});
我得到了:
UnhandledPromiseRejectionWarning:未处理的承诺拒绝(拒绝ID:2):错误:SQLITE_RANGE:绑定或列索引超出范围,代码有什么问题?任何人都可以解决它吗?
答案 0 :(得分:1)
sql.run('INSERT INTO users (userId, username) VALUES (?, ?)', [member.id, 1, 0]);
您提供3个值(member.id,1和0),其中您只有2个(userId和username)。此外,sql.get()
不会返回承诺,因此您无法使用.then()
。第二个参数是回调函数,它接受两个参数,可能的错误和行,所以你的代码看起来应该是
sql.get('SELECT * FROM users WHERE userId = ?',[member.id] , (err,row) => {
if (!row) {
sql.run('INSERT INTO users (userId, username) VALUES (?, ?)', [member.id, 1]);
} else {
sql.run('UPDATE users SET username = ? WHERE userId = ?', [member.user.username, member.id]);
}
})