我正在制作一个新的Discord Bot,我正在使用Classes,但是她没有工作。
这用于运行JavaScript的DiscordBot。
主文件中的代码
async run(client, message, args) {
const Player = require('./../../class/PlayerManager');
const user = new Player(client, message.author.id);
console.log(user.checkIfUserNotExist())
if(user.checkIfUserNotExist()){
user.insertUserIntoDatabase();
message.channel.send('User created')
}
};
类文件中的代码
class Player {
constructor(client, id) {
this.userId = id;
this.client = client;
}
checkIfUserNotExist(){
this.client.database.query(\`SELECT * FROM users WHERE id=${this.userId}\`, function (error, results, fields){
if(error) throw error;
if(!results[0]){
return "true";
}else{
return false;
}
})
}
insertUserIntoDatabase(){
client.database.query(\`INSERT INTO users VALUES (${this.userId}, 0, ${this.client.users.get(this.userId).username})\`);
}
getId(){
console.log(this.userId)
}
}
module.exports = Player;
我希望输出为true,但此行上的输出未定义
console.log(user.checkIfUserNotExist())