【问题标题】:What's the easiest way to set a variable to be what the user types as a command?将变量设置为用户作为命令键入的最简单方法是什么?
【发布时间】:2020-09-16 08:30:06
【问题描述】:

我正在制作一个接受命令的不和谐机器人。我需要将变量设置为等于您输入的int,但我在考虑解决方案时遇到了麻烦。目前它看起来像这样:

switch(args[0]){
    case "test":

        if(args[1] == typeof 'number')
        {
            Functions.noOfPacks = args[1];
        }

但是,这不起作用。理想情况下,我希望能够输入 !pack 2 并将变量设置为 2。 我会通过用户输入来完成,但我不希望机器人等待输入,因为命令将不再起作用。我知道该函数(在另一个文件中)有效,因为如果我事先将变量设置为 2,它将循环两次。

【问题讨论】:

    标签: javascript node.js arguments discord.js


    【解决方案1】:

    一般来说,用户输入的任何内容都会被您的代码读取为字符串。

    例子:

    document.querySelector('#box').onkeyup = function (e) {
      if (e.code !== 'Enter') return;
    
      const v = this.value
      
      if (Number.isNaN(+v)) {
        console.log(`${v} is not a number`)
        this.value = '' //clear the input when it is not a number
      } else {
        console.log(`${v} is a number`)
      }
    }
    <label for="box">Type something in there and press Enter</label>
    <input type="text" id="box" />

    【讨论】:

      猜你喜欢
      • 2015-04-02
      • 2010-11-01
      • 2011-03-22
      • 2023-03-08
      • 2013-12-30
      • 2012-02-29
      • 2012-02-05
      相关资源
      最近更新 更多