Math Command

This commit is contained in:
Daniel Odendahl Jr
2017-09-23 13:08:25 +00:00
parent fb4bb67821
commit c0a75d04f1
3 changed files with 35 additions and 15 deletions
+5 -14
View File
@@ -1,4 +1,5 @@
const Command = require('../../structures/Command');
const math = require('mathjs');
const { stripIndents } = require('common-tags');
const { list } = require('../../structures/Util');
const difficulties = ['easy', 'medium', 'hard', 'extreme', 'impossible'];
@@ -38,28 +39,18 @@ module.exports = class MathGameCommand extends Command {
const value1 = Math.floor(Math.random() * maxValues[difficulty]) + 1;
const value2 = Math.floor(Math.random() * maxValues[difficulty]) + 1;
const operation = operations[Math.floor(Math.random() * operations.length)];
let answer;
switch (operation) {
case '+':
answer = value1 + value2;
break;
case '-':
answer = value1 - value2;
break;
case '*':
answer = value1 * value2;
break;
}
const expression = `${value1} ${operation} ${value2}`;
const answer = math.eval(expression).toString();
await msg.say(stripIndents`
**You have 10 seconds to answer this question.**
${value1} ${operation} ${value2}
${expression}
`);
const msgs = await msg.channel.awaitMessages(res => res.author.id === msg.author.id, {
max: 1,
time: 10000
});
if (!msgs.size) return msg.say(`Time! It was ${answer}, sorry!`);
if (msgs.first().content !== answer.toString()) return msg.say(`Nope, sorry, it's ${answer}.`);
if (msgs.first().content !== answer) return msg.say(`Nope, sorry, it's ${answer}.`);
return msg.say('Nice job! 10/10! You deserve some cake!');
}
};
+28
View File
@@ -0,0 +1,28 @@
const Command = require('../../structures/Command');
const math = require('mathjs');
module.exports = class MathCommand extends Command {
constructor(client) {
super(client, {
name: 'math',
group: 'num-edit',
memberName: 'math',
description: 'Calculates math expressions.',
args: [
{
key: 'expression',
prompt: 'What expression would you like to calculate?',
type: 'string'
}
]
});
}
run(msg, { expression }) {
try {
return msg.say(math.eval(expression).toString()).catch(() => msg.say('Invalid expression.'));
} catch (err) {
return msg.say('Invalid expression.');
}
}
};
+2 -1
View File
@@ -1,6 +1,6 @@
{
"name": "xiaobot",
"version": "42.4.0",
"version": "42.5.0",
"description": "Your personal server companion.",
"main": "Shard.js",
"scripts": {
@@ -37,6 +37,7 @@
"discord.js": "github:hydrabolt/discord.js",
"discord.js-commando": "github:gawdl3y/discord.js-commando",
"erlpack": "github:discordapp/erlpack",
"mathjs": "^3.16.3",
"node-opus": "^0.2.7",
"pg": "^6.4.2",
"pg-hstore": "^2.3.2",