-
Notifications
You must be signed in to change notification settings - Fork 59
/
activeThreadsCommand.ts
162 lines (154 loc) · 4.69 KB
/
activeThreadsCommand.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import {
APIThreadChannel,
ApplicationCommandType,
AttachmentBuilder,
ComponentType,
MessageActionRowComponentData,
MessageComponentInteraction,
PermissionsBitField,
SelectMenuComponentOptionData,
SnowflakeUtil,
} from 'discord.js'
import { BotContext } from '@/types/BotContext.js'
import { defineCommand } from '@/types/defineCommand'
import { ActionSet } from '@/utils/ActionSet.js'
import { generateTsv } from '@/utils/generateTsv.js'
import { toLocalDate } from '@/utils/toLocalDate.js'
import { getActiveThreads } from './getActiveThreads'
import { getThreadStats } from './getThreadStats'
export const activeThreadsCommand = defineCommand({
data: {
name: 'active-threads',
description: 'Get information and statistics about server threads',
defaultMemberPermissions: PermissionsBitField.Flags.ManageMessages,
type: ApplicationCommandType.ChatInput,
},
ephemeral: false,
execute: async (botContext, interaction) => {
if (!interaction.guild || !interaction.isChatInputCommand()) return
const { client } = botContext
const threads = await getActiveThreads(client, interaction.guild)
const actionSet = new ActionSet()
const stats = getThreadStats(threads)
const options: SelectMenuComponentOptionData[] = [
{
label: 'Generate report',
value: actionSet.register(
'generate-report',
async (selectInteraction) =>
generateReport(botContext, selectInteraction, threads),
),
},
...stats.pruningCriteria.map((item) => ({
label: `${item.name} (${item.threadIds.length})`,
value: actionSet.register(
'prune-old-threads',
async (selectInteraction) =>
pruneThreads(botContext, selectInteraction, item.threadIds),
),
})),
]
const components: MessageActionRowComponentData[] = [
{
type: ComponentType.StringSelect,
customId: actionSet.register('select', async (selectInteraction) => {
if (!selectInteraction.isStringSelectMenu()) return
const customId = selectInteraction.values[0]
const resolved = actionSet.resolve({ customId })
if (!resolved) return
await resolved.handler(selectInteraction)
}),
placeholder: 'Select an action',
options,
},
]
await interaction.editReply({
content: `There are currently **${threads.length}** active threads in this server.`,
components: [
{
type: ComponentType.ActionRow,
components,
},
],
})
const action = await actionSet.awaitInChannel(
interaction.channel,
60e3,
interaction.user,
)
await interaction.editReply({ components: [] })
if (!action) {
return
}
await action.registeredAction.handler(action.interaction)
},
})
async function generateReport(
botContext: BotContext,
interaction: MessageComponentInteraction,
threads: APIThreadChannel[],
) {
const tsv = generateTsv([
[
'ID',
'Name',
'Channel ID',
'Channel Name',
'Last Message',
'Message Count',
],
...threads.map((thread) => {
const channel = thread.parent_id
? botContext.client.channels.cache.get(thread.parent_id)
: undefined
return [
thread.id,
thread.name,
thread.parent_id ?? '-',
(channel && 'name' in channel && channel.name) ?? '-',
thread.last_message_id
? toLocalDate(SnowflakeUtil.timestampFrom(thread.last_message_id))
: '-',
thread.message_count,
]
}),
])
await interaction.reply({
content: 'Here is the report.',
files: [new AttachmentBuilder(Buffer.from(tsv), { name: 'threads.tsv' })],
ephemeral: true,
})
}
async function pruneThreads(
botContext: BotContext,
selectInteraction: MessageComponentInteraction,
threadIds: string[],
) {
await selectInteraction.reply({
content: `Archiving ${threadIds.length} threads...`,
ephemeral: true,
})
try {
let lastUpdate = 0
let count = 0
for (const threadId of threadIds) {
const thread = botContext.client.channels.cache.get(threadId)
if (!thread || !('setArchived' in thread)) continue
await thread.setArchived(true)
count++
if (Date.now() - lastUpdate > 5e3) {
await selectInteraction.editReply({
content: `Archived ${count}/${threadIds.length} threads...`,
})
lastUpdate = Date.now()
}
}
await selectInteraction.editReply({
content: `Finished archiving ${threadIds.length} threads.`,
})
} catch (error) {
await selectInteraction.editReply({
content: `Error archiving threads: ${error}`,
})
}
}