1
1
import type {
2
2
DMChannel ,
3
3
Guild ,
4
+ GuildMember ,
4
5
Message ,
5
6
MessageOptions ,
6
7
MessagePayload ,
7
8
TextChannel ,
8
- ThreadChannel } from 'discord.js' ;
9
- import {
10
- MessageActionRow ,
11
- MessageButton ,
12
- MessageEmbed
9
+ ThreadChannel ,
13
10
} from 'discord.js' ;
11
+ import { MessageActionRow , MessageButton , MessageEmbed } from 'discord.js' ;
14
12
15
13
import type { CommandDataWithHandler } from '../../../../types' ;
16
14
import { SERVER_ID , MOD_ROLE_ID , DM_ALT_CHANNEL_ID } from '../../../env' ;
@@ -104,7 +102,7 @@ export const sendCommand: CommandDataWithHandler = {
104
102
] ,
105
103
} ) ;
106
104
} catch ( error ) {
107
- console . error ( error )
105
+ console . error ( error ) ;
108
106
}
109
107
} ,
110
108
onAttach ( client ) {
@@ -126,8 +124,12 @@ export const sendCommand: CommandDataWithHandler = {
126
124
return ;
127
125
}
128
126
129
- const dmChannel = await member . createDM ( ) ;
130
- const message = await dmChannel . messages . fetch ( msgId ) ;
127
+ const message = await getDmMessage ( {
128
+ member,
129
+ msgId,
130
+ guild : interaction . guild ,
131
+ } ) ;
132
+
131
133
try {
132
134
await message . delete ( ) ;
133
135
if ( 'edit' in interaction . message ) {
@@ -179,7 +181,7 @@ async function sendFakeDM(
179
181
{ upsert : true , new : true }
180
182
) . exec ( ) ;
181
183
182
- console . log ( dmThread )
184
+ console . log ( dmThread ) ;
183
185
184
186
const channel = ( await guild . channels . fetch (
185
187
dmThread . channelId
@@ -190,20 +192,59 @@ async function sendFakeDM(
190
192
thread = await channel . threads . fetch ( dmThread . threadId ) ;
191
193
} else {
192
194
try {
193
- thread = await channel . threads . create ( {
194
- name : `${ dmChannel . recipient . username } _${ dmChannel . recipient . discriminator } [${ userId } ]` ,
195
- type : 'GUILD_PRIVATE_THREAD' ,
196
- } ) ;
197
- } catch {
198
- thread = await channel . threads . create ( {
199
- name : `${ dmChannel . recipient . username } _${ dmChannel . recipient . discriminator } [${ userId } ]` ,
200
- type : 'GUILD_PUBLIC_THREAD' ,
201
- } ) ;
202
- }
195
+ thread = await channel . threads . create ( {
196
+ name : `${ dmChannel . recipient . username } _${ dmChannel . recipient . discriminator } [${ userId } ]` ,
197
+ type : 'GUILD_PRIVATE_THREAD' ,
198
+ } ) ;
199
+ } catch {
200
+ thread = await channel . threads . create ( {
201
+ name : `${ dmChannel . recipient . username } _${ dmChannel . recipient . discriminator } [${ userId } ]` ,
202
+ type : 'GUILD_PUBLIC_THREAD' ,
203
+ } ) ;
204
+ }
203
205
204
206
dmThread . updateOne ( { threadId : thread . id } ) . exec ( ) ;
205
207
}
206
208
207
209
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
208
- return thread . send ( typeof content === 'string' ? `${ dmChannel . recipient } ${ content } ` : { content : `${ dmChannel . recipient } ${ ( content as { content ?: string } ) . content ?? '' } ` , ...content } )
210
+ return thread . send (
211
+ typeof content === 'string'
212
+ ? `${ dmChannel . recipient } ${ content } `
213
+ : {
214
+ content : `${ dmChannel . recipient } ${
215
+ ( content as { content ?: string } ) . content ?? ''
216
+ } `,
217
+ ...content ,
218
+ }
219
+ ) ;
220
+ }
221
+ async function getDmMessage ( {
222
+ member,
223
+ guild,
224
+ msgId,
225
+ } : {
226
+ member : GuildMember ;
227
+ guild : Guild ;
228
+ msgId : string ;
229
+ } ) : Promise < Message | null > {
230
+ const userId = member . user . id ;
231
+ const dmChannelReplacement = await DMThread . findOne ( {
232
+ guildId : SERVER_ID ,
233
+ userId,
234
+ closedAt : { $exists : false } ,
235
+ } ) . exec ( ) ;
236
+
237
+ console . log ( dmChannelReplacement )
238
+ if ( dmChannelReplacement ) {
239
+ const channel = ( await guild . channels . fetch (
240
+ dmChannelReplacement . channelId
241
+ ) ) as TextChannel ;
242
+
243
+ const thread = await channel . threads . fetch ( dmChannelReplacement . threadId ) ;
244
+ return thread . messages . fetch ( msgId ) ;
245
+ }
246
+
247
+ const dmChannel = await member . createDM ( ) ;
248
+
249
+ return await dmChannel . messages . fetch ( msgId ) ;
209
250
}
0 commit comments