mirror of
https://github.com/ReVanced/revanced-bots.git
synced 2026-01-11 13:56:15 +00:00
feat(bots/discord): switch duration parser to @sapphire/duration
This commit is contained in:
@@ -32,11 +32,11 @@
|
||||
"@discordjs/rest": "^2.4.3",
|
||||
"@revanced/bot-api": "workspace:*",
|
||||
"@revanced/bot-shared": "workspace:*",
|
||||
"@sapphire/duration": "^1.2.0",
|
||||
"chalk": "^5.4.1",
|
||||
"decancer": "^3.2.8",
|
||||
"discord.js": "^14.18.0",
|
||||
"drizzle-orm": "^0.31.4",
|
||||
"parse-duration": "^1.1.2"
|
||||
"drizzle-orm": "^0.31.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@libsql/client": "^0.7.0",
|
||||
|
||||
@@ -1,25 +1,40 @@
|
||||
import parse from 'parse-duration'
|
||||
import { Duration, DurationFormatter } from '@sapphire/duration'
|
||||
|
||||
parse[''] = parse['s']!
|
||||
parse['mo'] = parse['M'] = parse['month']!
|
||||
const fmt = new DurationFormatter({
|
||||
year: {
|
||||
DEFAULT: 'y',
|
||||
},
|
||||
month: {
|
||||
DEFAULT: 'M',
|
||||
},
|
||||
week: {
|
||||
DEFAULT: 'w',
|
||||
},
|
||||
day: {
|
||||
DEFAULT: 'd',
|
||||
},
|
||||
hour: {
|
||||
DEFAULT: 'h',
|
||||
},
|
||||
minute: {
|
||||
DEFAULT: 'm',
|
||||
},
|
||||
second: {
|
||||
DEFAULT: 's',
|
||||
},
|
||||
})
|
||||
|
||||
export const parseDuration = (duration: string, defaultUnit?: parse.Units) => {
|
||||
const defaultUnitValue = parse['']!
|
||||
if (defaultUnit) parse[''] = parse[defaultUnit]!
|
||||
const result = parse(duration, 'ms') ?? Number.NaN
|
||||
parse[''] = defaultUnitValue
|
||||
return result
|
||||
export const parseDuration = (duration: string, defaultUnit = 's') => {
|
||||
// adds default unit to the end of the string if it doesn't have a unit
|
||||
// 100 -> 100s
|
||||
// 10m100 -> 10m100s
|
||||
// biome-ignore lint/style/noParameterAssign: this is fine
|
||||
if (/\d$/.test(duration)) duration += defaultUnit
|
||||
return new Duration(duration).offset
|
||||
}
|
||||
|
||||
export const durationToString = (duration: number) => {
|
||||
if (duration === 0) return '0s'
|
||||
|
||||
const days = Math.floor(duration / (24 * 60 * 60 * 1000))
|
||||
const hours = Math.floor((duration % (24 * 60 * 60 * 1000)) / (60 * 60 * 1000))
|
||||
const minutes = Math.floor((duration % (60 * 60 * 1000)) / (60 * 1000))
|
||||
const seconds = Math.floor((duration % (60 * 1000)) / 1000)
|
||||
|
||||
return `${days ? `${days}d` : ''}${hours ? `${hours}h` : ''}${minutes ? `${minutes}m` : ''}${
|
||||
seconds ? `${seconds}s` : ''
|
||||
}`
|
||||
return fmt.format(duration, undefined, {
|
||||
left: '',
|
||||
})
|
||||
}
|
||||
|
||||
8
bun.lock
8
bun.lock
@@ -40,17 +40,17 @@
|
||||
},
|
||||
"bots/discord": {
|
||||
"name": "@revanced/discord-bot",
|
||||
"version": "1.1.0",
|
||||
"version": "1.1.1",
|
||||
"dependencies": {
|
||||
"@discordjs/builders": "^1.10.1",
|
||||
"@discordjs/rest": "^2.4.3",
|
||||
"@revanced/bot-api": "workspace:*",
|
||||
"@revanced/bot-shared": "workspace:*",
|
||||
"@sapphire/duration": "^1.2.0",
|
||||
"chalk": "^5.4.1",
|
||||
"decancer": "^3.2.8",
|
||||
"discord.js": "^14.18.0",
|
||||
"drizzle-orm": "^0.31.4",
|
||||
"parse-duration": "^1.1.2",
|
||||
},
|
||||
"devDependencies": {
|
||||
"@libsql/client": "^0.7.0",
|
||||
@@ -337,6 +337,8 @@
|
||||
|
||||
"@sapphire/async-queue": ["@sapphire/async-queue@1.5.5", "", {}, "sha512-cvGzxbba6sav2zZkH8GPf2oGk9yYoD5qrNWdu9fRehifgnFZJMV+nuy2nON2roRO4yQQ+v7MK/Pktl/HgfsUXg=="],
|
||||
|
||||
"@sapphire/duration": ["@sapphire/duration@1.2.0", "", {}, "sha512-LxjOAFXz81WmrI8XX9YaVcAZDjQj/1p78lZCvkAWZB1nphOwz/D0dU3CBejmhOWx5dO5CszTkLJMNR0xuCK+Zg=="],
|
||||
|
||||
"@sapphire/shapeshift": ["@sapphire/shapeshift@4.0.0", "", { "dependencies": { "fast-deep-equal": "^3.1.3", "lodash": "^4.17.21" } }, "sha512-d9dUmWVA7MMiKobL3VpLF8P2aeanRTu6ypG2OIaEv/ZHH/SUQ2iHOVyi5wAPjQ+HmnMuL0whK9ez8I/raWbtIg=="],
|
||||
|
||||
"@sapphire/snowflake": ["@sapphire/snowflake@3.5.3", "", {}, "sha512-jjmJywLAFoWeBi1W7994zZyiNWPIiqRRNAmSERxyg93xRGzNYvGjlZ0gR6x0F4gPRi2+0O6S71kOZYyr3cxaIQ=="],
|
||||
@@ -981,8 +983,6 @@
|
||||
|
||||
"parse-conflict-json": ["parse-conflict-json@3.0.1", "", { "dependencies": { "json-parse-even-better-errors": "^3.0.0", "just-diff": "^6.0.0", "just-diff-apply": "^5.2.0" } }, "sha512-01TvEktc68vwbJOtWZluyWeVGWjP+bZwXtPDMQVbBKzbJ/vZBif0L69KH1+cHv1SZ6e0FKLvjyHe8mqsIqYOmw=="],
|
||||
|
||||
"parse-duration": ["parse-duration@1.1.2", "", {}, "sha512-p8EIONG8L0u7f8GFgfVlL4n8rnChTt8O5FSxgxMz2tjc9FMP199wxVKVB6IbKx11uTbKHACSvaLVIKNnoeNR/A=="],
|
||||
|
||||
"parse-json": ["parse-json@5.2.0", "", { "dependencies": { "@babel/code-frame": "^7.0.0", "error-ex": "^1.3.1", "json-parse-even-better-errors": "^2.3.0", "lines-and-columns": "^1.1.6" } }, "sha512-ayCKvm/phCGxOkYRSCM82iDwct8/EonSEgCSxWxD7ve6jHggsFl4fZVQBPRNgQoKiuV/odhFrGzQXZwbifC8Rg=="],
|
||||
|
||||
"parse-ms": ["parse-ms@4.0.0", "", {}, "sha512-TXfryirbmq34y8QBwgqCVLi+8oA3oWx2eAnSn62ITyEhEYaWRlVZ2DvMM9eZbMs/RfxPu/PK/aBLyGj4IrqMHw=="],
|
||||
|
||||
Reference in New Issue
Block a user