diff options
author | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-06-24 00:56:16 -0400 |
---|---|---|
committer | IRONM00N <64110067+IRONM00N@users.noreply.github.com> | 2021-06-24 00:56:16 -0400 |
commit | 4176b6258e44e4a095376aaf0f4c687244243a69 (patch) | |
tree | 3b9144be9a2045483c90d92fff05b3ca0b288e52 /src/arguments | |
parent | e80446e23060c0325bbd6db620920d86694ec3ce (diff) | |
download | tanzanite-4176b6258e44e4a095376aaf0f4c687244243a69.tar.gz tanzanite-4176b6258e44e4a095376aaf0f4c687244243a69.tar.bz2 tanzanite-4176b6258e44e4a095376aaf0f4c687244243a69.zip |
feat(*): Began working on other punishment commands etc
Diffstat (limited to 'src/arguments')
-rw-r--r-- | src/arguments/duration.ts | 55 |
1 files changed, 55 insertions, 0 deletions
diff --git a/src/arguments/duration.ts b/src/arguments/duration.ts new file mode 100644 index 0000000..a2f7751 --- /dev/null +++ b/src/arguments/duration.ts @@ -0,0 +1,55 @@ +import { BushArgumentTypeCaster } from '../lib/extensions/BushArgumentTypeCaster'; +import { BushMessage } from '../lib/extensions/BushMessage'; + +// Stolen from @Mzato0001 (pr to discord akairo that hasn't been merged yet) +const TimeUnits = { + years: { + label: '(?:years?|y)', + value: 1000 * 60 * 60 * 24 * 365 + }, + months: { + label: '(?:months?|mo)', + value: 1000 * 60 * 60 * 24 * 30 + }, + weeks: { + label: '(?:weeks?|w)', + value: 1000 * 60 * 60 * 24 * 7 + }, + days: { + label: '(?:days?|d)', + value: 1000 * 60 * 60 * 24 + }, + hours: { + label: '(?:hours?|hrs?|h)', + value: 1000 * 60 * 60 + }, + minutes: { + label: '(?:minutes?|mins?|m)', + value: 1000 * 60 + }, + seconds: { + label: '(?:seconds?|secs?|s)', + value: 1000 + }, + milliseconds: { + label: '(?:milliseconds?|msecs?|ms)', + value: 1 + } +}; +export const durationTypeCaster: BushArgumentTypeCaster = async (_message: BushMessage, phrase): Promise<number> => { + if (!phrase) return null; + + const regexString = Object.entries(TimeUnits) + .map(([name, { label }]) => String.raw`(?:(?<${name}>-?(?:\d+)?\.?\d+) *${label})?`) + .join('\\s*'); + const match = new RegExp(`^${regexString}$`, 'i').exec(phrase); + if (!match) return null; + + let milliseconds = 0; + for (const key in match.groups) { + const value = Number(match.groups[key] || 0); + milliseconds += value * TimeUnits[key].value; + } + + return milliseconds; +}; |