Skip to content

Commit

Permalink
enhanced the backup commands
Browse files Browse the repository at this point in the history
  • Loading branch information
GhomKrosmonaute committed Oct 23, 2024
1 parent ec9cbeb commit cab92db
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 20 deletions.
38 changes: 31 additions & 7 deletions src/commands/backup.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import fs from "fs"
import path from "path"

import * as app from "#app"
import restart from "#tables/restart.ts"
Expand Down Expand Up @@ -39,10 +40,16 @@ export default new app.Command({
}
} catch {}

const view = await message.reply(
`${app.emote(message, "Loading")} Creating backup...`,
)

const startAt = new Date().toISOString()

await app.database.createBackup(message.args.name)

return message.reply(
`${app.emote(message, "CheckMark")} Successfully created backup.`,
return view.edit(
`${app.emote(message, "CheckMark")} Successfully created backup (${app.formatDuration(startAt)})`,
)
},
}),
Expand Down Expand Up @@ -91,11 +98,28 @@ export default new app.Command({
app.database.config.backups!.location!,
)

return message.reply(
backups.length
? backups.join("\n")
: `${app.emote(message, "Cross")} No backups found.`,
)
if (backups.length === 0) {
return message.reply(
`${app.emote(message, "Cross")} No backups found.`,
)
}

new app.DynamicPaginator({
target: message.channel,
filter: (reaction, user) => user.id === message.author.id,
fetchPageCount: () => backups.length,
fetchPage: async (pageIndex) => {
const name = backups[pageIndex]

const chunks = await fs.promises.readdir(
path.join(app.database.config.backups!.location!, name),
)

return {
content: `**${name}**\n${chunks.map((chunk) => `- ${chunk}`).join("\n")}`,
}
},
})
} catch {
return message.reply(
`${app.emote(message, "Cross")} No backups found.`,
Expand Down
14 changes: 1 addition & 13 deletions src/listeners/restart.ready.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,7 @@ const listener: app.Listener<"ready"> = {
)

if (channel?.isSendable()) {
const content = `${restartMessage.content} (${time
.duration(
new Date(restartMessage.created_at).getTime() - Date.now(),
{
format: "ms",
maxPartCount: 3,
},
)
.replace(
/(?:millièmes? de seconde|thousandths? of (?:a )?second)/,
"ms",
)
.replace(/(\d+)/g, "**$1**")})`
const content = `${restartMessage.content} (${app.formatDuration(restartMessage.created_at)})`

if (!restartMessage.last_message_id) await channel.send(content)
else {
Expand Down
10 changes: 10 additions & 0 deletions src/namespaces/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,13 @@ export function addCreatedAt(table: Knex.CreateTableBuilder) {
})
.defaultTo(app.database.database.fn.now())
}

export function formatDuration(from: string) {
return app
.duration(new Date(from).getTime() - Date.now(), {
format: "ms",
maxPartCount: 3,
})
.replace(/(?:millièmes? de seconde|thousandths? of (?:a )?second)/, "ms")
.replace(/(\d+)/g, "**$1**")
}

0 comments on commit cab92db

Please sign in to comment.