Discord.js v13 如何检查静音时间是否设置正确?

Discord.js v13 How to check if mute time is correctly specified?

所以我在 discord.js 中制作了一个 tempmute 命令,我几乎完成了它,只有一件事是万无一失的,那就是静音时间。查看正确的命令用法类似于:!tempmute @user 60m 并将它们静音 60 分钟(我使用的是 npm ms 包 https://www.npmjs.com/package/ms)。当我正确键入命令 aka !tempmute @user 10m 或此包支持的任何其他时间量时,它就像一个魅力。问题是,如果不是以正确的格式给出时间,或者只是把乱码放在那里,它就不起作用并且会导致我的机器人崩溃。我如何防止这种情况发生? 这是我的代码:

const ms = require('ms');
const { Permissions, MessageActionRow, UserFlags } = require('discord.js');

module.exports = {
    name: 'tempmute',
    description: "Temporarily mutes a user",
    execute(message, args)
    {
        const target = message.mentions.members.first();
        let muteRole = message.guild.roles.cache.find(role => role.name === "muted");
        if(message.member.permissions.has(Permissions.FLAGS.MODERATE_MEMBERS))
        {
            if(target)
            {
                let memberTarget = message.guild.members.cache.get(target.id);
                if(target.id == 'myBotsID')
                {
                    message.reply("I can't mute myself.")
                }
                else if(message.member == target)
                {
                    message.reply("You can't mute yourself!")
                }
                else if(memberTarget.permissions.has(Permissions.FLAGS.MODERATE_MEMBERS))
                {
                    
                    message.channel.send(`<@${memberTarget.user.id}> has been muted for ${ms(ms(args[1]))}`);                
                }
                else 
                {
                    if(args[1])
                    {
                        memberTarget.roles.add(muteRole.id);
                        message.reply("<@" + memberTarget.user.id + ">" + "has been muted for " + ms(ms(args[1])))

                        setTimeout(function () {
                            memberTarget.roles.remove(muteRole.id);
                        }, ms(args[1]));
                    }
                    else
                    {
                        message.reply("You have to specify the mute duration (https://www.npmjs.com/package/ms)")
                    }
                }
            }
            else
            {
                message.reply("You have to mention a valid member of this server.")
            }
        }
        else
        {
            message.reply("You can't use that.")
        }
    }
}

这是我遇到的错误:

C:\Users\Miki\Desktop\discord boty\jajco bot\node_modules\ms\index.js:34
  throw new Error(
  ^

Error: val is not a non-empty string or a valid number. val=undefined
    at module.exports (C:\Users\Miki\Desktop\discord boty\jajco bot\node_modules\ms\index.js:34:9)
    at Object.execute (C:\Users\Miki\Desktop\discord boty\jajco bot\commands\tempmute.js:34:99)
    at Client.<anonymous> (C:\Users\Miki\Desktop\discord boty\jajco bot\main.js:100:41)
    at Client.emit (node:events:520:28)
    at MessageCreateAction.handle (C:\Users\Miki\Desktop\discord boty\jajco bot\node_modules\discord.js\src\client\actions\MessageCreate.js:26:14)
    at Object.module.exports [as MESSAGE_CREATE] (C:\Users\Miki\Desktop\discord boty\jajco bot\node_modules\discord.js\src\client\websocket\handlers\MESSAGE_CREATE.js:4:32)
    at WebSocketManager.handlePacket (C:\Users\Miki\Desktop\discord boty\jajco bot\node_modules\discord.js\src\client\websocket\WebSocketManager.js:351:31)
    at WebSocketShard.onPacket (C:\Users\Miki\Desktop\discord boty\jajco bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:444:22)
    at WebSocketShard.onMessage (C:\Users\Miki\Desktop\discord boty\jajco bot\node_modules\discord.js\src\client\websocket\WebSocketShard.js:301:10)
    at WebSocket.onMessage (C:\Users\Miki\Desktop\discord boty\jajco bot\node_modules\ws\lib\event-target.js:199:18)
                        }, ms(args[1]));

在此处,args[1] 未定义(因为消息不包含第二个参数)因此 ms(undefined) returns 错误!

尝试捕获该错误:

if(!args[1]) return message.reply('time not given.')
try {
  let time = ms(args[1])
} catch (err) {
  message.reply("Can't transform into millisecond `"+args[1]+"`")
}