Skip to content

Commit 9616347

Browse files
authored
Simplify components.rst (#219)
1 parent cfb7706 commit 9616347

File tree

1 file changed

+21
-7
lines changed

1 file changed

+21
-7
lines changed

docs/components.rst

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,17 @@ First lets cover the basics. Discord messages can have *components*, such as but
99
Sending some components
1010
_______________________
1111

12+
.. note:: This will work in both slash commands, and discord.py commands
13+
1214
First we need to create some buttons, lets put them in a list for now. We'll use :meth:`create_button() <discord_slash.utils.manage_components>` to create a green button
1315

1416
.. code-block:: python
1517
16-
from discord_slash.utils import manage_components
18+
from discord_slash.utils.manage_components import create_button, create_actionrow
1719
from discord_slash.model import ButtonStyle
1820
1921
buttons = [
20-
manage_components.create_button(
22+
create_button(
2123
style=ButtonStyle.green,
2224
label="A Green Button"
2325
),
@@ -27,8 +29,7 @@ So we have a button, but where do we use it. Let's create an action row with :fu
2729

2830
.. code-block:: python
2931
30-
[...]
31-
action_row = manage_components.create_actionrow(*buttons)
32+
action_row = create_actionrow(*buttons)
3233
3334
3435
Fantastic, we now have an action row with a green button in it, now lets get it sent in discord
@@ -37,7 +38,18 @@ Fantastic, we now have an action row with a green button in it, now lets get it
3738
3839
await ctx.send("My Message", components=[action_row])
3940
40-
.. note:: This will work in both slash commands, and discord.py commands
41+
42+
And to bring it all together, you could use this:
43+
44+
.. code-block:: python
45+
46+
from discord_slash.utils.manage_components import create_button, create_actionrow
47+
from discord_slash.model import ButtonStyle
48+
49+
await ctx.send("My Message", components=[
50+
create_actionrow(
51+
create_button(style=ButtonStyle.green, label="A Green Button"))
52+
])
4153
4254
Now if you've followed along, you have a green button in discord! But theres a problem, whenever you click it you see that the ``interaction failed``. Why is that?
4355
Well, in Discord, clicking buttons and using slash commands are called ``interactions``, and Discord doesn't know if we've received them or not unless we tell Discord. So how do we do that?
@@ -55,9 +67,11 @@ This method will return a :class:`ComponentContext <discord_slash.context.Compon
5567

5668
.. code-block:: python
5769
70+
from discord_slash.utils.manage_components import wait_for_component
71+
5872
await ctx.send("My Message", components=[action_row])
5973
# note: this will only catch one button press, if you want more, put this in a loop
60-
button_ctx: ComponentContext = await manage_components.wait_for_component(bot, components=action_row)
74+
button_ctx: ComponentContext = await wait_for_component(bot, components=action_row)
6175
await button_ctx.edit_origin(content="You pressed a button!")
6276
6377
.. note:: It's worth being aware that if you handle the event in the command itself, it will not persist reboots. As such when you restart the bot, the interaction will fail
@@ -77,7 +91,7 @@ Next we'll go over the alternative, a global event handler. This works just the
7791
Component callbacks
7892
********************
7993

80-
There is one more method - making a function that'll be component callback - triggered when components in specified messages or with specified custom_ids would be activated
94+
There is one more method - making a function that'll be component callback - triggered when components in a specified message or specific ``custom_id`` are used.
8195
Let's register our callback function via decorator :meth:`component_callback() <discord_slash.client.SlashCommand.component_callback>`, in similar ways to slash commands.
8296

8397
.. code-block:: python

0 commit comments

Comments
 (0)