【问题标题】:My coinflip function isnt outputting anything [closed]我的硬币翻转功能没有输出任何东西[关闭]
【发布时间】:2021-08-21 19:52:24
【问题描述】:

(忽略愚蠢的消息回复,内部笑话) - 我想知道为什么我的硬币翻转不起作用。我得到了第一个消息功能可以正常工作,没有任何问题,但是硬币翻转一个根本不起作用。 (我已经包含了几乎所有经过审查的完整代码)这是在 VSC 中使用 JS、Node 和 Discord.js。

console.log('gamercum');

const Discord = require('discord.js');
const client = new Discord.Client();
client.login('ODUwMDI5MjUxMzIyNTc2OTI0.YLjxbg.EZBRMra3o8Srs4gBCwLz_1NQgTU');

client.on('ready', readyDiscord);

function readyDiscord() {
    console.log('boolin');
}

function coinFlip() {
    var flip = Math.floor(Math.random());
    if(flip = 1) {
        msg.reply('heads');
    } else {
        msg.reply('tails');
    }
}


client.on('message', gotMessage);

function gotMessage(msg) {
    console.log(msg.content);
    if(msg.content === 'gamercum' || msg.content === 'Gamercum' || msg.content === 'gamer cum' 
|| msg.content === 'Gamer cum') {
        msg.reply('we pimp chimpin');
    }
}

client.on('message', getMessage);

function getMessage(msg) {
   console.log(msg.content);
     if(msg.content === 'coinflip') {
        coinFlip();
     }
 }

【问题讨论】:

  • 我对 javascript 不太熟悉,但你说 if (flip = 1) 但不应该是 if(flip == 1) 吗?
  • 定义“不工作”

标签: javascript discord discord.js


【解决方案1】:

var flip = Math.floor(Math.random()); 将永远给你 0。
你可能想要Math.round(Math.random())

【讨论】:

    【解决方案2】:

    除了 Musa 所说的之外,主要问题是您在 if 语句中分配了翻转。

    if(flip = 1)
    

    应该是

    if(flip == 1)
    

    这是一个可以使用 coinflip 功能的 repl。我将 msg.reply 替换为 console.log 进行测试。

    https://replit.com/@AdrianEdelen/ImpoliteDishonestGraphicslibrary#index.js

    【讨论】:

      【解决方案3】:

      这里看起来可能存在多个问题。除了分配而不是比较和数学函数之外,您可能还需要将调用函数中的msg 传递给coinFlip

      function coinFlip(msg) {
          var flip = Math.round(Math.random());
          if (flip == 1) {
              msg.reply('heads');
          } else {
              msg.reply('tails');
          }
      }
      
      function getMessage(msg) {
          console.log(msg.content);
          if (msg.content === 'coinflip') {
              coinFlip(msg);
          }
      }
      

      【讨论】:

      • 谢谢!现在一切正常。我对编码很陌生,我想我可能已经咬得比我能咀嚼的更多了哈哈。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-24
      • 2021-01-16
      • 2023-03-07
      • 2016-04-02
      • 2017-01-28
      • 2014-06-19
      • 1970-01-01
      相关资源
      最近更新 更多