aboutsummaryrefslogtreecommitdiff
path: root/src/arguments/duration.ts
blob: a2f7751b126dfe36ceb8edbc1ee76edf3c1cbf1a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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;
};