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