-
Notifications
You must be signed in to change notification settings - Fork 2
/
activity.ready.ts
75 lines (60 loc) · 2 KB
/
activity.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
66
67
68
69
70
71
72
73
74
75
import * as app from "#app"
const intervals: Record<string, NodeJS.Timeout> = {}
let lastActiveCount = 0
const listener: app.Listener<"ready"> = {
event: "ready",
description: "Start an interval to update the active list",
async run(client) {
const guilds = await client.guilds.fetch()
for (const [, guild] of guilds) {
const config = await app.getGuild(guild)
if (!config?.active_role_id) continue
if (intervals[guild.id] !== undefined) clearInterval(intervals[guild.id])
const interval = Number(config.active_refresh_interval)
const period = Number(config.active_period)
const messageCount = Number(config.active_message_count)
intervals[guild.id] = setInterval(
async () => {
const realGuild = await guild.fetch()
if (!(await app.hasActivity(config._id, interval))) return
let found: number
try {
found = await app.updateActive(realGuild, {
force: false,
period,
messageCount,
guildConfig: config,
})
} catch (error: any) {
await app.sendLog(
realGuild,
`Failed to update the active list...${await app.code.stringify({
content: error.message,
lang: "js",
})}`,
)
return
}
if (found > lastActiveCount) {
await app.sendLog(
realGuild,
`Finished updating the active list, found **${
found - lastActiveCount
}** active members.`,
)
} else if (found < lastActiveCount) {
await app.sendLog(
realGuild,
`Finished updating the active list, **${
lastActiveCount - found
}** members have been removed.`,
)
}
lastActiveCount = found
},
interval * 1000 * 60 * 60,
)
}
},
}
export default listener