我的代码:
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行。
答案 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()
}});
}
}
错误将被捕获并在通道中发送!