简体   繁体   English

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

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

I'm making a discord bot that takes commands.我正在制作一个接受命令的 discord 机器人。 I need it to set a variable to be equal to the int you type in, but I'm having trouble thinking of the solution.我需要它将一个变量设置为等于您输入的int ,但我在考虑解决方案时遇到了麻烦。 Currently it looks something like this:目前它看起来像这样:

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

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

This doesn't work, however.然而,这不起作用。 Ideally, I want to be able to type !pack 2 and set the variable to 2. I would do it through user input, but I don't want the bot to wait for input, as the commands won't work anymore.理想情况下,我希望能够键入!pack 2并将变量设置为 2。我会通过用户输入来完成,但我不希望机器人等待输入,因为命令将不再起作用。 I know the function (in another file) works because it will loop twice if I set the variable to be 2 beforehand, for example.我知道 function (在另一个文件中)可以工作,因为例如,如果我事先将变量设置为 2,它将循环两次。

As a general rule whatever a user types in will be read by your code as a String.作为一般规则,用户输入的任何内容都将被您的代码读取为字符串。

Example:例子:

 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