-
Notifications
You must be signed in to change notification settings - Fork 2
/
restart.ready.ts
65 lines (53 loc) · 1.64 KB
/
restart.ready.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
import * as time from "tims"
import * as app from "#app"
import restart, { Restart } from "#tables/restart.ts"
import { filename } from "dirname-filename-esm"
const __filename = filename(import.meta)
const listener: app.Listener<"ready"> = {
event: "ready",
description: "Send restart messages",
once: true,
async run(client) {
const restartMessages: Restart[] = await restart.query.select()
app.log("restart messages: " + restartMessages.length)
for (const restartMessage of restartMessages) {
const channel = await client.channels.fetch(
restartMessage.last_channel_id,
{ force: true },
)
if (channel?.isTextBased()) {
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**")})`
if (!restartMessage.last_message_id) await channel.send(content)
else {
const message = await channel.messages.fetch(
restartMessage.last_message_id,
)
try {
await message.edit(content)
} catch (error: any) {
app.error(error, __filename)
}
}
} else {
app.error(
`channel ${restartMessage.last_channel_id} is not a text channel`,
__filename,
)
}
}
await restart.query.delete()
},
}
export default listener