This repository has been archived by the owner on Dec 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Credit for this code goes to https://github.com/FlexLabs/Dyno-fun/blob/develop/src/commands/Math.js
- Loading branch information
Matthew Ray
committed
Sep 12, 2018
1 parent
93fe6be
commit e4e4824
Showing
1 changed file
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]" | ||
}; |