Skip to content

Commit

Permalink
docs: New select examples (#1843)
Browse files Browse the repository at this point in the history
* 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 <71356958+BobDotCom@users.noreply.github.com>

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

Co-authored-by: Nziie3 <106406461+Nzii3@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Middledot <78228142+Middledot@users.noreply.github.com>
Co-authored-by: BobDotCom <71356958+BobDotCom@users.noreply.github.com>
  • Loading branch information
5 people committed Jan 5, 2023
1 parent 0c1e611 commit b00b3da
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
39 changes: 39 additions & 0 deletions examples/views/channel_select.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import discord

# Channel selects (dropdowns) are a new type of select menu/dropdown Discord has added so users can select channels from a dropdown.

# Defines a simple View that allows the user to use the Select menu.
# In this view, we define the channel_select with `discord.ui.channel_select`
# Using the decorator automatically sets `select_type` to `discord.ComponentType.channel_select`.
class DropdownView(discord.ui.View):
@discord.ui.channel_select(
placeholder="Select channels...", min_values=1, max_values=3
) # Users can select a maximum of 3 channels in the dropdown
async def channel_select_dropdown(self, select, interaction):
await interaction.response.send_message(
f"You selected the following channels:"
+ f", ".join(f"{channel.mention}" for channel in select.values)
)


bot = discord.Bot(debug_guilds=[...])


@bot.slash_command()
async def channel_select(ctx: discord.ApplicationContext):
"""Sends a message with our dropdown that contains a channel select."""

# Create the view containing our dropdown
view = DropdownView()

# Sending a message containing our View
await ctx.respond("Select channels:", view=view)


@bot.event
async def on_ready():
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
print("------")


bot.run("TOKEN")
39 changes: 39 additions & 0 deletions examples/views/role_select.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import discord

# Role selects (dropdowns) are a new type of select menu/dropdown Discord has added so people can select server roles from a dropdown.

# Defines a simple View that allows the user to use the Select menu.
# In this view, we define the role_select with `discord.ui.role_select`
# Using the decorator automatically sets `select_type` to `discord.ComponentType.role_select`.
class DropdownView(discord.ui.View):
@discord.ui.role_select(
placeholder="Select roles...", min_values=1, max_values=3
) # Users can select a maximum of 3 roles in the dropdown
async def role_select_dropdown(self, select, interaction):
await interaction.response.send_message(
f"You selected the following roles:"
+ f", ".join(f"{role.mention}" for role in select.values)
)


bot = discord.Bot(debug_guilds=[...])


@bot.slash_command()
async def role_select(ctx: discord.ApplicationContext):
"""Sends a message with our dropdown that contains a role select."""

# Create the view containing our dropdown
view = DropdownView()

# Sending a message containing our View
await ctx.respond("Select roles:", view=view)


@bot.event
async def on_ready():
print(f"Logged in as {bot.user} (ID: {bot.user.id})")
print("------")


bot.run("TOKEN")

0 comments on commit b00b3da

Please sign in to comment.