Skip to content
This repository has been archived by the owner on Dec 24, 2021. It is now read-only.

Added duration argument type. #366

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/types/duration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const ArgumentType = require('./base');

class DurationArgumentType extends ArgumentType {
constructor(client) {
super(client, 'duration');
}

validate(val, msg, arg) {
if(val.includes('.')) return false;
// No decimals allowed!!!

const millis = timeStrToMillis(val);
if(millis > Number.MAX_SAFE_INTEGER || millis < 5000) {
arg.error = 'Time must be greater than or equal to 5 seconds.';
return false;
}

return val.split(' ').reduce((acc, timePart) => acc && !!timePart.match(/\d+(?=w|d|h|m|s)/i), true);
}

parse(val) {
return timeStrToMillis(val);
}
}

function timeStrToMillis(str) {
const weeks = +str.match(/\d+(?=w)/i);
const days = +str.match(/\d+(?=d)/i);
const hours = +str.match(/\d+(?=h)/i);
const minutes = +str.match(/\d+(?=m)/i);
const seconds = +str.match(/\d+(?=s)/i);

const millis = (weeks * 7 * 24 * 60 * 60 * 1000) +
(days * 24 * 60 * 60 * 1000) +
(hours * 60 * 60 * 1000) +
(minutes * 60 * 1000) +
(seconds * 1000);

return millis;
}

module.exports = DurationArgumentType;
32 changes: 32 additions & 0 deletions test/commands/util/wait-send-message.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const commando = require('../../../src');
const { promisify } = require('util');

module.exports = class WaitSendMessage extends commando.Command {
constructor(client) {
super(client, {
name: 'waitsendmessage',
aliases: ['wsm', 'wait'],
group: 'util',
memberName: 'waitsendmessage',
description: 'Send a message. Wait an amount of time, then edit it.',
examples: ['waitsendmessage 5m', 'wsm 10h'],
args: [
{
key: 'duration',
prompt: 'How long would you like the message to be edited after being sent?',
type: 'duration'
}
]
});
}

async run(msg, { duration }) {
// Send a message
const sentMessage = await msg.channel.send(`Waiting ${duration} ms to edit...`);
// Wait for the duration
const sleep = promisify(setTimeout);
await sleep(duration);
// Edit the message
return sentMessage.edit(`Successfully waited ${duration} ms!`);
}
};
42 changes: 42 additions & 0 deletions test/types/duration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const commando = require('../../src');

class DurationArgumentType extends commando.ArgumentType {
constructor(client) {
super(client, 'duration');
}

validate(val, msg, arg) {
if(val.includes('.')) return false;
// No decimals allowed!!!

const millis = timeStrToMillis(val);
if(millis > Number.MAX_SAFE_INTEGER || millis < 5000) {
arg.error = 'Time must be greater than or equal to 5 seconds.';
return false;
}

return val.split(' ').reduce((acc, timePart) => acc && !!timePart.match(/\d+(?=w|d|h|m|s)/i), true);
}

parse(val) {
return timeStrToMillis(val);
}
}

function timeStrToMillis(str) {
const weeks = +str.match(/\d+(?=w)/i);
const days = +str.match(/\d+(?=d)/i);
const hours = +str.match(/\d+(?=h)/i);
const minutes = +str.match(/\d+(?=m)/i);
const seconds = +str.match(/\d+(?=s)/i);

const millis = (weeks * 7 * 24 * 60 * 60 * 1000) +
(days * 24 * 60 * 60 * 1000) +
(hours * 60 * 60 * 1000) +
(minutes * 60 * 1000) +
(seconds * 1000);

return millis;
}

module.exports = DurationArgumentType;