You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Fixed PartialEmoji usage in create_button
* Include component callbacks in docs, patch some things in component docs.
* Apply suggestions from code review
Co-authored-by: LordOfPolls <[email protected]>
* Add subsections
* Applied suggestions from reviews
Co-authored-by: LordOfPolls <[email protected]>
Copy file name to clipboardExpand all lines: docs/components.rst
+28-5Lines changed: 28 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -45,28 +45,51 @@ Well, in Discord, clicking buttons and using slash commands are called ``interac
45
45
Responding to interactions
46
46
__________________________
47
47
48
-
When responding, you have 2 choices in how you handle interactions. You can either wait for them in the command itself, or listen for them in a global event handler (similar to :meth:`on_slash_command_error`)
48
+
When responding, you have 3 choices in how you handle interactions. You can either wait for them in the command itself, or listen for them in a global event handler (similar to :func:`on_slash_command_error`), or register async function as component callback.
49
49
50
-
Lets go through the most common method first, responding in the event itself. We simply need to :meth:`wait_for` the event, just like you do for reactions. For this we're going to use :func:`wait_for_component() <discord_slash.utils.manage_components>`, and we're going to only wait for events from the action row we just .
51
-
This method will return a :class:`ComponentContext <discord_slash.context.ComponentContext>` object that we can use to respond. For this example, we'll just edit the original message (:meth:`edit_origin() <discord_slash.context.ComponentContext.edit_origin>`)
50
+
Wait_for
51
+
********
52
+
53
+
Lets go through the most common method first, responding in the command itself. We simply need to :func:`wait_for` the event, just like you do for reactions. For this we're going to use :func:`wait_for_component() <discord_slash.utils.manage_components>`, and we're going to only wait for events from the action row we just sent.
54
+
This method will return a :class:`ComponentContext <discord_slash.context.ComponentContext>` object that we can use to respond. For this example, we'll just edit the original message (:meth:`edit_origin() <discord_slash.context.ComponentContext.edit_origin>`, uses same logic as :func:`edit()`)
await button_ctx.edit_origin(content="You pressed a button!")
59
62
60
63
.. 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
61
64
62
-
Next we'll go over the alternative, a global event handler. This works just the same as :meth:`on_slash_command_error` or `on_ready`.
65
+
Global event handler
66
+
********************
67
+
68
+
Next we'll go over the alternative, a global event handler. This works just the same as :func:`on_slash_command_error` or `on_ready`. But note that this code will be triggered on any components interaction.
63
69
64
70
.. code-block:: python
65
71
66
72
@bot.event
67
73
asyncdefon_component(ctx: ComponentContext):
74
+
# you may want to filter or change behaviour based on custom_id or message
68
75
await ctx.edit_origin(content="You pressed a button!")
69
76
77
+
Component callbacks
78
+
********************
79
+
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
81
+
Let's register our callback function via decorator :meth:`component_callback() <discord_slash.client.SlashCommand.component_callback>`, in similar ways to slash commands.
82
+
83
+
.. code-block:: python
84
+
85
+
@slash.component_callback()
86
+
asyncdefhello(button_context: ComponentContext):
87
+
await ctx.edit_origin(content="You pressed a button!")
88
+
89
+
In this example, :func:`hello` will be triggered when you receive interaction event from a component with a `custom_id` set to `"hello"`. Just like slash commands, The callback's `custom_id` defaults to the function name.
90
+
You can also register such callbacks in cogs using :func:`cog_component() <discord_slash.cog_ext>`
91
+
Additionally, component callbacks can be dynamically added, removed or edited - see :class:`SlashCommand <discord_slash.client.SlashCommand>`
0 commit comments