11import "reflect-metadata" ;
22import { readFileSync } from "node:fs" ;
33import process from "node:process" ;
4- import { setTimeout } from "node:timers" ;
54import { URL , fileURLToPath } from "node:url" ;
6- import type { ToEventProps } from "@discordjs/core" ;
75import { Client , GatewayDispatchEvents , GatewayIntentBits , MessageFlags } from "@discordjs/core" ;
86import { inlineCode } from "@discordjs/formatters" ;
97import { REST } from "@discordjs/rest" ;
108import { WebSocketManager } from "@discordjs/ws" ;
119import * as TOML from "@ltd/j-toml" ;
12- import type { GatewayMessageCreateDispatchData } from "discord-api-types/v10" ;
1310import {
1411 ApplicationCommandOptionType ,
1512 ApplicationCommandType ,
@@ -97,66 +94,34 @@ client.on(GatewayDispatchEvents.MessageCreate, async ({ data: message }) => {
9794 }
9895} ) ;
9996
100- /**
101- * Wait for message in the provided channel
102- *
103- * @param channelId - The id of the channel to listen in
104- * @param timeoutMs - The time in milliseconds to wait until the listener is aborted
105- * @returns The received message
106- */
107- async function waitForMessage ( channelId : string , timeoutMs ?: number ) {
108- return new Promise ( ( resolve , reject ) => {
109- const callback = ( { data : message } : ToEventProps < GatewayMessageCreateDispatchData > ) => {
110- if ( message . channel_id === channelId ) {
111- resolve ( message ) ;
112- }
113- } ;
114-
115- client . once ( GatewayDispatchEvents . MessageCreate , callback ) ;
116-
117- if ( timeoutMs ) {
118- setTimeout ( ( ) => {
119- client . off ( GatewayDispatchEvents . MessageCreate , callback ) ;
120- reject ( new Error ( "time" ) ) ;
121- } , timeoutMs ) ;
122- }
123- } ) ;
124- }
97+ const supportTheadParents = new Map < string , string > ( ) ;
12598
12699client . on ( GatewayDispatchEvents . ThreadCreate , async ( { data : channel } ) => {
127- if ( ! channel . newly_created || ! ASSISTCHANNELS . includes ( channel . parent_id ?? "" ) ) {
100+ if ( ! channel . parent_id || ! ASSISTCHANNELS . includes ( channel . parent_id ) ) {
128101 return ;
129102 }
130103
131- const receivedAt = Date . now ( ) ;
132-
133- // Messages can only be sent after the thread starter message has arrived.
134- // This can take substantial amounts of time after thread create in the case of large attachments
135- const collected = await waitForMessage ( channel . id , 60_000 ) . catch ( ( error : Error ) => {
136- const failedAt = Date . now ( ) ;
137- const failedAfterSeconds = ( failedAt - receivedAt ) / 1_000 ;
138- logger . info (
139- error ,
140- `Failed while waiting for a message in channel ${ channel . id } : ${
141- error . message
142- } , failed after: ${ failedAfterSeconds . toFixed ( 2 ) } s`,
143- ) ;
144- } ) ;
104+ supportTheadParents . set ( channel . id , channel . parent_id ) ;
105+ } ) ;
145106
146- if ( ! collected ) {
107+ client . on ( GatewayDispatchEvents . MessageCreate , async ( { data : message } ) => {
108+ const parent = supportTheadParents . get ( message . channel_id ) ;
109+ if ( message . id !== message . channel_id || ! parent || ! ASSISTCHANNELS . includes ( parent ) ) {
147110 return ;
148111 }
149112
113+ supportTheadParents . delete ( message . channel_id ) ;
114+
150115 const parts : string [ ] = [ ] ;
151- if ( channel . parent_id === SUPPORT_CHANNEL_VOICE ) {
116+ if ( parent === SUPPORT_CHANNEL_VOICE ) {
152117 parts . push (
153118 "- What are your intents? `GuildVoiceStates` is **required** to receive voice data!" ,
154119 "- Show what dependencies you are using -- `generateDependencyReport()` is exported from `@discordjs/voice`." ,
155120 "- Try looking at common examples: <https://github.com/discordjs/voice-examples>." ,
156121 ) ;
157122 }
158123
159- if ( channel . parent_id === SUPPORT_CHANNEL ) {
124+ if ( parent === SUPPORT_CHANNEL ) {
160125 parts . push (
161126 "- What's your exact discord.js `npm list discord.js` and node `node -v` version?" ,
162127 "- Not a discord.js issue? Check out <#1081585952654360687>." ,
@@ -170,7 +135,7 @@ client.on(GatewayDispatchEvents.ThreadCreate, async ({ data: channel }) => {
170135 "- Show your code!" ,
171136 ) ;
172137
173- await client . api . channels . createMessage ( channel . id , {
138+ await client . api . channels . createMessage ( message . channel_id , {
174139 flags : MessageFlags . IsComponentsV2 ,
175140 components : [
176141 {
0 commit comments