Description
I am writing a Discord Bot in NodeJS and am currently experiencing a very odd issue.
What I am wanting to do is to get the result of health via method getHP(), afterwards updating the health property with the setHP() method.
This works for one class, but not for another. So basically, the code is practically the same, but for the other class it does not update the property.
I am calling both the classes their setHP() methods in their constructors.
Code:
// Player.js - This works and displays: { current: 98, max: 98 }
class Player {
constructor(member, msg) {
this.member = member
this.msg = msg
this.setHP()
}
health = {}
setHP() {
this.getHP.then(hp => {
this.health = { current: hp.current, max: hp.current }
})
}
get getHP() {
return new Promise(async (resolve) => {
const stats = await this.stats
resolve(stats.find(stat => stat.id === 'health'))
})
}
get stats() {
return new Promise(async (resolve) => {
const result = await DB.query(`select stats from members where member_id = ${this.member.id} `)
resolve(JSON.parse(result[0][0].stats))
})
}
get difficulty() {
return new Promise(async (resolve) => {
const result = await DB.query(`select difficulty from members where member_id = ${this.member.id} `)
resolve(result[0][0].difficulty)
})
}
}
// Enemy.js - Doesn't work and displays: {}
class Enemy {
constructor(player) {
this.player = player
this.setHP()
}
hp = {}
setHP() {
this.getHP.then(int => {
this.hp = { current: int, max: int }
})
}
get getHP() {
return new Promise(async (resolve) => {
const difficulty = await this.player.difficulty
const int = Math.floor(this.player.health.current * (difficulty * (Math.random() * 0.10 + 0.95)))
resolve(int)
})
}
// minion_fight.js - Where the classes are used
const Enemy = require("Enemy.js")
const Player = require("Player.js")
module.exports.execute = async (msg) => {
const player = new Player(msg.member, msg)
const enemy = new Enemy(player)
// ...
}