fire, percentage, units

This commit is contained in:
Daniel Odendahl Jr
2018-03-07 22:02:04 +00:00
parent 1fa68a3497
commit b2b13139c5
8 changed files with 127 additions and 66 deletions
+30
View File
@@ -0,0 +1,30 @@
const { Command } = require('discord.js-commando');
module.exports = class PercentageCommand extends Command {
constructor(client) {
super(client, {
name: 'percentage',
aliases: ['percent'],
group: 'number-edit',
memberName: 'percentage',
description: 'Determines the percentage one number is of another.',
args: [
{
key: 'amount',
prompt: 'How much of the maximum value should be used?',
type: 'float'
},
{
key: 'maximum',
prompt: 'What is the maximum value of the percentage?',
type: 'float'
}
]
});
}
run(msg, { amount, maximum }) {
const percentage = (amount / maximum) * 100;
return msg.say(`${amount} is ${percentage}% of ${maximum}.`);
}
};
-59
View File
@@ -1,59 +0,0 @@
const { Command } = require('discord.js-commando');
const { list } = require('../../util/Util');
const units = ['celsius', 'fahrenheit', 'kelvin'];
module.exports = class TemperatureCommand extends Command {
constructor(client) {
super(client, {
name: 'temperature',
aliases: ['temperature-convert', 'convert-temperature', 'temp-convert', 'convert-temp'],
group: 'number-edit',
memberName: 'temperature',
description: `Converts temperatures to/from ${list(units, 'or')}.`,
details: `**Units**: ${units.join(', ')}`,
args: [
{
key: 'base',
prompt: `What temperature unit do you want to use as the base? Either ${list(units, 'or')}.`,
type: 'string',
validate: base => {
if (units.includes(base.toLowerCase())) return true;
return `Invalid base, please enter either ${list(units, 'or')}.`;
},
parse: base => base.toLowerCase()
},
{
key: 'target',
prompt: `What temperature unit do you want to convert to? Either ${list(units, 'or')}.`,
type: 'string',
validate: target => {
if (units.includes(target.toLowerCase())) return true;
return `Invalid target, please enter either ${list(units, 'or')}.`;
},
parse: target => target.toLowerCase()
},
{
key: 'amount',
prompt: 'What temperature should be converted?',
type: 'float'
}
]
});
}
run(msg, { base, target, amount }) { // eslint-disable-line consistent-return
if (base === target) return msg.say(`Converting ${base} to ${target} is the same value, dummy.`);
if (base === 'celsius') {
if (target === 'fahrenheit') return msg.say(`${amount}°C is ${(amount * 1.8) + 32}°F.`);
if (target === 'kelvin') return msg.say(`${amount}°C is ${amount + 273.15}°K.`);
}
if (base === 'fahrenheit') {
if (target === 'celsius') return msg.say(`${amount}°F is ${(amount - 32) / 1.8}°C.`);
if (target === 'kelvin') return msg.say(`${amount}°F is ${(amount + 459.67) * (5 / 9)}°K.`);
}
if (base === 'kelvin') {
if (target === 'celsius') return msg.say(`${amount}°K is ${amount - 273.15}°C.`);
if (target === 'fahrenheit') return msg.say(`${amount}°K is ${(amount * 1.8) - 459.67}°F.`);
}
}
};
+42
View File
@@ -0,0 +1,42 @@
const { Command } = require('discord.js-commando');
const math = require('mathjs');
module.exports = class UnitsCommand extends Command {
constructor(client) {
super(client, {
name: 'units',
aliases: ['convert-units', 'unit-converter'],
group: 'number-edit',
memberName: 'units',
description: 'Converts units to/from other units.',
args: [
{
key: 'base',
prompt: 'What unit type do you want to convert from?',
type: 'string',
parse: base => base.toLowerCase()
},
{
key: 'target',
prompt: 'What unit type do you want to convert to?',
type: 'string',
parse: target => target.toLowerCase()
},
{
key: 'amount',
prompt: 'How many units should be converted?',
type: 'float'
}
]
});
}
run(msg, { base, target, amount }) {
try {
const value = math.unit(amount, base).to(target).toString();
return msg.say(value);
} catch (err) {
return msg.say('Either an invalid unit type was provided or the unit types do not match.');
}
}
};