Skip to content

Commit ed3490d

Browse files
committed
docs: document onEnd
1 parent 3214039 commit ed3490d

File tree

1 file changed

+41
-7
lines changed

1 file changed

+41
-7
lines changed

apps/docs/pages/guide/buttonkit.mdx

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ButtonKit is an enhanced version of the native Discord.js [`ButtonBuilder`](http
66

77
It is not recommended to use this to listen for button clicks forever since it creates collectors. For that purpose, it's recommended to use a regular "interactionCreate" event listener.
88

9-
## Usage
9+
## Handle button clicks
1010

1111
<Tabs items={['CommonJS', 'ESM', 'TypeScript']}>
1212
<Tabs.Tab>
@@ -98,7 +98,7 @@ In the above example, you may notice how similar `ButtonKit` is to the native Di
9898

9999
Here's an empty `onClick` method without any arguments:
100100

101-
```js
101+
```js copy
102102
const myButton = new ButtonKit()
103103
.setCustomId('custom_button')
104104
.setLabel('Click me!')
@@ -109,15 +109,15 @@ myButton.onClick();
109109

110110
The first argument required by this function is your handler function which will acknowledge button clicks (interactions) handled by ButtonKit. You can handle them like so:
111111

112-
```js
112+
```js copy
113113
myButton.onClick((buttonInteraction) => {
114114
buttonInteraction.reply('You clicked a button!');
115115
});
116116
```
117117

118-
However, the code above won't actually work since ButtonKit doesn't have any idea where it's supposed to specifically listen button clicks from. To fix that, you need to pass in a second argument which houses all the collector options. The `message` property is the message that ButtonKit will use to listen for button clicks.
118+
However, the code above won't actually work since ButtonKit doesn't have any idea where it's supposed to specifically listen button clicks from. To fix that, you need to pass in a second argument, also known as your [options]() which houses all the collector configuration. The `message` property is the message that ButtonKit will use to listen for button clicks.
119119

120-
```js
120+
```js copy
121121
const row = new ActionRowBuilder().addComponents(myButton);
122122
const message = await channel.send({ components: [row] });
123123

@@ -131,7 +131,7 @@ myButton.onClick(
131131

132132
This also works with interaction replies. Just ensure you pass `fetchReply` alongside your components:
133133

134-
```js
134+
```js copy
135135
const row = new ActionRowBuilder().addComponents(myButton);
136136
const message = await interaction.reply({ components: [row], fetchReply: true });
137137

@@ -143,7 +143,7 @@ myButton.onClick(
143143
);
144144
```
145145

146-
## ButtonKit options
146+
## ButtonKit `onClick` options
147147

148148
### `message`
149149

@@ -154,6 +154,7 @@ The message object that ButtonKit uses to listen for button clicks (interactions
154154
### `time` (optional)
155155

156156
- Type: `number`
157+
- Default: `86400000`
157158

158159
The duration (in ms) the collector should run for and listen for button clicks.
159160

@@ -166,3 +167,36 @@ Whether or not the collector should automatically reset the timer when a button
166167
### Additional optional options
167168

168169
- Type: [`InteractionCollectorOptions`](https://old.discordjs.dev/#/docs/discord.js/main/typedef/InteractionCollectorOptions)
170+
171+
## Handle collector end
172+
173+
<Callout type="warning">
174+
This feature is currently only available in the [development
175+
version](/guide/installation#development-version)
176+
</Callout>
177+
178+
When setting up an `onClick()` method using ButtonKit, you may also want to run some code after the collector ends running. The default timeout is 1 day, but you can modify this in [onClickOptions#time](/guide/buttonkit#time-optional). To handle when the collector ends, you can setup an `onEnd()` method like this:
179+
180+
```js copy {16-21}
181+
const myButton = new ButtonKit()
182+
.setCustomId('custom_button')
183+
.setLabel('Click me!')
184+
.setStyle(ButtonStyle.Primary);
185+
186+
const row = new ActionRowBuilder().addComponents(myButton);
187+
const message = await interaction.reply({ components: [row], fetchReply: true });
188+
189+
myButton
190+
.onClick(
191+
(buttonInteraction) => {
192+
buttonInteraction.reply('You clicked a button!');
193+
},
194+
{ message },
195+
)
196+
.onEnd(() => {
197+
console.log('Button collector ended.');
198+
199+
myButton.setDisabled(true);
200+
message.edit({ components: [row] });
201+
});
202+
```

0 commit comments

Comments
 (0)