Better Math Game, Remove Math Command

This commit is contained in:
Daniel Odendahl Jr
2017-08-30 16:49:59 +00:00
parent f46864dce3
commit b997537505
3 changed files with 30 additions and 50 deletions
+29 -18
View File
@@ -1,21 +1,20 @@
const Command = require('../../structures/Command');
const { MessageEmbed } = require('discord.js');
const math = require('mathjs');
const { list } = require('../../structures/Util');
const difficulties = ['easy', 'medium', 'hard', 'extreme', 'impossible'];
const operations = {
easy: ['+', '-'],
medium: ['+', '-'],
hard: ['-', '*'],
extreme: ['*', '/'],
impossible: ['/', '^']
const operations = ['addition', 'subtraction', 'multiplication', 'division'];
const operationDisplay = {
addition: '+',
subtraction: '-',
multiplication: '*',
division: '÷'
};
const maxValues = {
easy: 5,
medium: 10,
hard: 50,
extreme: 75,
impossible: 100
easy: 10,
medium: 100,
hard: 500,
extreme: 1000,
impossible: 1000000
};
module.exports = class MathGameCommand extends Command {
@@ -27,6 +26,16 @@ module.exports = class MathGameCommand extends Command {
description: 'See how fast you can answer a math problem in a given time limit.',
clientPermissions: ['EMBED_LINKS'],
args: [
{
key: 'operation',
prompt: `Which operation should be used for the game? Either ${list(operations, 'or')}.`,
type: 'string',
validate: operation => {
if (operations.includes(operation.toLowerCase())) return true;
return `Invalid operation, please enter either ${list(operations, 'or')}.`;
},
parse: operation => operation.toLowerCase()
},
{
key: 'difficulty',
prompt: `What should the difficulty of the game be? Either ${list(difficulties, 'or')}.`,
@@ -42,23 +51,25 @@ module.exports = class MathGameCommand extends Command {
}
async run(msg, args) {
const { difficulty } = args;
const operation = operations[difficulty][Math.floor(Math.random() * operations[difficulty].length)];
const { operation, difficulty } = args;
const value1 = Math.floor(Math.random() * maxValues[difficulty]) + 1;
const value2 = Math.floor(Math.random() * maxValues[difficulty]) + 1;
const expression = `${value1} ${operation} ${value2}`;
const answer = math.eval(expression).toString();
let answer;
if (operation === 'addition') answer = value1 + value2;
else if (operation === 'subtraction') answer = value1 - value2;
else if (operation === 'multiplication') answer = value1 * value2;
else if (operation === 'division') answer = value1 / value2;
const embed = new MessageEmbed()
.setTitle('You have 10 seconds to answer:')
.setColor(0x9797FF)
.setDescription(expression);
.setDescription(`${value1} ${operationDisplay[operation]} ${value2}`);
await msg.embed(embed);
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) return msg.say(`Nope, sorry, it's ${answer}.`);
if (msgs.first().content !== answer.toString()) return msg.say(`Nope, sorry, it's ${answer}.`);
return msg.say('Nice job! 10/10! You deserve some cake!');
}
};
-30
View File
@@ -1,30 +0,0 @@
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: 'Evaluates math expressions.',
args: [
{
key: 'expression',
prompt: 'What do you want to answer?',
type: 'string'
}
]
});
}
run(msg, args) {
const { expression } = args;
try {
const solved = math.eval(expression).toString();
return msg.say(solved).catch(() => msg.say('Invalid statement.'));
} catch (err) {
return msg.say('Invalid statement.');
}
}
};
+1 -2
View File
@@ -1,6 +1,6 @@
{
"name": "xiaobot",
"version": "33.0.3",
"version": "34.0.0",
"description": "Your personal server companion.",
"main": "Shard.js",
"scripts": {
@@ -36,7 +36,6 @@
"discord.js": "github:hydrabolt/discord.js",
"discord.js-commando": "github:gawdl3y/discord.js-commando",
"erlpack": "github:hammerandchisel/erlpack",
"mathjs": "^3.16.3",
"moment": "^2.18.1",
"moment-duration-format": "^1.3.0",
"node-opus": "^0.2.6",