From df17f4b03ec96e4b81f602605453941cde9f48c8 Mon Sep 17 00:00:00 2001 From: aLEGEND21 <77472111+aLEGEND21@users.noreply.github.com> Date: Wed, 3 May 2023 20:48:17 +0000 Subject: [PATCH 1/4] docs: Document select types and select channel types --- docs/interactions/ui-components/dropdowns.mdx | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/docs/interactions/ui-components/dropdowns.mdx b/docs/interactions/ui-components/dropdowns.mdx index 4a180183..b4cd5e7e 100644 --- a/docs/interactions/ui-components/dropdowns.mdx +++ b/docs/interactions/ui-components/dropdowns.mdx @@ -81,6 +81,8 @@ about making your code do amazing things, while Pycord handles the rest! #### Select Menu Properties +- `select_type`: The type of select to create. This must be a [`discord.ComponentType`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ComponentType) value. +- `channel_types`: A list of channel types that can be selected in the menu. This is only valid for selects of `select_type` [`discord.ComponentType.channel_select`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ComponentType). - `options`\*: A list of [`discord.SelectOption`](https://docs.pycord.dev/en/stable/api/data_classes.html#discord.SelectOption) values. These are the options that can be selected in this menu. - `placeholder` is the placeholder text shown in the select menu if no option is selected. - `custom_id`: The ID of the select menu that gets received during an interaction. It is recommended not to set this to anything unless you are trying to create a persistent view. @@ -100,6 +102,39 @@ In the `options` parameter, you pass a list of [`discord.SelectOption`](https:// - `label` (the name displayed to users, can be up to 100 characters) - `value` (a special value of the option, defaults to the label). +## Select Types + +In addition to regular string selects, you can also have your select menu contain users, roles, mentionables, and channels as its options. You can use these alternative select types by passing a [`discord.ComponentType`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ComponentType) value for the `select_type` parameter when creating the Select. + +:::info Viewing the selected option + +When accessing the value the user selected through `select.values[0]`, you will only be able to retrieve the ID of the user, role, mentionable, or channel if you are not using a [`discord.ComponentType.string_select`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ComponentType.string_select). + +::: + +```python +class MyView(discord.ui.View): + @discord.ui.select( + select_type=discord.ComponentType.user_select + ) + async def select_callback(self, select, interaction): + await interaction.response.send_message(f"Hello, <@{select.values[0]}>") +``` + +### Specifying Channel Types + +When using a [`discord.ComponentType.channel_select`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ComponentType.channel_select) type select menu, you can pass in a list of [`discord.ChannelType`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ChannelType) values to limit which types of channels users can choose when interacting with the select menu. + +```python +class MyView(discord.ui.View): + @discord.ui.select( + select_type=discord.ComponentType.channel_select, + channel_types=[discord.ChannelType.text, discord.ChannelType.voice] + ) + async def select_callback(self, select, interaction): + await interaction.response.send_message(f"You selected <#{select.values[0]}>") +``` + ## Action Rows We have discussed that Views can have 5 rows. Each row has 5 slots. A button takes a single slot, while a select menu takes up all 5 of them. This means a view can have a maximum of 5 select menus, or any possible combination of select menus and buttons. From 51adadf865be042b38d282da8f45049a603d66b8 Mon Sep 17 00:00:00 2001 From: aLEGEND21 <77472111+aLEGEND21@users.noreply.github.com> Date: Thu, 4 May 2023 22:45:25 +0000 Subject: [PATCH 2/4] fix: Remove inaccurate info about select.values --- docs/interactions/ui-components/dropdowns.mdx | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/docs/interactions/ui-components/dropdowns.mdx b/docs/interactions/ui-components/dropdowns.mdx index b4cd5e7e..42bedb8d 100644 --- a/docs/interactions/ui-components/dropdowns.mdx +++ b/docs/interactions/ui-components/dropdowns.mdx @@ -83,7 +83,7 @@ about making your code do amazing things, while Pycord handles the rest! - `select_type`: The type of select to create. This must be a [`discord.ComponentType`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ComponentType) value. - `channel_types`: A list of channel types that can be selected in the menu. This is only valid for selects of `select_type` [`discord.ComponentType.channel_select`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ComponentType). -- `options`\*: A list of [`discord.SelectOption`](https://docs.pycord.dev/en/stable/api/data_classes.html#discord.SelectOption) values. These are the options that can be selected in this menu. +- `options`: A list of [`discord.SelectOption`](https://docs.pycord.dev/en/stable/api/data_classes.html#discord.SelectOption) values. These are the options that can be selected in this menu. - `placeholder` is the placeholder text shown in the select menu if no option is selected. - `custom_id`: The ID of the select menu that gets received during an interaction. It is recommended not to set this to anything unless you are trying to create a persistent view. - `row`: The relative row this select menu belongs to. A Discord component can only have 5 rows. By default, items are arranged automatically into those 5 rows. If you’d like to control the relative positioning of the row then passing an index is advised. For example, row=1 will show up before row=2. Defaults to None, which is automatic ordering. The row number must be between 0 and 4 (i.e. zero indexed). @@ -91,8 +91,6 @@ about making your code do amazing things, while Pycord handles the rest! - `max_values`: The maximum number of items that must be chosen for this select menu. Defaults to 1 and must be between 1 and 25. - `disabled`: Whether the select is disabled or not. Defaults to False. -\* means that the parameter is required. - #### Select Option Properties In the `options` parameter, you pass a list of [`discord.SelectOption`](https://docs.pycord.dev/en/stable/api/data_classes.html#discord.SelectOption) values. This class also has a few parameters: @@ -106,12 +104,6 @@ In the `options` parameter, you pass a list of [`discord.SelectOption`](https:// In addition to regular string selects, you can also have your select menu contain users, roles, mentionables, and channels as its options. You can use these alternative select types by passing a [`discord.ComponentType`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ComponentType) value for the `select_type` parameter when creating the Select. -:::info Viewing the selected option - -When accessing the value the user selected through `select.values[0]`, you will only be able to retrieve the ID of the user, role, mentionable, or channel if you are not using a [`discord.ComponentType.string_select`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ComponentType.string_select). - -::: - ```python class MyView(discord.ui.View): @discord.ui.select( From 81fc10e0d4ce57b211e5ef52248dbdbade389283 Mon Sep 17 00:00:00 2001 From: aLEGEND21 <77472111+aLEGEND21@users.noreply.github.com> Date: Thu, 4 May 2023 22:51:46 +0000 Subject: [PATCH 3/4] feat: Add shortcut decorators for select menus --- docs/interactions/ui-components/dropdowns.mdx | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/docs/interactions/ui-components/dropdowns.mdx b/docs/interactions/ui-components/dropdowns.mdx index 42bedb8d..3a8b8587 100644 --- a/docs/interactions/ui-components/dropdowns.mdx +++ b/docs/interactions/ui-components/dropdowns.mdx @@ -113,6 +113,15 @@ class MyView(discord.ui.View): await interaction.response.send_message(f"Hello, <@{select.values[0]}>") ``` +Additionally, you can use shortcut decorators to create a [`discord.ui.Select`](https://docs.pycord.dev/en/master/api/ui_kit.html#discord.ui.select) with a predetermined [`discord.ComponentType`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ComponentType) value. Using a shortcut decorator, the above code can be rewritten like this: + +```python +class MyView(discord.ui.View): + @discord.ui.user_select() + async def select_callback(self, select, interaction): + await interaction.response.send_message(f"Hello, <@{select.values[0]}>") +``` + ### Specifying Channel Types When using a [`discord.ComponentType.channel_select`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ComponentType.channel_select) type select menu, you can pass in a list of [`discord.ChannelType`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ChannelType) values to limit which types of channels users can choose when interacting with the select menu. From e08e188055e17baa79b28f92ab791e1cfe48e12d Mon Sep 17 00:00:00 2001 From: aLEGEND21 <77472111+aLEGEND21@users.noreply.github.com> Date: Thu, 4 May 2023 22:53:15 +0000 Subject: [PATCH 4/4] fix: Update code to use `.mention` on select.values[0] instead of using a raw mention --- docs/interactions/ui-components/dropdowns.mdx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/interactions/ui-components/dropdowns.mdx b/docs/interactions/ui-components/dropdowns.mdx index 3a8b8587..7adb6e37 100644 --- a/docs/interactions/ui-components/dropdowns.mdx +++ b/docs/interactions/ui-components/dropdowns.mdx @@ -110,7 +110,7 @@ class MyView(discord.ui.View): select_type=discord.ComponentType.user_select ) async def select_callback(self, select, interaction): - await interaction.response.send_message(f"Hello, <@{select.values[0]}>") + await interaction.response.send_message(f"Hello, {select.values[0].mention}") ``` Additionally, you can use shortcut decorators to create a [`discord.ui.Select`](https://docs.pycord.dev/en/master/api/ui_kit.html#discord.ui.select) with a predetermined [`discord.ComponentType`](https://docs.pycord.dev/en/stable/api/enums.html#discord.ComponentType) value. Using a shortcut decorator, the above code can be rewritten like this: @@ -119,7 +119,7 @@ Additionally, you can use shortcut decorators to create a [`discord.ui.Select`]( class MyView(discord.ui.View): @discord.ui.user_select() async def select_callback(self, select, interaction): - await interaction.response.send_message(f"Hello, <@{select.values[0]}>") + await interaction.response.send_message(f"Hello, {select.values[0].mention}") ``` ### Specifying Channel Types @@ -133,7 +133,7 @@ class MyView(discord.ui.View): channel_types=[discord.ChannelType.text, discord.ChannelType.voice] ) async def select_callback(self, select, interaction): - await interaction.response.send_message(f"You selected <#{select.values[0]}>") + await interaction.response.send_message(f"You selected {select.values[0].mention}") ``` ## Action Rows