@@ -15,9 +15,11 @@ import {
1515 * If the first argument is null, it means that the interaction collector has been destroyed.
1616 */
1717export type CommandKitButtonBuilderInteractionCollectorDispatch = (
18- interaction : ButtonInteraction | null ,
18+ interaction : ButtonInteraction ,
1919) => Awaitable < void > ;
2020
21+ export type CommandKitButtonBuilderOnEnd = ( ) => Awaitable < void > ;
22+
2123export type CommandKitButtonBuilderInteractionCollectorDispatchContextData = {
2224 /**
2325 * The message to listen for button interactions on.
@@ -35,6 +37,7 @@ export type CommandKitButtonBuilderInteractionCollectorDispatchContextData = {
3537
3638export class ButtonKit extends ButtonBuilder {
3739 #onClickHandler: CommandKitButtonBuilderInteractionCollectorDispatch | null = null ;
40+ #onEndHandler: CommandKitButtonBuilderOnEnd | null = null ;
3841 #contextData: CommandKitButtonBuilderInteractionCollectorDispatchContextData | null = null ;
3942 #collector: InteractionCollector < ButtonInteraction > | null = null ;
4043
@@ -63,29 +66,38 @@ export class ButtonKit extends ButtonBuilder {
6366 * button.onClick(null);
6467 * ```
6568 */
66- public onClick ( handler : null ) : this;
6769 public onClick (
6870 handler : CommandKitButtonBuilderInteractionCollectorDispatch ,
69- data : CommandKitButtonBuilderInteractionCollectorDispatchContextData ,
70- ) : this;
71- public onClick (
72- handler : CommandKitButtonBuilderInteractionCollectorDispatch | null ,
7371 data ?: CommandKitButtonBuilderInteractionCollectorDispatchContextData ,
7472 ) : this {
7573 if ( this . data . style === ButtonStyle . Link ) {
7674 throw new TypeError ( 'Cannot setup "onClick" handler on link buttons.' ) ;
7775 }
7876
77+ if ( ! handler ) {
78+ throw new TypeError ( 'Cannot setup "onClick" without a handler function parameter.' ) ;
79+ }
80+
7981 this . #destroyCollector( ) ;
8082
8183 this . #onClickHandler = handler ;
82- if ( handler && data ) this . #contextData = data ;
84+ if ( data ) this . #contextData = data ;
8385
8486 this . #setupInteractionCollector( ) ;
8587
8688 return this ;
8789 }
8890
91+ public onEnd ( handler : CommandKitButtonBuilderOnEnd ) : this {
92+ if ( ! handler ) {
93+ throw new TypeError ( 'Cannot setup "onEnd" without a handler function parameter.' ) ;
94+ }
95+
96+ this . #onEndHandler = handler ;
97+
98+ return this ;
99+ }
100+
89101 #setupInteractionCollector( ) {
90102 if ( ! this . #contextData || ! this . #onClickHandler) return ;
91103
@@ -133,11 +145,11 @@ export class ButtonKit extends ButtonBuilder {
133145
134146 this . #collector. on ( 'end' , ( ) => {
135147 this . #destroyCollector( ) ;
148+ this . #onEndHandler?.( ) ;
136149 } ) ;
137150 }
138151
139152 #destroyCollector( ) {
140- this . #onClickHandler?.( null ) ;
141153 this . #collector?. stop ( 'end' ) ;
142154 this . #collector?. removeAllListeners ( ) ;
143155 this . #collector = null ;
0 commit comments