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