From f4f9b91bf7c0399d675c97a4153c06db3b45c810 Mon Sep 17 00:00:00 2001 From: Arjun Srivastav Date: Fri, 29 Jan 2021 05:39:07 -0500 Subject: [PATCH] Added duration argument type. --- src/types/duration.js | 42 +++++++++++++++++++++++++ test/commands/util/wait-send-message.js | 32 +++++++++++++++++++ test/types/duration.js | 42 +++++++++++++++++++++++++ 3 files changed, 116 insertions(+) create mode 100644 src/types/duration.js create mode 100644 test/commands/util/wait-send-message.js create mode 100644 test/types/duration.js diff --git a/src/types/duration.js b/src/types/duration.js new file mode 100644 index 000000000..4e5a628bf --- /dev/null +++ b/src/types/duration.js @@ -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; diff --git a/test/commands/util/wait-send-message.js b/test/commands/util/wait-send-message.js new file mode 100644 index 000000000..7192e64e7 --- /dev/null +++ b/test/commands/util/wait-send-message.js @@ -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!`); + } +}; diff --git a/test/types/duration.js b/test/types/duration.js new file mode 100644 index 000000000..1f663fb16 --- /dev/null +++ b/test/types/duration.js @@ -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;