no longer crashes when invalid equation is entered

This commit is contained in:
Emily 2020-10-03 10:53:16 +10:00
parent ba4781f0f4
commit f31f75ae79

View file

@ -1,42 +1,46 @@
var allowed = ["+", "-", "*", "/", "(", ")", " "]; var allowed = ["+", "-", "*", "/", "(", ")", " "];
exports.run = (client, message, args) => { exports.run = (client, message, args) => {
let exercise = args.join(" "); let exercise = args.join(" ");
if (!exercise) { if (!exercise) {
return message.channel.send( return message.channel.send(
`<:error:466995152976871434> No equation provided. Usage :\`${client.commands.get(`calculate`).help.usage}\`` `<:error:466995152976871434> No equation provided. Usage :\`${client.commands.get(`calculate`).help.usage}\``
); );
} }
for (var i = 0; i < exercise.length; i++) { try {
let c = exercise.charAt(i); for (var i = 0; i < exercise.length; i++) {
let found = allowed.find((element) => element === c); let c = exercise.charAt(i);
let found = allowed.find((element) => element === c);
if(c == "0") found = true; if(c == "0") found = true;
if(!(Number(c) || found)) if(!(Number(c) || found))
{ {
return message.channel.send( return message.channel.send(
`<:error:466995152976871434> Invalid equation. Please use \`*\` for multiplication and \`/\` for division!` `<:error:466995152976871434> Invalid equation. Please use \`*\` for multiplication and \`/\` for division!`
); );
} }
} }
let result = (new Function( 'return ' + exercise )()); let result = (new Function( 'return ' + exercise )());
message.channel.send(`\`RESULT:\`\n\`\`\`${result}\`\`\``); message.channel.send(`\`RESULT:\`\n\`\`\`${result}\`\`\``)
}; } catch (err) {
message.channel.send('<:error:466995152976871434> Malformed input.')
}
};
exports.conf = { exports.conf = {
enabled: true, enabled: true,
guildOnly: false, guildOnly: false,
aliases: ["calc", "math"], aliases: ["calc", "math"],
permLevel: "User", permLevel: "User",
requiredPerms: [] requiredPerms: []
}; };
exports.help = { exports.help = {
name: "calculate", name: "calculate",
category: "Utility", category: "Utility",
description: "Solves basic mathematical equations.", description: "Solves basic mathematical equations.",
usage: "calculate [equation]" usage: "calculate [equation]"
}; };