我的 javascript 变量不会随着函数而改变

时间:2021-05-05 01:54:06

标签: javascript discord discord.js botkit

我一直试图在开始时建立 player1 和 player2 以便我可以执行更多用户特定的操作,但是每次调用没有重新建立变量的命令时,它都会返回未定义。请帮忙

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);
  });
}

0 个答案:

没有答案