|
| 1 | +# Creating Buttons |
| 2 | + |
| 3 | +`Button` files contain a **button handler**. They aren't responsible for adding buttons to messages but are a way of running code when the buttons you made are pressed. |
| 4 | + |
| 5 | +```js |
| 6 | +import { button } from 'jellycommands'; |
| 7 | + |
| 8 | +export default button({ |
| 9 | + id: 'test', |
| 10 | + |
| 11 | + run: () => { |
| 12 | + // Do something with button click |
| 13 | + } |
| 14 | +}) |
| 15 | +``` |
| 16 | + |
| 17 | +[You can view a list of all the button options here](/api/buttons#options) |
| 18 | + |
| 19 | +:::tip TIP |
| 20 | +If you are unsure how to add buttons to messages, checkout the [Discord.js Guide](https://discordjs.guide/interactions/buttons.html) |
| 21 | +::: |
| 22 | + |
| 23 | +## Button `id` |
| 24 | + |
| 25 | +The `id` field on the button is important, it can be a `string`, `regexp`, or a function. The `id` field corresponds to the `customId` feild you set when you create a button. |
| 26 | + |
| 27 | +### Simple ids |
| 28 | + |
| 29 | +The simplest form is a plain string, for example you could have an id of `hello`. This might be a button that does the same thing each time. |
| 30 | + |
| 31 | +```js |
| 32 | +import { button } from 'jellycommands'; |
| 33 | + |
| 34 | +export default button({ |
| 35 | + id: 'hello', |
| 36 | + |
| 37 | + run: async ({ interaction }) => { |
| 38 | + await interaction.reply({ |
| 39 | + content: 'Hello there!' |
| 40 | + }) |
| 41 | + } |
| 42 | +}) |
| 43 | +``` |
| 44 | + |
| 45 | +### Regex & Function ids |
| 46 | + |
| 47 | +If the id is not the same each time. As an example, say you have a button that should respond with a fruit name. Your id might look like `fruit_[FRUIT NAME]`, we can impliment this with regex or a function. The function should always return `true` or `false` |
| 48 | + |
| 49 | +```js |
| 50 | +import { button } from 'jellycommands'; |
| 51 | + |
| 52 | +export default button({ |
| 53 | + // a regex id |
| 54 | + id: /fruit_([\w])+/, |
| 55 | + |
| 56 | + // a function id |
| 57 | + id: (id) => { |
| 58 | + return id.startsWith('fruit_'); |
| 59 | + }, |
| 60 | + |
| 61 | + run: async ({ interaction }) => { |
| 62 | + const fruit = interaction.customId.replace('fruit_', ''); |
| 63 | + |
| 64 | + await interaction.reply({ |
| 65 | + content: `Your fruit is ${fruit}` |
| 66 | + }) |
| 67 | + } |
| 68 | +}) |
| 69 | +``` |
| 70 | + |
| 71 | +## Button `run` handler |
| 72 | + |
| 73 | +When an `event` is invoked, the event's `run` function is called. This is where your custom event logic lives. |
| 74 | + |
| 75 | +When a button is clicked, the `run` function is called. This is where your custom logic for the button lives. |
| 76 | + |
| 77 | +You are provided with [`context`](/guide/buttons/files.html#context), which allows you to get things such as the `interaction`. |
| 78 | + |
| 79 | +### Context |
| 80 | + |
| 81 | +The context object has the following properties: |
| 82 | + |
| 83 | +### interaction [`ButtonInteraction`](https://discord.js.org/#/docs/discord.js/main/class/ButtonInteraction) |
| 84 | + |
| 85 | +The button interaction from discord.js |
| 86 | + |
| 87 | +#### client [`JellyCommands`](/api/client) |
| 88 | + |
| 89 | +The client used by the command. |
| 90 | + |
| 91 | +#### props [`Props`](/api/props) |
| 92 | + |
| 93 | +Your project's props. |
0 commit comments