-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
backup.ts
131 lines (116 loc) · 3.56 KB
/
backup.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import fs from "fs"
import path from "path"
import * as app from "#app"
import restart from "#tables/restart.ts"
export default new app.Command({
name: "backup",
description: "Manage database backups",
channelType: "all",
botOwnerOnly: true,
async run(message) {
return app.sendCommandDetails(message, this)
},
subs: [
new app.Command({
name: "create",
description: "Create a database backup",
aliases: ["new", "add", "save"],
channelType: "all",
botOwnerOnly: true,
positional: [
{
name: "name",
description: "The name of the backup",
required: true,
type: "string",
},
],
async run(message) {
try {
const backups = await fs.promises.readdir(
app.database.config.backups!.location!,
)
if (backups.includes(message.args.name)) {
return message.reply(
`${app.emote(message, "Cross")} Backup with that name already exists.`,
)
}
} 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 view.edit(
`${app.emote(message, "CheckMark")} Successfully created backup (${app.formatDuration(startAt)})`,
)
},
}),
new app.Command({
name: "restore",
description: "Restore a database backup",
aliases: ["load"],
channelType: "all",
botOwnerOnly: true,
positional: [
{
name: "name",
description: "The name of the backup",
required: true,
type: "string",
},
],
async run(message) {
const view = await message.reply(
`${app.emote(message, "Loading")} Restoring backup...`,
)
await app.database.restoreBackup(message.args.name)
const created_at = new Date().toISOString()
await restart.query.insert({
content: `${app.emote(message, "CheckMark")} Successfully restored the "${message.args.name}" backup and restarted the bot.`,
last_channel_id: message.channel.id,
last_message_id: view.id,
created_at,
})
process.exit(0)
},
}),
new app.Command({
name: "list",
description: "List all database backups",
aliases: ["ls"],
channelType: "all",
botOwnerOnly: true,
async run(message) {
try {
const backups = await fs.promises.readdir(
app.database.config.backups!.location!,
)
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.`,
)
}
},
}),
],
})