简体   繁体   中英

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. 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. 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. I know the function (in another file) works because it will loop twice if I set the variable to be 2 beforehand, for example.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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