-
-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy paththreadCreateWelcome.ts
More file actions
91 lines (79 loc) · 2.64 KB
/
threadCreateWelcome.ts
File metadata and controls
91 lines (79 loc) · 2.64 KB
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
import {
Container,
type Client,
type ListenerEventData,
TextDisplay,
ThreadCreateListener
} from "@buape/carbon"
import { upsertTrackedThread } from "../utils/trackedThreads.js"
import { postWorkerEvent } from "../utils/workerEvent.js"
const defaultWelcomeTemplate =
`Welcome to the help channel!
<@1457407575476801641> cannot see your system — it only knows what you tell it. The more details you include, the easier it is to help. If you haven’t included it yet, please consider sending:
- What you’re trying to do (goal / expected behaviour)
- What happened instead (exact error message)
- What you ran or clicked (commands, config snippet, etc.)
- Your environment (OS, install method, versions)
- Relevant logs (the smallest useful snippet)
Posts like “it doesn’t work” without details are very hard to debug.
If new issues arise, please open a new thread instead of continuing here — one issue per thread helps keep answers accurate and searchable.`
export default class ThreadCreateWelcome extends ThreadCreateListener {
async handle(data: ListenerEventData[this["type"]], _client: Client) {
const welcomeParentId = process.env.HELPER_THREAD_WELCOME_PARENT_ID?.trim()
if (!welcomeParentId) {
return
}
const thread = data.thread
const parentId = thread.parentId
if (thread.archived || !parentId || parentId !== welcomeParentId) {
return
}
const configuredTemplate = process.env.HELPER_THREAD_WELCOME_TEMPLATE
const template =
configuredTemplate && configuredTemplate.trim().length > 0
? configuredTemplate
: defaultWelcomeTemplate
const createdAt = thread.createTimestamp ?? new Date().toISOString()
const initialMessageCount =
thread.totalMessageSent ?? thread.messageCount ?? null
const workerEventResult = await Promise.allSettled([
postWorkerEvent({
type: "thread_welcome_created",
invokedBy: {
id: null,
username: null,
globalName: null
},
context: {
guildId: thread.guildId ?? null,
channelId: parentId,
threadId: thread.id,
messageCount: initialMessageCount,
parentId
},
data: {}
}),
upsertTrackedThread({
threadId: thread.id,
createdAt,
lastChecked: null,
solved: false,
warningLevel: 0,
closed: false,
lastMessageCount: initialMessageCount
})
])
for (const result of workerEventResult) {
if (result.status === "rejected") {
console.error("Failed to register tracked helper thread:", result.reason)
}
}
try {
await thread.send({
components: [new Container([new TextDisplay(template)])]
})
} catch (error) {
console.error("Failed to send thread welcome message:", error)
}
}
}