Skip to content

Commit c58df24

Browse files
committed
reference,learn: start moving custom interactions to learn
Signed-off-by: Luca Zeuch <[email protected]>
1 parent b6db845 commit c58df24

File tree

7 files changed

+196
-0
lines changed

7 files changed

+196
-0
lines changed

content/learn/advanced/_index.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
+++
2+
title = "Advanced"
3+
weight = 400
4+
+++
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
+++
2+
title = "Custom Interactions"
3+
weight = 410
4+
+++
Loading
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
+++
2+
title = "Creating Interactive Elements"
3+
weight = 412
4+
+++
5+
6+
Before you can start triggering custom commands with interactive elements---such as buttons---you'll obviously need
7+
to have elements to interact with. In this section, we'll cover how to create these elements and then make them usable
8+
to trigger custom commands.
9+
10+
## Message Components
11+
12+
### Buttons
13+
14+
Buttons are probably the simplest interactive element to create, so we'll start with them. To create a button, we use
15+
the [`cbutton`](docs/reference/templates/functions#cbutton) function. In and of itself, that is rather useless, so we'll
16+
also have to attach it to a message. We do that by calling the
17+
[`complexMessage`](docs/reference/templates/functions#complexmessage) builder and adding the result of `cbutton` to it.
18+
Finally, we send the message.
19+
20+
```yag
21+
{{ $button := cbutton "label" "My Cool Button" "custom-id" "buttons-duck" }}
22+
{{ $m := complexMessage "buttons" $button }}
23+
{{ sendMessage nil $m }}
24+
```
25+
26+
Result:
27+
28+
![A basic button, sent with the above code.](basic_button.png)
29+
30+
We've successfully crated a basic button! It doesn't do anything yet, but we'll cover that in a later section.
31+
In the meantime, play around with the values `cbutton` takes. Try to attach an emoji to it, or change its style!
32+
33+
### Select Menus
34+
35+
Select menus act as dropdowns, allowing users to select one or more of several options. To further complicate things,
36+
select menus come in different types, each offering different functionality. We'll first focus on the most intuitive
37+
type, the **Text** select menu. This type allows you to define a custom list of options.
38+
39+
Available select menu types are as follows:
40+
41+
- **Text** - The options available to be selected are defined when creating the select menu. Options have labels, and
42+
can optionally have emojis and longer-form descriptions.
43+
- **User** - The options are populated with users on the server by Discord.
44+
- **Role** - The options are populated with roles on the server by Discord.
45+
- **Mentionable** - The options are auto-populated with both users and roles on the server by Discord, allowing members
46+
to select both.
47+
- **Channel** - The options are populated with channels on the server by Discord. You can limit which channel types
48+
appear as options when creating the select menu.
49+
50+
#### Text Select Menus
51+
52+
To create a text select menu, we use the [`cmenu`](docs/reference/templates/functions#cmenu) function. Then, just like
53+
with a button, we attach it to a message and send it.
54+
55+
```yag
56+
{{ $menu := cmenu
57+
"type" "text"
58+
"placeholder" "Choose a terrible thing"
59+
"custom_id" "menus-duck"
60+
"options" (cslice
61+
(sdict "label" "Two Ducks" "value" "opt-1" "default" true)
62+
(sdict "label" "A Duck" "value" "duck-option" "emoji" (sdict "name" "🦆"))
63+
(sdict "label" "Half a Duck" "value" "third-option" "description" "Don't let the smaller amount fool you."))
64+
"max_values" 3
65+
}}
66+
67+
{{ sendMessage nil (complexMessage "menus" $menu) }}
68+
```
69+
70+
Opening the select menu that was sent using the above code should yield the following result:
71+
72+
![A Photo of the above menu](text_menu_example.png)
73+
74+
In this menu, our first option (Ducks) is defined as `default`, which is why it is already selected when we look at the
75+
menu on our server. You can define multiple default options, however the amount of default options you define must fall
76+
between your `min_values` and `max_values`.
77+
78+
We have also set the `max_values` to 3, and we haven't set a `min_values` argument. This means the server member could
79+
select anywhere between 1 and 3 of these options.
80+
81+
#### Other Select Menu Types
82+
83+
The other select menu types are created in the same way as the text select menu, but with a few differences. As Discord
84+
automatically populates the options, you need not---nor can you---define these options.
85+
86+
```yag
87+
{{ $menu := cmenu
88+
"type" "role"
89+
"placeholder" "Choose roles who are secretly ducks"
90+
"custom_id" "menus-duck-roles"
91+
"max_values" 3
92+
}}
93+
94+
{{ sendMessage nil (complexMessage "menus" $menu) }}
95+
```
96+
97+
![A photo of the above role select menu.](role_select_menu_example.png)
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
+++
2+
title = "Introduction"
3+
weight = 411
4+
description = "Learn how to use buttons, modals, and select menus in custom commnads."
5+
+++
6+
7+
{{< callout context="danger" title="Danger: Advanced Topic" icon="outline/alert-octagon" >}}
8+
9+
Use of interactions within YAGPDB is an advanced topic; you will need a thorough understanding of YAGPDB's scripting
10+
language before learning interactions.
11+
12+
{{< /callout >}}
13+
14+
Interactions within Discord allow server members to use alternative, built-in features to trigger bots to take action
15+
other than messages or reactions. These features include builtin buttons, dropdown selection menus, or submitting a
16+
modal (basically a pop-up form). Within custom commands it is possible to not only create and customize these new
17+
interactive features, but respond to them as well, opening up new possibilities for ephemeral message responses, modals,
18+
and more within custom commands.
19+
20+
## Interaction Lifetime
21+
22+
An interaction's lifetime starts with the initial *interaction* with an *interactive element*.
23+
24+
1. A server member clicks on a *button*, uses a *menu*, or submits a *modal* after filling it out.
25+
2. This interaction is sent to YAGPDB, and becomes available to trigger any custom commands which match it.
26+
3. Within the triggered custom command(s), YAGPDB should then *respond* once to the interaction, sending a message,
27+
updating the triggering message, or sending a modal. This may only be done within the CC which was triggered by the
28+
interaction.
29+
4. (optional) Continue to send followup responses for up to 15 minutes until the interaction token expires.
30+
31+
```mermaid
32+
graph LR;
33+
A[Button pressed] --> B{CC Triggered}
34+
C[Menu used] --> B
35+
D[Modal submitted] --> B
36+
B --> E[Bot sends message response]
37+
B --> G[Bot sends modal response]
38+
B --> H[Bot updates message]
39+
E -.-> F(Bot sends followups)
40+
G -.-> F
41+
H -.-> F
42+
```
43+
44+
## Definitions
45+
46+
On the following pages, we will the listed terms with their respective definitions, which are also used by the Discord
47+
API.
48+
49+
Interaction
50+
: A user engaging with YAGPDB through one of Discord's builtin features: Clicking a button, Making a
51+
selection with a select menu, or Submitting a modal.
52+
53+
Response
54+
: YAGPDB is required to respond promptly after receiving an interaction by either sending a message or modal, or by
55+
updating the message on which the interaction was triggered. If it does not do this, the user triggering the interaction
56+
will see a "This application did not respond" error. The bot cannot respond to an interaction more than once.
57+
58+
Followup
59+
: Since YAGPDB may only *respond* to an *interaction* once, it is subsequently required to send an interaction
60+
followup if it still needs to interface with the interaction. These followups can be sent up to 15 minutes after the initial
61+
interaction, and you can send as many as you want. YAGPDB may only send a followup in one of the following ways: Sending
62+
a followup message, editing an initial response or previous followup message, or getting an initial response or previous
63+
followup message.
64+
65+
Interactive Elements
66+
: Elements users can interact with to send *interactions*, i.e. buttons, menus, and modals.
67+
68+
Message Components
69+
: *Interactive Elements* which can be attached to YAGPDB's Discord messages, i.e. buttons and menus.
70+
71+
Button
72+
: A button appearing in or under a Discord message sent by YAGPDB. You can create and customize these
73+
buttons' appearance and behavior with color, emoji, label text, etc. When a button is clicked, an *interaction* is sent
74+
to the bot.
75+
76+
Menu
77+
: A dropdown select menu appearing in or under a Discord message sent by YAGPDB. You can create and customize these
78+
menus' appearance and behavior with placeholder text, predefined options with labels, descriptions, and/or emojis,
79+
designate the entire menu as a user or role select menu instead, etc. When a select menu is used, an *interaction* is
80+
sent to the bot.
81+
82+
Modal
83+
: A pop-up form YAGPDB can send in response to an interaction. It allows users to privately input text which
84+
is sent directly to YAGPDB for use in CC scripting. You can create and customize these modals' appearance and
85+
behavior with a title and fields. YAGPDB can both **receive a submitted modal** (which is an
86+
*interaction*), and **send a modal** for a member to fill out, (which is an interaction *response*).
87+
88+
Ephemeral
89+
: An ephemeral message is sent to a server channel but only appears to a single user. YAGPDB cannot send
90+
these ephemeral messages to users except in response to an *interaction*. Both *response* messages and *followup*
91+
messages can be ephemeral.
Loading
Loading

0 commit comments

Comments
 (0)