diff --git a/src/lib/command/middleware/Middleware.ts b/src/lib/command/middleware/Middleware.ts index 9c148a44..bdb46202 100644 --- a/src/lib/command/middleware/Middleware.ts +++ b/src/lib/command/middleware/Middleware.ts @@ -18,7 +18,7 @@ export class Middleware * any invalid input.

* * Valid types are:
- *
'String' | 'Number' | 'User' | 'Member' | 'BannedUser' | 'Role' | 'Channel'

+ *
'String' | 'Number' | 'Duration' | 'User' | 'Member' | 'BannedUser' | 'Role' | 'Channel'

* * Example:
*
{ 'mem': 'Member', 'age>': 'Number', '...desc': 'String' }
@@ -52,7 +52,7 @@ export class Middleware
 	 * 

* * If verifying a `BannedUser` returned from the ResolveArgs middleware, - * use the `User` type. + * use the `User` type. If verifying `Duration` type, `Number` should be used. * * This middleware does not modify args in any way. * @name expect diff --git a/src/lib/command/middleware/ResolveArgs.ts b/src/lib/command/middleware/ResolveArgs.ts index 88bf6da6..5eb8493f 100644 --- a/src/lib/command/middleware/ResolveArgs.ts +++ b/src/lib/command/middleware/ResolveArgs.ts @@ -48,6 +48,25 @@ export function resolveArgs>(argTypes: { [na `in arg \`${name}\`: \`${arg}\` could not be resolved to a number.\n${usage}`); } + else if (type === 'Duration') + { + let duration: number, match: RegExpMatchArray; + if (/^(?:\d+(?:\.\d+)?)[s|m|h|d]$/.test(arg)) + { + match = arg.match(/(\d+(?:\.\d+)?)(s|m|h|d)$/); + duration = parseFloat(match[1]); + duration = match[2] === 's' + ? duration * 1000 : match[2] === 'm' + ? duration * 1000 * 60 : match[2] === 'h' + ? duration * 1000 * 60 * 60 : match[2] === 'd' + ? duration * 1000 * 60 * 60 * 24 : null; + } + if (!duration) throw new Error( + `in arg \`${name}\`: \`${arg}\` could not be resolved to a duration.\n${usage}`); + + args[index] = duration; + } + else if (type === 'User') { let user: User; diff --git a/src/lib/types/ResolveArgType.ts b/src/lib/types/ResolveArgType.ts index 215bd644..be7adfac 100644 --- a/src/lib/types/ResolveArgType.ts +++ b/src/lib/types/ResolveArgType.ts @@ -1,7 +1,7 @@ -export type ResolveArgType = 'String' | 'Number' | 'User' | 'Member' | 'BannedUser' | 'Channel' | 'Role'; +export type ResolveArgType = 'String' | 'Number' | 'Duration' | 'User' | 'Member' | 'BannedUser' | 'Channel' | 'Role'; /** * @typedef {string} ResolveArgType Valid arg type values for the ResolveArgs middleware * Can be one of the following string literals:
- *
'String' | 'Number' | 'User' | 'Member' | 'BannedUser' | 'Role' | 'Channel'
+ *
'String' | 'Number' | 'Duration' | 'User' | 'Member' | 'BannedUser' | 'Role' | 'Channel'
*/