This repository has been archived by the owner on Aug 10, 2022. It is now read-only.
/
button-component.ts
79 lines (71 loc) · 1.9 KB
/
button-component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import type { EmojiResolvable, Message, MessageButtonStyle } from "discord.js"
import { randomUUID } from "node:crypto"
import type { InteractionContext } from "../interaction-context"
/**
* Options for {@link buttonComponent}
*/
export type ButtonComponentOptions = {
/**
* The text to display on the button
*/
label: string
/**
* An emoji displayed on the button.
* If you only want to show an emoji, pass an empty string for the label.
* @see https://discord.js.org/#/docs/main/stable/typedef/EmojiResolvable
*/
emoji?: EmojiResolvable
/**
* The color and intent of the button.
* @see https://discord.js.org/#/docs/main/stable/typedef/MessageButtonStyle
*/
style: Exclude<MessageButtonStyle, "LINK">
/**
* Whether the button is disabled.
* This can be anti-accessibility, so consider an alternative,
* like showing an error on click, or hiding the button entirely.
*/
disabled?: boolean
/**
* Called when the button is clicked
*/
onClick: (context: ButtonInteractionContext) => unknown
}
/**
* Returned from {@link buttonComponent}
*/
export type ButtonComponent = ButtonComponentOptions & {
type: "button"
customId: string
}
/**
* The context object received by button onClick handlers.
* See {@link buttonComponent}
*/
export type ButtonInteractionContext = InteractionContext & {
readonly message: Message
}
/**
* Represents a discord [button](https://discord.com/developers/docs/interactions/message-components#buttons) component.
* Does not support URL or disabled props yet.
*
* ```js
* context.reply(() => (
* buttonComponent({
* label: "Click me!",
* onClick: context => {
* context.reply(() => "You clicked me!"),
* },
* }),
* ))
* ```
*/
export function buttonComponent(
options: ButtonComponentOptions,
): ButtonComponent {
return {
...options,
type: "button",
customId: randomUUID(),
}
}