-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
fetch.ts
79 lines (62 loc) · 1.88 KB
/
fetch.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
import * as app from "#app"
import messages, { Message } from "#tables/message.ts"
export default new app.Command({
name: "fetch",
description: "Fetch all messages from a channel",
channelType: "guild",
botOwnerOnly: true,
positional: [
{
name: "channel",
description: "The channel to fetch messages from",
type: "channel",
required: true,
},
],
async run(message) {
const target = message.args.channel
const feedback = await message.channel.send(
`${app.emote(message, "Loading")} Fetching messages from ${target}...`,
)
let found = 0
const guild = await app.getGuild(message.guild, { forceExists: true })
const userCache = new Map()
const editInterval = 5000
let lastEdit = Date.now()
await app.fetchMessages(message.args.channel, async (chunk) => {
found += chunk.length
const data: Message[] = []
for (const m of chunk) {
if (!userCache.has(m.author.id)) {
const user = await app.getUser(m.author, true)
userCache.set(m.author.id, user)
}
data.push({
author_id: userCache.get(m.author.id)!._id,
guild_id: guild._id,
created_at: m.createdAt.toISOString(),
})
}
if (data.length > 0) await messages.query.insert(data)
if (Date.now() < lastEdit + editInterval) return
lastEdit = Date.now()
await feedback.edit(
`${app.emote(
message,
"Loading",
)} Fetching messages from ${target}... (**${found}** messages from **${
userCache.size
}** users)`,
)
})
if (target.isTextBased()) target.messages.cache.clear()
return feedback.edit(
`${app.emote(
message,
"CheckMark",
)} Successfully fetched **${found}** messages from **${
userCache.size
}** users from ${target}.`,
)
},
})