繁体   English   中英

将变量设置为用户作为命令键入的最简单方法是什么?

[英]What's the easiest way to set a variable to be what the user types as a command?

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

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

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

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

作为一般规则,用户输入的任何内容都将被您的代码读取为字符串。

例子:

 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" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM