My javascript variables will not change with the function

  botkit, discord, discord.js, javascript

I keep trying to establish player1 and player2 at the beginning so i can perform more user specific actions, but every time a command is called that doesn’t have the variables re-established, it comes back undefined. Please help

var player1hp = 20;
var player2hp = 20;
var player1pot = 3;
var player2pot = 3;
var playerturn = 0;
var player1;
var player2;
module.exports = function(controller){        
  controller.hears("-player1_attack", ["direct_mention", "ambient"], (bot, message) => {
    if(player2hp>=2){
    const embed = new controller.RichEmbed()
    player2hp-=1;
    embed.setThumbnail('https://cdn.glitch.com/5a418a95-d8a1-4407-a723-04a60f57fbc4%2FMap.png?v=1599775345226');
    embed.addField("Success", `${player1}`);
    embed.addField("The current health module of player 2", `${player2hp}`);
      embed.setColor('#4a2271');
    bot.reply(message, embed);
    }
    else if(player2hp){
    const embed = new controller.RichEmbed()
    embed.setThumbnail('https://cdn.glitch.com/5a418a95-d8a1-4407-a723-04a60f57fbc4%2FMap.png?v=1599775345226');
    embed.addField("Success", `${player1}`);
    embed.addField("The current health module of player 2", `${player2hp}`);
      embed.setColor('#4a2271');
    bot.reply(message, embed);
    }
  });

  controller.hears("-player2_attack", ["direct_mention", "ambient"], (bot, message) => {
    const embed = new controller.RichEmbed()
    embed.setThumbnail('https://cdn.glitch.com/5a418a95-d8a1-4407-a723-04a60f57fbc4%2FMap.png?v=1599775345226');
    embed.addField("Success", `${player1}`);
    embed.addField("The current health module of player 2", `${player2hp}`);
      embed.setColor('#4a2271');
    bot.reply(message, embed);
  });
  
  controller.hears("-tstart", ["direct_mention", "ambient"], (bot, message) => {
    var player1 = message.user;
    var player2 = message.mentions.users.filter(user => user.bot === false).last();
    const embed = new controller.RichEmbed()
    embed.setThumbnail('https://cdn.glitch.com/5a418a95-d8a1-4407-a723-04a60f57fbc4%2FMap.png?v=1599775345226');
    embed.addField("Player 1 is", `${player1}`);
    embed.addField("Player 2 is", `${player2}`);
    embed.addField("The current health module", `${player1hp}`);
    embed.addField("Start with player 1", `Type a command ${player1}`);
      embed.setColor('#4a2271');
    bot.reply(message, embed);
  });
}

Source: Ask Javascript Questions

LEAVE A COMMENT