Skip to content

Commit b00b3da

Browse files
LulalabyNzii3pre-commit-ci[bot]MiddledotBobDotCom
authored
docs: New select examples (#1843)
* docs: Add channel select example (#1841) * Create channel_select.py Adds an example for the new channel selects * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * docs: Add role select example (#1842) * Create role_select.py Adds an example of the new role selects * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update role_select.py Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> * chore: Fix sentences * Apply suggestions from code review Co-authored-by: BobDotCom <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: Nziie3 <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Middledot <[email protected]> Co-authored-by: BobDotCom <[email protected]>
1 parent 0c1e611 commit b00b3da

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

examples/views/channel_select.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import discord
2+
3+
# Channel selects (dropdowns) are a new type of select menu/dropdown Discord has added so users can select channels from a dropdown.
4+
5+
# Defines a simple View that allows the user to use the Select menu.
6+
# In this view, we define the channel_select with `discord.ui.channel_select`
7+
# Using the decorator automatically sets `select_type` to `discord.ComponentType.channel_select`.
8+
class DropdownView(discord.ui.View):
9+
@discord.ui.channel_select(
10+
placeholder="Select channels...", min_values=1, max_values=3
11+
) # Users can select a maximum of 3 channels in the dropdown
12+
async def channel_select_dropdown(self, select, interaction):
13+
await interaction.response.send_message(
14+
f"You selected the following channels:"
15+
+ f", ".join(f"{channel.mention}" for channel in select.values)
16+
)
17+
18+
19+
bot = discord.Bot(debug_guilds=[...])
20+
21+
22+
@bot.slash_command()
23+
async def channel_select(ctx: discord.ApplicationContext):
24+
"""Sends a message with our dropdown that contains a channel select."""
25+
26+
# Create the view containing our dropdown
27+
view = DropdownView()
28+
29+
# Sending a message containing our View
30+
await ctx.respond("Select channels:", view=view)
31+
32+
33+
@bot.event
34+
async def on_ready():
35+
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
36+
print("------")
37+
38+
39+
bot.run("TOKEN")

examples/views/role_select.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import discord
2+
3+
# Role selects (dropdowns) are a new type of select menu/dropdown Discord has added so people can select server roles from a dropdown.
4+
5+
# Defines a simple View that allows the user to use the Select menu.
6+
# In this view, we define the role_select with `discord.ui.role_select`
7+
# Using the decorator automatically sets `select_type` to `discord.ComponentType.role_select`.
8+
class DropdownView(discord.ui.View):
9+
@discord.ui.role_select(
10+
placeholder="Select roles...", min_values=1, max_values=3
11+
) # Users can select a maximum of 3 roles in the dropdown
12+
async def role_select_dropdown(self, select, interaction):
13+
await interaction.response.send_message(
14+
f"You selected the following roles:"
15+
+ f", ".join(f"{role.mention}" for role in select.values)
16+
)
17+
18+
19+
bot = discord.Bot(debug_guilds=[...])
20+
21+
22+
@bot.slash_command()
23+
async def role_select(ctx: discord.ApplicationContext):
24+
"""Sends a message with our dropdown that contains a role select."""
25+
26+
# Create the view containing our dropdown
27+
view = DropdownView()
28+
29+
# Sending a message containing our View
30+
await ctx.respond("Select roles:", view=view)
31+
32+
33+
@bot.event
34+
async def on_ready():
35+
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
36+
print("------")
37+
38+
39+
bot.run("TOKEN")

0 commit comments

Comments
 (0)