-
Notifications
You must be signed in to change notification settings - Fork 187
add component tutorial #212
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
LordOfPolls
merged 8 commits into
interactions-py:components
from
LordOfPolls:components
Jun 18, 2021
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cd063d6
add component tutorial
LordOfPolls 5211c1b
mention that this only handles one press
LordOfPolls 488d585
add link to components in gettingstarted.rst
LordOfPolls 52887db
meth -> func
LordOfPolls 5064d14
meth -> func
LordOfPolls 2c18bd8
comma for easier copying
LordOfPolls 9eec9b8
changes based on feedback
LordOfPolls 7bf8fe8
update responding brief
LordOfPolls File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| How to use components | ||
| ===================== | ||
|
|
||
|
|
||
| So you've seen the new fancy buttons and want to give them a try, but dont know where to start. No problem! | ||
|
|
||
| First lets cover the basics. Discord messages can have *components*, such as buttons, dropdowns etc. These components sit in whats called an "action row". An action row can hold 5 components at a time. Your message can have 5 action rows, so thats a total of 25 components! | ||
|
|
||
| Sending some components | ||
| _______________________ | ||
|
|
||
| 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 | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from discord_slash.utils import manage_components | ||
| from discord_slash.model import ButtonStyle | ||
|
|
||
| buttons = [ | ||
| manage_components.create_button( | ||
| style=ButtonStyle.green, | ||
| label="A Green Button" | ||
| ), | ||
| ] | ||
|
|
||
| So we have a button, but where do we use it. Let's create an action row with :func:`create_actionrow() <discord_slash.utils.manage_components>` and put our buttons in it | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| [...] | ||
| action_row = manage_components.create_actionrow(*buttons) | ||
|
|
||
|
|
||
| Fantastic, we now have an action row with a green button in it, now lets get it sent in discord | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| await ctx.send("My Message", components=[action_row]) | ||
|
|
||
| .. note:: This will work in both slash commands, and discord.py commands | ||
|
|
||
| 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? | ||
| 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? | ||
|
|
||
| Responding to interactions | ||
| __________________________ | ||
|
|
||
| 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`) | ||
|
|
||
| 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 . | ||
| 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>`) | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| await ctx.send("My Message", components=[action_row]) | ||
| # note: this will only catch one button press, if you want more, put this in a loop | ||
| button_ctx: ComponentContext = await manage_components.wait_for_component(bot, action_row) | ||
| await button_ctx.edit_origin(content="You pressed a button!") | ||
|
|
||
| .. 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 | ||
|
|
||
| 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`. | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| @bot.event | ||
| async def on_component(ctx: ComponentContext): | ||
| await button_ctx.edit_origin(content="You pressed a button!") | ||
|
|
||
| But [writer], I dont want to edit the message | ||
| ********************************************* | ||
|
|
||
| Well lucky for you, you don't have to. You can either respond silently, with a thinking animation, or send a whole new message. Take a look here: :class:`ComponentContext <discord_slash.context.ComponentContext>` | ||
LordOfPolls marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| How do i know which button was pressed? | ||
| _______________________________________ | ||
|
|
||
| Each button gets a ``custom_id``, this is a unique identifier of which button is being pressed. You can specify what the ID is when you define your button, if you don't; a random one will be generated. When handling the event, simply check the custom_id, and handle accordingly. | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.