mirror of
https://github.com/Cog-Creators/Red-DiscordBot.git
synced 2025-11-06 03:08:55 -05:00
Fix interaction docs example code (#6154)
Co-authored-by: Jakub Kuczys <me@jacken.men>
This commit is contained in:
parent
327f2fed50
commit
8dfa5c2a86
@ -248,7 +248,7 @@ intersphinx_mapping = {
|
|||||||
# This allows to create links to d.py docs with
|
# This allows to create links to d.py docs with
|
||||||
# :dpy_docs:`link text <site_name.html>`
|
# :dpy_docs:`link text <site_name.html>`
|
||||||
extlinks = {
|
extlinks = {
|
||||||
"dpy_docs": (f"{dpy_docs_url}/%s", None),
|
"dpy_docs": (f"{dpy_docs_url}%s", None),
|
||||||
"issue": ("https://github.com/Cog-Creators/Red-DiscordBot/issues/%s", "#%s"),
|
"issue": ("https://github.com/Cog-Creators/Red-DiscordBot/issues/%s", "#%s"),
|
||||||
"ghuser": ("https://github.com/%s", "@%s"),
|
"ghuser": ("https://github.com/%s", "@%s"),
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,7 @@ Getting Started
|
|||||||
---------------
|
---------------
|
||||||
|
|
||||||
To start off, we will have to import some additional modules to our cog file.
|
To start off, we will have to import some additional modules to our cog file.
|
||||||
We will be using the :class:`redbot.core.app_commands` module to create our slash commands.
|
We will be using the ``redbot.core.app_commands`` module to create our slash commands.
|
||||||
Once we have imported the module, we can start creating our slash commands in our cog class.
|
Once we have imported the module, we can start creating our slash commands in our cog class.
|
||||||
For this example we will use a basic hello world command.
|
For this example we will use a basic hello world command.
|
||||||
|
|
||||||
@ -49,7 +49,7 @@ Below we will go over some of the different stuff you can do with slash commands
|
|||||||
Decorators
|
Decorators
|
||||||
----------
|
----------
|
||||||
Just like with text commands, we can use decorators to modify the behaviour of our slash commands.
|
Just like with text commands, we can use decorators to modify the behaviour of our slash commands.
|
||||||
For example, we can use the :func:`app_commands.guild_only` decorator to make our slash command only work in guilds.
|
For example, we can use the `app_commands.guild_only() <discord.app_commands.guild_only>` decorator to make our slash command only work in guilds.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
@ -66,7 +66,7 @@ For example, we can use the :func:`app_commands.guild_only` decorator to make ou
|
|||||||
async def hello(self, interaction: discord.Interaction):
|
async def hello(self, interaction: discord.Interaction):
|
||||||
await interaction.response.send_message("Hello World!", ephemeral=True)
|
await interaction.response.send_message("Hello World!", ephemeral=True)
|
||||||
|
|
||||||
One of the more useful decorators is the :func:`app.commands.choices` decorator.
|
One of the more useful decorators is the `app_commands.choices() <discord.app_commands.choices>` decorator.
|
||||||
This decorator allows us to specify a list of choices for a specific argument.
|
This decorator allows us to specify a list of choices for a specific argument.
|
||||||
This is useful for arguments that have a limited number of options.
|
This is useful for arguments that have a limited number of options.
|
||||||
For example, we can use this to create a command that allows us to choose between two different colors.
|
For example, we can use this to create a command that allows us to choose between two different colors.
|
||||||
@ -85,15 +85,16 @@ For example, we can use this to create a command that allows us to choose betwee
|
|||||||
app_commands.Choice(name="Red", value="red"),
|
app_commands.Choice(name="Red", value="red"),
|
||||||
app_commands.Choice(name="Blue", value="blue"),
|
app_commands.Choice(name="Blue", value="blue"),
|
||||||
])
|
])
|
||||||
async def color(self, interaction: discord.Interaction, color: Color):
|
async def color(self, interaction: discord.Interaction, color: app_commands.Choice[str]):
|
||||||
await interaction.response.send_message(f"Your color is {color}", ephemeral=True)
|
await interaction.response.send_message(f"Your color is {color.value}", ephemeral=True)
|
||||||
|
|
||||||
The user will be shown the ``name`` of the choice, and the argument will be passed the
|
The user will be shown the ``name`` of the choice, and the argument will be passed a
|
||||||
``value`` associated with that choice. This allows user-facing names to be prettier than
|
`app_commands.Choice <discord.app_commands.Choice>` object with the ``name`` and ``value`` associated with that choice.
|
||||||
what is actually processed by the command.
|
This allows user-facing names to be prettier than what is actually processed by the command.
|
||||||
|
|
||||||
Alternatively, ``Literal`` can be used if the argument does not need a different
|
Alternatively, ``Literal`` can be used if the argument does not need a different
|
||||||
user-facing label.
|
user-facing label. When done this way, the resulting parameter will be one of
|
||||||
|
the literal values listed.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
@ -109,8 +110,8 @@ user-facing label.
|
|||||||
async def color(self, interaction: discord.Interaction, color: Literal["Red", "Blue"]):
|
async def color(self, interaction: discord.Interaction, color: Literal["Red", "Blue"]):
|
||||||
await interaction.response.send_message(f"Your color is {color}", ephemeral=True)
|
await interaction.response.send_message(f"Your color is {color}", ephemeral=True)
|
||||||
|
|
||||||
Finally, an ``Enum`` subclass can be used to specify choices. When done this way, the
|
Finally, an `enum.Enum` subclass can be used to specify choices. When done this way, the
|
||||||
resulting parameter will be an instance of that enum, rather than the ``value``.
|
resulting parameter will be an instance of that enum, rather than `app_commands.Choice <discord.app_commands.Choice>`.
|
||||||
|
|
||||||
.. code-block:: python
|
.. code-block:: python
|
||||||
|
|
||||||
@ -130,7 +131,7 @@ resulting parameter will be an instance of that enum, rather than the ``value``.
|
|||||||
async def color(self, interaction: discord.Interaction, color: Color):
|
async def color(self, interaction: discord.Interaction, color: Color):
|
||||||
await interaction.response.send_message(f"Your color is {color.value}", ephemeral=True)
|
await interaction.response.send_message(f"Your color is {color.value}", ephemeral=True)
|
||||||
|
|
||||||
Check out the full reference of decorators on Discord.py's documentation `here <https://discordpy.readthedocs.io/en/stable/interactions/api.html#decorators>`__.
|
Check out :dpy_docs:`the full reference of decorators at Discord.py's documentation <interactions/api.html#decorators>`.
|
||||||
|
|
||||||
|
|
||||||
Groups & Subcommands
|
Groups & Subcommands
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user