forked from embee/woomy
		
	no longer crashes when invalid equation is entered
This commit is contained in:
		
							parent
							
								
									ba4781f0f4
								
							
						
					
					
						commit
						f31f75ae79
					
				
					 1 changed files with 43 additions and 39 deletions
				
			
		| 
						 | 
				
			
			@ -1,42 +1,46 @@
 | 
			
		|||
var allowed = ["+", "-", "*", "/", "(", ")", " "];
 | 
			
		||||
exports.run = (client, message, args) => {
 | 
			
		||||
	let exercise = args.join(" ");
 | 
			
		||||
	
 | 
			
		||||
  if (!exercise) {
 | 
			
		||||
    return message.channel.send(
 | 
			
		||||
      `<: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 )());
 | 
			
		||||
  var allowed = ["+", "-", "*", "/", "(", ")", " "];
 | 
			
		||||
  exports.run = (client, message, args) => {
 | 
			
		||||
    let exercise = args.join(" ");
 | 
			
		||||
    
 | 
			
		||||
    if (!exercise) {
 | 
			
		||||
      return message.channel.send(
 | 
			
		||||
        `<:error:466995152976871434> No equation provided. Usage :\`${client.commands.get(`calculate`).help.usage}\``
 | 
			
		||||
      );
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
  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 = {
 | 
			
		||||
  enabled: true,
 | 
			
		||||
  guildOnly: false,
 | 
			
		||||
  aliases: ["calc", "math"],
 | 
			
		||||
  permLevel: "User",
 | 
			
		||||
  requiredPerms: []
 | 
			
		||||
};
 | 
			
		||||
      message.channel.send(`\`RESULT:\`\n\`\`\`${result}\`\`\``)
 | 
			
		||||
    } catch (err) {
 | 
			
		||||
      message.channel.send('<:error:466995152976871434> Malformed input.')
 | 
			
		||||
    }
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
exports.help = {
 | 
			
		||||
  name: "calculate",
 | 
			
		||||
  category: "Utility",
 | 
			
		||||
  description: "Solves basic mathematical equations.",
 | 
			
		||||
  usage: "calculate [equation]"
 | 
			
		||||
};
 | 
			
		||||
  exports.conf = {
 | 
			
		||||
    enabled: true,
 | 
			
		||||
    guildOnly: false,
 | 
			
		||||
    aliases: ["calc", "math"],
 | 
			
		||||
    permLevel: "User",
 | 
			
		||||
    requiredPerms: []
 | 
			
		||||
  };
 | 
			
		||||
 | 
			
		||||
  exports.help = {
 | 
			
		||||
    name: "calculate",
 | 
			
		||||
    category: "Utility",
 | 
			
		||||
    description: "Solves basic mathematical equations.",
 | 
			
		||||
    usage: "calculate [equation]"
 | 
			
		||||
  };
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue