If started to work on an rpg bot recently.
I was tryng to implement a turned based combat system.
But during the process I've find out that my while loop which is supposed to let the combat run till someone wins, won't send any message which will make the player able to play (console.log comments works but not the message sending via the discord api).
I think it isn't able to resolve itself for some reason.
Here is the code I'm using.
let winner = false;
let filter = m => m.author.id === message.author.id;
message.channel.send("A battle is about to begin !");
do {
message.channel.send(`What attack do you wanna use ?`).then(() => {
message.channel.awaitMessages(filter, {
max: 1,
time: 30000,
errors: ['time']
}).then(message => {
message = message.first()
if (message.content.toUpperCase() == 'Fireball' || message.content.toUpperCase() == 'fireball') {
message.channel.send(`Fireball has been used !`)
} else {
message.channel.send(`Terminated: Invalid Response`)
}
}).catch(collected => {
message.channel.send('Timeout');
});
})
} while (battleStats[0]["CurrentHp"] != 0);
Note: the value of battleStats[0]["CurrentHp"]
is 100 and contain en Int value
Okay so after some thinking, I've decided to get rid of the while loop which was the source of the problem. I've taken inspiration from this post. It made me realize that making a while loop to create a turned based combat isn't necessary at all.
So in order to get over this problem, I've exported an object containing all my player and opponent data by using the
module.exports
module.By requesting it in an other command, we will be able to stock the data and modify it for the turned based combat system.
In case if you were wandering the code I'm using here it is.
Starting the battle command which initiate the data collection about the player and the ennemy. (note that I'm now using an object to store the data and note an Array anymore.
Here is my utility file which contain a set of function necessary for a battle logging system in JSON.
Finally the battle function file to initiate the fight
Note: The attack function isn't adjust for this setup.