简体   繁体   English

Discord.js 在某些情况下遇到 args 问题

[英]Discord.js having trouble with args in cases

I'm having trouble getting my command to register more than 1 argument but it doesn't seem to want to work anyway I put it and it isn't giving off errors it will just go to the default case if the arguments are more than one.我无法让我的命令注册超过 1 个参数,但它似乎无论如何都不想工作,我放了它,它不会发出错误,如果 arguments 超过 go一。 it worked when I had it args1 = args[1] but I am now trying to incorporate spacing in some arguments and it falls short there.当我拥有它 args1 = args[1] 时它起作用了,但我现在正试图在一些 arguments 中加入间距,但它在那里不足。

const Discord = require('discord.js');
const util = require('../../Util/utils');
const trickarray = require (`../../json/broomtricks`);

module.exports.run = async (bot, message, args) => {

    let mention = message.mentions.users.first();
    let london = "london";
    let godricshollow = "godrics hollow";
    let ministry = "ministry of magic";
    //Code Start

    let opt = args.shift(` `).toLowerCase()

    let itemName = "broomstick";

    let userDB = bot.db.get(message.author.id)

    let item = bot.config.shop[itemName]
    if(!item || !userDB.inv.some(i => i.name == itemName)){
        message.channel.send(bot.embed(`You don't have a broomstick`))
    }else{



       
        switch(opt[0]){

            case 'travel':
                 switch (opt[1]){
                     case 'london':
                        bot.db.set(`${message.author.id}.location`, london)
                        message.channel.send(bot.embed(`${message.author} Used their broomstick to travel to london.`))
                        console.log(`${message.author.username} travelled to ${userDB.location} using their broomstick.`)
                        break;
                     
                     case 'godrics':
                        bot.db.set(`${message.author.id}.location`, godricshollow)
                        message.channel.send(bot.embed(`${message.author} Used their broomstick to travel to godrics hollow.`))
                        console.log(`${message.author.username} travelled to ${userDB.location} using their broomstick.`)
                        break;
                     case 'ministry':
                         bot.db.set(`${message.author.id}.location`, ministry)
                         message.channel.send(bot.embed(`${message.author} Used their broomstick to travel to the Ministry Of Magic.`))
                         console.log(`${message.author.username} travelled to ${userDB.location} using their broomstick.`)
                         break;                 
                     default:
                         message.channel.send(bot.embed(`london | godrics hollow | Ministry of Magic`))
                         break;

                }

            break;

            case 'race':
                let flip = Math.floor(Math.random(1 * 101))
                switch(!mention){
                      case flip > 51:
                        message.channel.send(bot.embed(`You race ${mention || `a random`} and placed **1st**`))
                        break;

                      case flip < 50:
                        message.channel.send(bot.embed(`You race ${mention || `a random`} and placed **last**`))
                        break;

                };
                break;
            case 'trick':
                let trick = util.dynamicgenerator(trickarray);
                message.channel.send(`You decide to do a trick on your broom you do a **${trick}**.`)
                break;
            

            
        default:
            message.channel.send(bot.embed(`>broomstick travel | race | trick  `))
          
        
             }
            }

    
    //Code End

}

is there another way to format the "opt[1]" any help appreciated?还有另一种格式化“opt [1]”的方法吗?任何帮助表示赞赏?

Quoting the mdn docs:引用mdn文档:

The shift() method removes the first element from an array and returns that removed element. shift() 方法从数组中删除第一个元素并返回删除的元素。 This method changes the length of the array.此方法更改数组的长度。

So what you are doing with opt[0] is accessing the first character of a string, instead of the first value of an array.所以你用opt[0]做的是访问字符串的第一个字符,而不是数组的第一个值。 In addition, adding a string with a space as argument to the shift method does nothing.此外,添加一个带有空格的字符串作为shift方法的参数什么都不做。 You can remove the opt part and just do switch(args[0].toLowerCase()) { directly.您可以删除opt部分,然后直接执行switch(args[0].toLowerCase()) {

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

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