Skip to content

Commit

Permalink
Add Duration type to resolveArgs middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
zajrik committed Mar 9, 2017
1 parent e16f296 commit e09431d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/lib/command/middleware/Middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class Middleware
* any invalid input.<br><br>
*
* Valid types are:<br>
* <pre class="prettyprint"><code>'String' | 'Number' | 'User' | 'Member' | 'BannedUser' | 'Role' | 'Channel'</code></pre><br>
* <pre class="prettyprint"><code>'String' | 'Number' | 'Duration' | 'User' | 'Member' | 'BannedUser' | 'Role' | 'Channel'</code></pre><br>
*
* Example:<br>
* <pre class="prettyprint"><code>{ 'mem': 'Member', 'age>': 'Number', '...desc': 'String' }
Expand Down Expand Up @@ -52,7 +52,7 @@ export class Middleware
* </code></pre><br>
*
* 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
Expand Down
19 changes: 19 additions & 0 deletions src/lib/command/middleware/ResolveArgs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,25 @@ export function resolveArgs<T extends Bot, U extends Command<T>>(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;
Expand Down
4 changes: 2 additions & 2 deletions src/lib/types/ResolveArgType.ts
Original file line number Diff line number Diff line change
@@ -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:<br>
* <pre class="prettyprint"><code>'String' | 'Number' | 'User' | 'Member' | 'BannedUser' | 'Role' | 'Channel'</code></pre>
* <pre class="prettyprint"><code>'String' | 'Number' | 'Duration' | 'User' | 'Member' | 'BannedUser' | 'Role' | 'Channel'</code></pre>
*/

0 comments on commit e09431d

Please sign in to comment.