1
1
import { ApplyOptions } from '@sapphire/decorators' ;
2
2
import { InteractionHandler , type InteractionHandlerOptions , InteractionHandlerTypes } from '@sapphire/framework' ;
3
- import { ActionRowBuilder , ButtonInteraction , ModalBuilder , ModalSubmitInteraction , TextInputBuilder , TextInputStyle } from 'discord.js' ;
3
+ import { ActionRowBuilder , ButtonInteraction , MessageFlags , ModalBuilder , ModalSubmitInteraction , TextInputBuilder , TextInputStyle } from 'discord.js' ;
4
4
5
5
@ApplyOptions < InteractionHandlerOptions > ( {
6
6
interactionHandlerType : InteractionHandlerTypes . Button ,
@@ -9,25 +9,23 @@ import { ActionRowBuilder, ButtonInteraction, ModalBuilder, ModalSubmitInteracti
9
9
export class TheButtonClick extends InteractionHandler {
10
10
11
11
public async parse ( interaction : ButtonInteraction ) {
12
- if ( interaction . customId !== 'SafetyButton' ) {
12
+ if ( ! interaction . customId . startsWith ( 'SafetyButton' ) ) {
13
13
return this . none ( ) ;
14
14
}
15
15
16
- return this . some ( ) ;
16
+ const [ , userId ] = interaction . customId . split ( '|' ) ;
17
+ return this . some ( userId ) ;
17
18
}
18
19
19
- public async run ( interaction : ButtonInteraction ) {
20
- if ( ! process . env . OWNERS ) {
21
- throw new Error ( 'No Owners defined' ) ;
22
- }
23
- await this . container . client . users . fetch ( process . env . OWNERS ) . then ( owner => owner . send ( '**The Button** was pressed' ) ) ;
20
+ public async run ( interaction : ButtonInteraction , userId : InteractionHandler . ParseResult < this> ) {
21
+ await this . container . client . users . fetch ( userId ) . then ( user => user . send ( '**The Button** was pressed' ) ) ;
24
22
25
23
interaction . showModal ( new ModalBuilder ( )
26
- . setCustomId ( ' SafetyModal' )
24
+ . setCustomId ( ` SafetyModal| ${ userId } ` )
27
25
. setTitle ( 'You clicked The Button' )
28
26
. addComponents ( new ActionRowBuilder < TextInputBuilder > ( ) . addComponents (
29
27
new TextInputBuilder ( )
30
- . setCustomId ( ' SafetyContext' )
28
+ . setCustomId ( ` SafetyContext` )
31
29
. setLabel ( 'Add context to your click?' )
32
30
. setStyle ( TextInputStyle . Paragraph )
33
31
. setPlaceholder ( 'Please click submit with this box empty if you wish to not add context' )
@@ -45,26 +43,23 @@ export class TheButtonClick extends InteractionHandler {
45
43
export class TheButtonModal extends InteractionHandler {
46
44
47
45
public async parse ( interaction : ModalSubmitInteraction ) {
48
- if ( interaction . customId !== 'SafetyModal' ) {
46
+ if ( ! interaction . customId . startsWith ( 'SafetyModal' ) ) {
49
47
return this . none ( ) ;
50
48
}
51
49
52
- return this . some ( ) ;
50
+ const [ , userId ] = interaction . customId . split ( '|' ) ;
51
+ return this . some ( userId ) ;
53
52
}
54
53
55
- public async run ( interaction : ModalSubmitInteraction ) {
56
- if ( ! process . env . OWNERS ) {
57
- throw new Error ( 'No Owners defined' ) ;
58
- }
59
-
54
+ public async run ( interaction : ModalSubmitInteraction , userId : InteractionHandler . ParseResult < this> ) {
60
55
const input = interaction . fields . getTextInputValue ( 'SafetyContext' ) ;
61
56
62
- await this . container . client . users . fetch ( process . env . OWNERS )
63
- . then ( owner => owner . send ( input === '' ? 'No context provided' : input ) ) ;
57
+ await this . container . client . users . fetch ( userId )
58
+ . then ( user => user . send ( input === '' ? 'No context provided' : input ) ) ;
64
59
65
60
interaction . reply ( {
66
61
content : `${ input === '' ? 'No additional context was sent' : 'I\'ve sent that context' } ` ,
67
- ephemeral : true
62
+ flags : MessageFlags . Ephemeral
68
63
} ) ;
69
64
}
70
65
0 commit comments