Commando icon indicating copy to clipboard operation
Commando copied to clipboard

Argument type: duration

Open JohnScience opened this issue 6 years ago • 11 comments

The most of Discord bots are expected to provide "mute" and "ban" commands. Their implementation involves juggling with parsing strings just to get so-much-desired duration.

Formats:

"3w 1d 11 [many spaces]h \t30m 20s 5ms"

Methods:

  • duration.toWeeks() -> float
  • duration.toDays() -> float
  • duration.toMinutes() -> float
  • duration.toSeconds() -> float
  • duration.toMilliseconds() -> float

And perhaps a few aliases:

  • duration.toW()
  • duration.toD()
  • duration.toM()
  • duration.toMs()

etc

JohnScience avatar Aug 10 '18 13:08 JohnScience

This would make my bot so much cleaner haha!

greavettey avatar Aug 26 '18 11:08 greavettey

Would Commando handle parsing a duration itself or use an external package like moment.js or date-fns to handle it?

jonahsnider avatar Dec 11 '18 04:12 jonahsnider

It should parse the duration itself. Cutting down dependencies is usually welcome.

Edit: I've written some tentative regex (I wrote it on mobile, on a bus). It would look somewhat like this:

/(\d+)\s*([A-Za-z]+)/g

Of course, I don't know how allowing the duration to have whitespace in it would effect arguments after it. We may need to disallow whitespace unless in quotes or brackets or something. Maybe some better regex would solve this problem.

On Mon, Dec 10, 2018, 11:42 PM Jonah Snider <[email protected] wrote:

Would Commando handle parsing a duration itself or use an external package like moment.js or date-fns to handle it?

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/discordjs/Commando/issues/182#issuecomment-446070397, or mute the thread https://github.com/notifications/unsubscribe-auth/AiKPagU7_swl7tQjMAPp56NdB8yU_Pxzks5u3ze_gaJpZM4V4Ls9 .

brandonbothell avatar Dec 11 '18 11:12 brandonbothell

I'm not opposed to using moment, since it covers a lot of possible input formats, and handles localisation. The only problem is it doesn't take the "6mo3d4h50m30s" format... which is something that, ideally, would also accept locale-specific suffixes.

Gawdl3y avatar Dec 11 '18 14:12 Gawdl3y

Hi, I am working on a solution to this but I am having difficulty testing it locally. I'm currently attempting to run the test bot included but receiving this error:

 $ node test/bot.js
/Users/madlab/repos/temp/Commando/src/extensions/message.js:7
module.exports = Structures.extend('Message', Message => {
                            ^

TypeError: Cannot read property 'extend' of undefined
    at Object.<anonymous> (/Users/madlab/repos/temp/Commando/src/extensions/message.js:7:29)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/madlab/repos/temp/Commando/src/registry.js:5:25)
    at Module._compile (module.js:652:30)

This is after a npm install and npm install discord.js. I ran into the same errors with npm link to my own bot locally.

NVM I found #215 and figured it out.

MADLAB96 avatar Mar 28 '19 19:03 MADLAB96

Okay I have this mostly done but I can't seem to get the methods working. Just adding them to my DurationArgumentType class doesn't do the trick. I also didn't find any examples of class methods with the rest of the types.

MADLAB96 avatar Mar 28 '19 21:03 MADLAB96

This is the regex I'm using to parse duration in one of my bots, not sure if it's any help to anybody:

^(?:(?<weeks>\d+)w)?(?:((?<days>\d+)d)?(?:(?<hours>\d+)h)?(?:(?<minutes>\d+)m)?)$

E.g. 2w3d4h15m where any part is optional

Jazcash avatar Apr 03 '19 10:04 Jazcash

I'm not opposed to using moment, since it covers a lot of possible input formats, and handles localisation. The only problem is it doesn't take the "6mo3d4h50m30s" format... which is something that, ideally, would also accept locale-specific suffixes.

Moment supports plugins which you can use to define new formats and date-fns lets you define a pattern for parsing dates.

jonahsnider avatar Sep 06 '19 17:09 jonahsnider

I'm guessing this was dropped, by people who tried to make it work? If so, I would like to give it a shot, and see what I can come up with. https://github.com/discordjs/Commando/pull/256 (Was this ever implemented?)

MagnusHJensen avatar Dec 23 '20 10:12 MagnusHJensen

I've given this a shot using code from my own bot (#366). I think this would make a great addition to Commando and would love to see any of the suggested implementations merged!

sriRacha21 avatar Jan 29 '21 10:01 sriRacha21

I have the same errorhttps://prnt.sc/1tkskl4

Nik0lovD avatar Sep 25 '21 11:09 Nik0lovD