Discord.js Bot在使用命令时崩溃

时间:2019-12-13 16:24:51

标签: javascript discord.js

我的问题是当我编写命令时! Temptute(用户)(时间)和in field(时间)我会写例如10 s / m / h / d,它的效果很好,但是当我在该领域bot中写其他字母时,它就会崩溃。 谁能告诉我如何解决?提前致谢! 代码:

  if (command === `${prefix}tempmute`) {
    let tomute = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
    if(!tomute) return message.reply("Nie moge znaleźć określonego użytkownika");
    if(tomute.hasPermission("ADMINISTRATOR")) return message.reply("Ta osoba ma zbyt wysoką rangę lub nie masz permisji!");
    let muterole = message.guild.roles.find(r => r.name === "muted");

    if(!muterole){
      try {
        muterole =  message.guild.createRole({
          name: "muted",
          color: "#0000000",
          permissions: []
        })
        message.guild.channels.forEach(async (channel, id) => {
           channel.overwritePermissions(muterole,{
            SEND_MESSAGES: false,
            ADD_REACTIONS: false
          });
        });
      } catch(e) {
        console.log(e.stack);
      }
    }

    let mutetime = args[1];
    if(!mutetime) return message.reply("Wprowadź czas na jaki ma byc zmutowana osoba")

    tomute.addRole(muterole.id);
    message.reply(`<@${tomute.id}> został zmutowany na ${ms(ms(mutetime))}`);



    setTimeout(function(){
      tomute.id.removeRole(muterole.id);
      message.channel.send(`<@${tomute.id}> został odmutowany!`);
    }, ms(mutetime));

    }
  }

  );

控制台错误:

C:\Users\pr0mo\OneDrive\Pulpit\Discord Rust 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\pr0mo\OneDrive\Pulpit\Discord Rust Bot\node_modules\ms\index.js:34:9)
    at Client.<anonymous> (C:\Users\pr0mo\OneDrive\Pulpit\Discord Rust Bot\bot.js:241:58)
    at Client.emit (events.js:210:5)
    at MessageCreateHandler.handle (C:\Users\pr0mo\OneDrive\Pulpit\Discord Rust Bot\node_modules\discord.js\src\client\websocket\packets\handlers\MessageCreate.js:9:34)
    at WebSocketPacketManager.handle (C:\Users\pr0mo\OneDrive\Pulpit\Discord Rust Bot\node_modules\discord.js\src\client\websocket\packets\WebSocketPacketManager.js:105:65)
    at WebSocketConnection.onPacket (C:\Users\pr0mo\OneDrive\Pulpit\Discord Rust Bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:333:35)
    at WebSocketConnection.onMessage (C:\Users\pr0mo\OneDrive\Pulpit\Discord Rust Bot\node_modules\discord.js\src\client\websocket\WebSocketConnection.js:296:17)
    at WebSocket.onMessage (C:\Users\pr0mo\OneDrive\Pulpit\Discord Rust Bot\node_modules\ws\lib\event-target.js:120:16)
    at WebSocket.emit (events.js:210:5)
    at Receiver.receiverOnMessage (C:\Users\pr0mo\OneDrive\Pulpit\Discord Rust Bot\node_modules\ws\lib\websocket.js:789:20)

1 个答案:

答案 0 :(得分:0)

将您的代码添加到try catch语句中,它不会再崩溃,但是如果您要检查参数是否符合您的要求,则可以进行正则表达式

尝试捕获:

try
{

}catch(error)
{
console.log(error)
}

正则表达式:

var match = /^(-?(?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|weeks?|w|years?|yrs?|y)?$/i.exec(
    mutetime);
 if(!match)return;