Skip to content

Commit

Permalink
feat(tempconvert): add tempconvert command
Browse files Browse the repository at this point in the history
  • Loading branch information
jwford committed Feb 25, 2021
1 parent a90e139 commit 1f2ad31
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 0 deletions.
169 changes: 169 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
49 changes: 49 additions & 0 deletions src/commands/Unit Conversions/tempconvert.ts
Original file line number Diff line number Diff line change
@@ -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<CommandOptions>({
aliases: ['temp'],
description: lang => lang.tget('commandTempConvertDescription'),
extendedHelp: lang => lang.tget('commandTempConvertExtended'),
usage: '<number:number> <firstUnit:unit> <secondUnit:unit>'
})
@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';
16 changes: 16 additions & 0 deletions src/languages/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 #
Expand Down
3 changes: 3 additions & 0 deletions src/lib/types/Languages.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 1f2ad31

Please sign in to comment.