From 1f2ad31ad3cb6d7e831e83506d7f106b31558ba9 Mon Sep 17 00:00:00 2001 From: Jonathan Date: Wed, 24 Feb 2021 20:13:56 -0500 Subject: [PATCH] feat(tempconvert): add tempconvert command --- package-lock.json | 169 +++++++++++++++++++ package.json | 2 + src/commands/Unit Conversions/tempconvert.ts | 49 ++++++ src/languages/en-US.ts | 16 ++ src/lib/types/Languages.d.ts | 3 + 5 files changed, 239 insertions(+) create mode 100644 src/commands/Unit Conversions/tempconvert.ts diff --git a/package-lock.json b/package-lock.json index 515d0b3..5949a4c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1764,6 +1764,12 @@ "integrity": "sha512-htRqZr5qn8EzMelhX/Xmx142z218lLyGaeZ3YR8jlze4TATRU9huKKvuBmAJEW4LCC4pnY1N6JAm6p85fMHjhg==", "dev": true }, + "@types/convert-units": { + "version": "2.3.3", + "resolved": "https://registry.npmjs.org/@types/convert-units/-/convert-units-2.3.3.tgz", + "integrity": "sha512-oo4ZyjU0nZMDQFP0AKYk4KvFjydlSpbgXTCdF+TC3d/TL4b85/zi0W5pC97UrzXXpwsbcVEfS01nEgUqKgjSww==", + "dev": true + }, "@types/eslint-visitor-keys": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/@types/eslint-visitor-keys/-/eslint-visitor-keys-1.0.0.tgz", @@ -2789,6 +2795,15 @@ "safe-buffer": "~5.1.1" } }, + "convert-units": { + "version": "2.3.4", + "resolved": "https://registry.npmjs.org/convert-units/-/convert-units-2.3.4.tgz", + "integrity": "sha512-ERHfdA0UhHJp1IpwE6PnFJx8LqG7B1ZjJ20UvVCmopEnVCfER68Tbe3kvN63dLbYXDA2xFWRE6zd4Wsf0w7POg==", + "requires": { + "lodash.foreach": "2.3.x", + "lodash.keys": "2.3.x" + } + }, "copy-descriptor": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/copy-descriptor/-/copy-descriptor-0.1.1.tgz", @@ -5998,18 +6013,172 @@ "integrity": "sha512-PlhdFcillOINfeV7Ni6oF1TAEayyZBoZ8bcshTHqOYJYlrqzRK5hagpagky5o4HfCzzd1TRkXPMFq6cKk9rGmA==", "dev": true }, + "lodash._basebind": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash._basebind/-/lodash._basebind-2.3.0.tgz", + "integrity": "sha1-K1vEUqDhBhQ7IYafIzvbWHQX0kg=", + "requires": { + "lodash._basecreate": "~2.3.0", + "lodash._setbinddata": "~2.3.0", + "lodash.isobject": "~2.3.0" + } + }, + "lodash._basecreate": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash._basecreate/-/lodash._basecreate-2.3.0.tgz", + "integrity": "sha1-m4ioak3P97fzxh2Dovz8BnHsneA=", + "requires": { + "lodash._renative": "~2.3.0", + "lodash.isobject": "~2.3.0", + "lodash.noop": "~2.3.0" + } + }, + "lodash._basecreatecallback": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash._basecreatecallback/-/lodash._basecreatecallback-2.3.0.tgz", + "integrity": "sha1-N7KrF1kaM56YjbMln81GAZ16w2I=", + "requires": { + "lodash._setbinddata": "~2.3.0", + "lodash.bind": "~2.3.0", + "lodash.identity": "~2.3.0", + "lodash.support": "~2.3.0" + } + }, + "lodash._basecreatewrapper": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash._basecreatewrapper/-/lodash._basecreatewrapper-2.3.0.tgz", + "integrity": "sha1-qgxhrZYETDkzN2ExSDqXWcNlEkc=", + "requires": { + "lodash._basecreate": "~2.3.0", + "lodash._setbinddata": "~2.3.0", + "lodash._slice": "~2.3.0", + "lodash.isobject": "~2.3.0" + } + }, + "lodash._createwrapper": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash._createwrapper/-/lodash._createwrapper-2.3.0.tgz", + "integrity": "sha1-0arhEC2t9EDo4G/BM6bt1/4UYHU=", + "requires": { + "lodash._basebind": "~2.3.0", + "lodash._basecreatewrapper": "~2.3.0", + "lodash.isfunction": "~2.3.0" + } + }, + "lodash._objecttypes": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash._objecttypes/-/lodash._objecttypes-2.3.0.tgz", + "integrity": "sha1-aj6jmH3W7rgCGy1cnDA1Scwrrh4=" + }, + "lodash._renative": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash._renative/-/lodash._renative-2.3.0.tgz", + "integrity": "sha1-d9jt1M7SbdWXH54Vpfdy5OMX+9M=" + }, + "lodash._setbinddata": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash._setbinddata/-/lodash._setbinddata-2.3.0.tgz", + "integrity": "sha1-5WEEkKzRMnfVmFjZW18nJ/FQjwQ=", + "requires": { + "lodash._renative": "~2.3.0", + "lodash.noop": "~2.3.0" + } + }, + "lodash._shimkeys": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash._shimkeys/-/lodash._shimkeys-2.3.0.tgz", + "integrity": "sha1-YR+TFJ4+bHIQlrSHae8pU3rai6k=", + "requires": { + "lodash._objecttypes": "~2.3.0" + } + }, + "lodash._slice": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash._slice/-/lodash._slice-2.3.0.tgz", + "integrity": "sha1-FHGYEyhZly5GgMoppZkshVZpqlw=" + }, + "lodash.bind": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash.bind/-/lodash.bind-2.3.0.tgz", + "integrity": "sha1-wqjhi2jl7MFS4rFoJmEW/qWwFsw=", + "requires": { + "lodash._createwrapper": "~2.3.0", + "lodash._renative": "~2.3.0", + "lodash._slice": "~2.3.0" + } + }, + "lodash.foreach": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash.foreach/-/lodash.foreach-2.3.0.tgz", + "integrity": "sha1-CDQEyR6EbudyRf3512UZxosq8Wg=", + "requires": { + "lodash._basecreatecallback": "~2.3.0", + "lodash.forown": "~2.3.0" + } + }, + "lodash.forown": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash.forown/-/lodash.forown-2.3.0.tgz", + "integrity": "sha1-JPtKr4ANRfwtxgv+w84EyDajrX8=", + "requires": { + "lodash._basecreatecallback": "~2.3.0", + "lodash._objecttypes": "~2.3.0", + "lodash.keys": "~2.3.0" + } + }, + "lodash.identity": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash.identity/-/lodash.identity-2.3.0.tgz", + "integrity": "sha1-awGiEMlIU1XCqRO0i2cRIZoXPe0=" + }, + "lodash.isfunction": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash.isfunction/-/lodash.isfunction-2.3.0.tgz", + "integrity": "sha1-aylz5HpkfPEucNZ2rqE2Q3BuUmc=" + }, + "lodash.isobject": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash.isobject/-/lodash.isobject-2.3.0.tgz", + "integrity": "sha1-LhbT/Fg9qYMZaJU/LY5tc0NPZ5k=", + "requires": { + "lodash._objecttypes": "~2.3.0" + } + }, + "lodash.keys": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash.keys/-/lodash.keys-2.3.0.tgz", + "integrity": "sha1-s1D0+Syqn0WkouzwGEVM8vKK4lM=", + "requires": { + "lodash._renative": "~2.3.0", + "lodash._shimkeys": "~2.3.0", + "lodash.isobject": "~2.3.0" + } + }, "lodash.memoize": { "version": "4.1.2", "resolved": "https://registry.npmjs.org/lodash.memoize/-/lodash.memoize-4.1.2.tgz", "integrity": "sha1-vMbEmkKihA7Zl/Mj6tpezRguC/4=", "dev": true }, + "lodash.noop": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash.noop/-/lodash.noop-2.3.0.tgz", + "integrity": "sha1-MFnWKNUbv5N80qC2/Dp/ISpmnCw=" + }, "lodash.sortby": { "version": "4.7.0", "resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz", "integrity": "sha1-7dFMgk4sycHgsKG0K7UhBRakJDg=", "dev": true }, + "lodash.support": { + "version": "2.3.0", + "resolved": "https://registry.npmjs.org/lodash.support/-/lodash.support-2.3.0.tgz", + "integrity": "sha1-fq8DivTw1qq3drRKptz8gDNMm/0=", + "requires": { + "lodash._renative": "~2.3.0" + } + }, "lru-cache": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/lru-cache/-/lru-cache-6.0.0.tgz", diff --git a/package.json b/package.json index 8e226ba..ba4fbb9 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ "@skyra/decorators": "^3.0.0", "axios": "^0.21.1", "common-tags": "^1.8.0", + "convert-units": "^2.3.4", "discord.js": "^12.5.1", "genius-lyrics": "^4.0.0", "klasa": "git://github.com/tuataria/klasa.git#725632bfcfc135bc49f5b102b67c726dac72d015", @@ -48,6 +49,7 @@ "@babel/preset-env": "^7.12.11", "@types/backoff": "^2.5.1", "@types/common-tags": "^1.8.0", + "@types/convert-units": "^2.3.3", "@types/jest": "^26.0.20", "@types/mongodb": "^3.3.16", "@types/node-fetch": "^2.5.4", diff --git a/src/commands/Unit Conversions/tempconvert.ts b/src/commands/Unit Conversions/tempconvert.ts new file mode 100644 index 0000000..a377fba --- /dev/null +++ b/src/commands/Unit Conversions/tempconvert.ts @@ -0,0 +1,49 @@ +import { SteveCommand } from '@lib/structures/commands/SteveCommand'; +import { ApplyOptions, CreateResolvers } from '@skyra/decorators'; +import { MessageEmbed } from 'discord.js'; +import { CommandOptions, KlasaMessage } from 'klasa'; +// eslint-disable-next-line @typescript-eslint/no-var-requires +const convert = require('convert-units'); + +@ApplyOptions({ + aliases: ['temp'], + description: lang => lang.tget('commandTempConvertDescription'), + extendedHelp: lang => lang.tget('commandTempConvertExtended'), + usage: ' ' +}) +@CreateResolvers([ + [ + 'unit', + (str, possible, msg) => { + str = str.toUpperCase(); + + if (['C', 'F', 'K', 'R'].includes(str)) return str; + if (str === 'CELSIUS' || str === 'FAHRENHEIT' || str === 'KELVIN' || str === 'RANKINE') return str[1]; + + throw msg.language.tget('commandTempConvertInvalidUnit', str); + } + ] +]) +export default class extends SteveCommand { + + public async run(msg: KlasaMessage, [num, unitConvertingFrom, unitConvertingTo]: [number, temperatureUnit, temperatureUnit]) { + // eslint-disable-next-line newline-per-chained-call + const convertedValue = Number(convert(num).from(unitConvertingFrom).to(unitConvertingTo).toFixed(2)); + + const embed = new MessageEmbed() + .addFields( + { name: this.getFullUnitName(unitConvertingFrom), value: num, inline: true }, + { name: this.getFullUnitName(unitConvertingTo), value: convertedValue, inline: true } + ) + .setColor(0x71adcf); + + return msg.channel.send(embed); + } + + private getFullUnitName(unit: temperatureUnit) { + return convert().describe(unit).singular.split(' ')[1]; + } + +} + +type temperatureUnit = 'C' | 'F' | 'K' | 'R'; diff --git a/src/languages/en-US.ts b/src/languages/en-US.ts index 7026a15..ac5ab28 100644 --- a/src/languages/en-US.ts +++ b/src/languages/en-US.ts @@ -1135,6 +1135,22 @@ export default class extends Language { commandFeedbackNoChannel: 'The specified feedback channel for this bot does not exist; contact a bot owner.', commandFeedbackSent: 'Your feedback has been sent, thanks!', commandSupportDescription: `Get a link to ${botName}'s support server.`, + /** + * ################################ + * # UNIT CONVERSION COMMANDS # + * ################################ + */ + commandTempConvertDescription: 'Easily convert temperatures', + commandTempConvertExtended: builder.display('tempconvert', { + examples: [ + '32|f|c', + '0|celsius|fahrenheit' + ], + explainedUsage: [ + ['unit', 'Supported units: Celsius (C), Fahrenheit (F), Kelvin (K), Rankine (R)'] + ] + }), + commandTempConvertInvalidUnit: unit => `**${unit}** is not a valid/supported unit.`, /** * ################################ * # LOG EVENT TOGGLE COMMANDS # diff --git a/src/lib/types/Languages.d.ts b/src/lib/types/Languages.d.ts index 78c7c4a..2324e69 100644 --- a/src/lib/types/Languages.d.ts +++ b/src/lib/types/Languages.d.ts @@ -458,6 +458,9 @@ declare module 'klasa' { commandFeedbackNoChannel: string; commandFeedbackSent: string; commandSupportDescription: string; + commandTempConvertDescription: string; + commandTempConvertExtended: string; + commandTempConvertInvalidUnit: (unit: string) => string; commandToggleChannelCreateDescription: string; commandToggleChannelCreate: (disabled: boolean) => string; commandToggleChannelDeleteDescription: string;