Skip to content

Commit 04ce825

Browse files
committed
feat(bots/discord): switch duration parser to @sapphire/duration
1 parent 27e06db commit 04ce825

File tree

3 files changed

+41
-26
lines changed

3 files changed

+41
-26
lines changed

bots/discord/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@
3232
"@discordjs/rest": "^2.4.3",
3333
"@revanced/bot-api": "workspace:*",
3434
"@revanced/bot-shared": "workspace:*",
35+
"@sapphire/duration": "^1.2.0",
3536
"chalk": "^5.4.1",
3637
"decancer": "^3.2.8",
3738
"discord.js": "^14.18.0",
38-
"drizzle-orm": "^0.31.4",
39-
"parse-duration": "^1.1.2"
39+
"drizzle-orm": "^0.31.4"
4040
},
4141
"devDependencies": {
4242
"@libsql/client": "^0.7.0",
4343
"discord-api-types": "^0.37.119",
4444
"drizzle-kit": "^0.22.8"
4545
}
46-
}
46+
}

bots/discord/src/utils/duration.ts

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,40 @@
1-
import parse from 'parse-duration'
1+
import { Duration, DurationFormatter } from '@sapphire/duration'
22

3-
parse[''] = parse['s']!
4-
parse['mo'] = parse['M'] = parse['month']!
3+
const fmt = new DurationFormatter({
4+
year: {
5+
DEFAULT: 'y',
6+
},
7+
month: {
8+
DEFAULT: 'M',
9+
},
10+
week: {
11+
DEFAULT: 'w',
12+
},
13+
day: {
14+
DEFAULT: 'd',
15+
},
16+
hour: {
17+
DEFAULT: 'h',
18+
},
19+
minute: {
20+
DEFAULT: 'm',
21+
},
22+
second: {
23+
DEFAULT: 's',
24+
},
25+
})
526

6-
export const parseDuration = (duration: string, defaultUnit?: parse.Units) => {
7-
const defaultUnitValue = parse['']!
8-
if (defaultUnit) parse[''] = parse[defaultUnit]!
9-
const result = parse(duration, 'ms') ?? Number.NaN
10-
parse[''] = defaultUnitValue
11-
return result
27+
export const parseDuration = (duration: string, defaultUnit = 's') => {
28+
// adds default unit to the end of the string if it doesn't have a unit
29+
// 100 -> 100s
30+
// 10m100 -> 10m100s
31+
// biome-ignore lint/style/noParameterAssign: this is fine
32+
if (/\d$/.test(duration)) duration += defaultUnit
33+
return new Duration(duration).offset
1234
}
1335

1436
export const durationToString = (duration: number) => {
15-
if (duration === 0) return '0s'
16-
17-
const days = Math.floor(duration / (24 * 60 * 60 * 1000))
18-
const hours = Math.floor((duration % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000))
19-
const minutes = Math.floor((duration % (60 * 60 * 1000)) / (60 * 1000))
20-
const seconds = Math.floor((duration % (60 * 1000)) / 1000)
21-
22-
return `${days ? `${days}d` : ''}${hours ? `${hours}h` : ''}${minutes ? `${minutes}m` : ''}${
23-
seconds ? `${seconds}s` : ''
24-
}`
37+
return fmt.format(duration, undefined, {
38+
left: '',
39+
})
2540
}

bun.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)