评估命令|错误不会发送到频道,而是在终端| discord.js

时间:2019-12-20 05:54:41

标签: discord.js

我的代码:

if (command === 'eval') {
    let toEval = args.join(' ');
    let evaluated = inspect(eval(toEval, {depth: 0} ));
    try {
        if (toEval) {
            message.channel.send({
                embed: {
                    color: 3066993,
                    title: 'Evaluation Executed!',
                    description: `${evaluated}`,
                    author: {
                        name: message.author.username,
                        icon_url: message.author.avatarURL
                    },
                    timestamp: new Date(),
                }
            });
        }
    } catch(error) {
        message.channel.send({
            embed: {
                color: 15158332,
                title: 'Evaluation Cancelled',
                description: `${error}`,
                author: {
                    name: message.author.username,
                    icon_url: message.author.avatarURL
                },
                timestamp: new Date()
            }
        });
    }
}

我该如何解决?如果我输入“!eval test”,它将在终端(通道中除外)中发送错误。我所知道的错误在于第16行。

1 个答案:

答案 0 :(得分:1)

您必须将eval()函数插入您的try {} catch {}中。只需将代码编辑为类似的内容即可:

if(command === "eval") {
  let toEval = args.join(" ");
  try {
    if(toEval) {
        let evaluated = inspect(eval(toEval, {depth: 0} ))
        message.channel.send({embed: {
        color: 3066993,
        title: "Evaluation Executed!",
        description: `${evaluated}`,
        author: {
          name: message.author.username,
          icon_url: message.author.avatarURL
        },
        timestamp: new Date(),
      }});
    }
} catch(error) {
      message.channel.send({embed: {
      color: 15158332,
      title: "Evaluation Cancelled",
      description: `${error}`,
      author: {
        name: message.author.username,
        icon_url: message.author.avatarURL
      },
      timestamp: new Date()
    }});
  }
}

错误将被捕获并在通道中发送!