Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Commit

Permalink
Add a Math command
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Ray committed Sep 12, 2018
1 parent 93fe6be commit e4e4824
Showing 1 changed file with 87 additions and 0 deletions.
87 changes: 87 additions & 0 deletions commands/math.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const superagent = require('superagent');
const math = require('mathjs');

async function request(i) {
return await superagent.get(`http://numbersapi.com/${i}?json`);
}
exports.run = async (client, message, args, level) => {
const input = args.join(' ');
const errmsg = {
exp: 'Couldn\'t evaluate the given expression',
err: 'An Error Occured',
fact: 'Couldn\'t get any facts for that number',
};
let res; // Used to get number facts
let output; // Output from mathjs
let result = {}; // Sends the result message
result.embed = {};
result.embed.fields = [];
result.embed.color = 0x41dae2;

if (args[0].toLowerCase() === 'fact') {
try {
res = await request('random/math');

if (res.body.found) {
result.embed.fields.push({
name: `Fact about ${res.body.number}`,
value: res.body.text,
inline: false,
});
}
} catch (e) {
result = errmsg.err;
}

return message.channel.send(result);
}

if (args.length === 1 && !isNaN(args[0])) {
const ainput = (input.startsWith('.')) ? '0' + input : input;
res = await request(`${ainput.split('.')[0]}/math`);

if (res.body.found) {
result.embed.fields.push({
name: `Fact about ${res.body.number}`,
value: res.body.text,
inline: false,
});
} else {
result = errmsg.fact;
}
} else {
try {
output = math.eval(input).toString();

result.embed.fields.push({
name: 'Input',
value: input,
inline: false,
});

result.embed.fields.push({
name: 'Output',
value: output,
inline: false,
});
} catch (e) {
result = errmsg.exp;
}
}

return message.channel.send(result);
}

exports.conf = {
enabled: true,
guildOnly: false,
aliases: ["calc"],
permLevel: "Systems Alpha/Dev Tester"
};

exports.help = {
name: "math",
category: "Fun",
description: "Calculates a given mathematical expression.",
usage: "math [...args]"
};

0 comments on commit e4e4824

Please sign in to comment.