fix(bots/discord): replace duration parser with a library

This commit is contained in:
PalmDevs
2024-08-23 17:36:32 +07:00
parent e3dcbab508
commit 94c4fedc06
3 changed files with 4 additions and 15 deletions

View File

@@ -35,7 +35,8 @@
"chalk": "^5.3.0",
"decancer": "^3.2.3",
"discord.js": "^14.15.3",
"drizzle-orm": "^0.31.4"
"drizzle-orm": "^0.31.4",
"parse-duration": "^1.1.0"
},
"devDependencies": {
"@libsql/client": "^0.7.0",

View File

@@ -1,18 +1,6 @@
export const parseDuration = (duration: string) => {
if (!duration.length) return Number.NaN
const matches = duration.match(/(?:(\d+y)?(\d+M)?(\d+w)?(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s?)/)!
import parse from 'parse-duration'
const [, years, months, weeks, days, hours, minutes, seconds] = matches.map(Number)
return (
(years || 0) * 290304e5 +
(months || 0) * 24192e5 +
(weeks || 0) * 6048e5 +
(days || 0) * 864e5 +
(hours || 0) * 36e5 +
(minutes || 0) * 6e4 +
(seconds || 0) * 1e3
)
}
export const parseDuration = (duration: string) => parse(duration, 'ms') ?? Number.NaN
export const durationToString = (duration: number) => {
if (duration === 0) return '0s'

BIN
bun.lockb

Binary file not shown.