Add guide for slash commands #6008

Merged
Kowlin merged 8 commits from pr/docs/cc-slash into V3/develop 2023-04-28 00:51:12 +02:00
Kowlin commented 2023-03-30 01:22:22 +02:00 (Migrated from github.com)

Description of the changes

Add a primer guide for Red 3.5 and Slash commands.

Have the changes in this PR been tested?

Tested for buildability, but not spell checked.

### Description of the changes Add a primer guide for Red 3.5 and Slash commands. ### Have the changes in this PR been tested? Tested for buildability, but not spell checked.
Flame442 (Migrated from github.com) requested changes 2023-03-30 06:12:59 +02:00
@ -0,0 +1,333 @@
.. Slash Commands and Interactions
Flame442 (Migrated from github.com) commented 2023-03-30 05:57:28 +02:00
It is also adviced to make yourself familiar with `Application Commands <https://discord.com/developers/docs/interactions/application-commands>`__ from Discord's documentation. 
```suggestion It is also adviced to make yourself familiar with `Application Commands <https://discord.com/developers/docs/interactions/application-commands>`__ from Discord's documentation. ```
Flame442 (Migrated from github.com) commented 2023-03-30 05:59:06 +02:00

I would probably prefer it be made more explicit that this is a command by including the prefix, while still linking to the docs for it. I'm not sure if this will work, but maybe formatted like this example? [p]slash
(Applies to future cases that reference command docs like this)

I would probably prefer it be made more explicit that this is a command by including the prefix, while still linking to the docs for it. I'm not sure if this will work, but maybe formatted like this example? [`[p]slash`](https://google.com) (Applies to future cases that reference command docs like this)
Flame442 (Migrated from github.com) commented 2023-03-30 05:59:18 +02:00
Go ahead and load your cog. Once it is loaded, we will have to enable and sync our slash commands.

At a minimum it needs to explicitly say both steps, though it might be helpful to add more explanation on top of this to walk them through enabling and syncing for the first time.

```suggestion Go ahead and load your cog. Once it is loaded, we will have to enable and sync our slash commands. ``` At a minimum it needs to explicitly say both steps, though it might be helpful to add more explanation on top of this to walk them through enabling and syncing for the first time.
Flame442 (Migrated from github.com) commented 2023-03-30 06:03:30 +02:00
        zoo = app_commands.Group(name="zoo", description="Zoo related commands")

        @zoo.command(name="add", description="Add an animal to the zoo")
        @app_commands.describe(animal="The animal you want to add")
        async def zoo_add(self, interaction: discord.Interaction, animal: str):
            await interaction.response.send_message(f"Added {animal} to the zoo", ephemeral=True)

        @zoo.command(name="remove", description="Remove an animal from the zoo")
        @app_commands.describe(animal="The animal you want to remove")
        async def zoo_remove(self, interaction: discord.Interaction, animal: str):
            await interaction.response.send_message(f"Removed {animal} from the zoo", ephemeral=True)

Keep the variable names consistent with how text command groups are usually done.

```suggestion zoo = app_commands.Group(name="zoo", description="Zoo related commands") @zoo.command(name="add", description="Add an animal to the zoo") @app_commands.describe(animal="The animal you want to add") async def zoo_add(self, interaction: discord.Interaction, animal: str): await interaction.response.send_message(f"Added {animal} to the zoo", ephemeral=True) @zoo.command(name="remove", description="Remove an animal from the zoo") @app_commands.describe(animal="The animal you want to remove") async def zoo_remove(self, interaction: discord.Interaction, animal: str): await interaction.response.send_message(f"Removed {animal} from the zoo", ephemeral=True) ``` Keep the variable names consistent with how text command groups are _usually_ done.
Flame442 (Migrated from github.com) commented 2023-03-30 06:04:40 +02:00

Calling the command the same thing as the argument makes it a little confusing that the name="Desc" in the describe deco applies to the parameter name, not the function name. The previous example doesn't have this issue, so I don't think it's a huge deal, but something to consider for this and following examples.

Calling the command the same thing as the argument makes it a little confusing that the `name="Desc"` in the describe deco applies to the parameter name, not the function name. The previous example doesn't have this issue, so I don't think it's a huge deal, but something to consider for this and following examples.
Flame442 (Migrated from github.com) commented 2023-03-30 06:06:12 +02:00
If you try out the channel command, you will see that it currently accepts any type of channel,
```suggestion If you try out the channel command, you will see that it currently accepts any type of channel, ```
Flame442 (Migrated from github.com) commented 2023-03-30 06:08:15 +02:00
            await ctx.send("Bark", ephemeral=True)

Just to include an example of ephemeral in the hybrids.

```suggestion await ctx.send("Bark", ephemeral=True) ``` Just to include an example of `ephemeral` in the hybrids.
Flame442 (Migrated from github.com) commented 2023-03-30 06:09:22 +02:00

Yeah... this is a little late lol. The other examples don't point out syncing. Like I said above, add a brief explanation of the enable/sync process for the first command, then assume they know it for the rest.

Yeah... this is a little late lol. The other examples don't point out syncing. Like I said above, add a brief explanation of the enable/sync process for the _first_ command, then assume they know it for the rest.
Flame442 (Migrated from github.com) commented 2023-03-30 06:10:17 +02:00

This should either be removed, or rephrased to say that these commands only accept one argument, the contextual user or message that was right clicked, and no more.

This should either be removed, or rephrased to say that these commands only accept _one_ argument, the contextual `user` or `message` that was right clicked, and no more.
Flame442 (Migrated from github.com) commented 2023-03-30 06:11:03 +02:00

Not sure why this is here, outside of a cog class

Not sure why this is here, outside of a cog class
Flame442 (Migrated from github.com) commented 2023-03-30 06:11:37 +02:00

If teardown is here, and not in __init__.py, it needs to be imported. Better yet, it too should be in __init__.py.

If teardown is here, and not in `__init__.py`, it needs to be imported. Better yet, it too should be in `__init__.py`.
Flame442 (Migrated from github.com) commented 2023-03-30 06:11:47 +02:00

MyCog was not defined.

`MyCog` was not defined.
Flame442 (Migrated from github.com) commented 2023-03-30 06:12:08 +02:00

Again, this is late and repeated.

Again, this is late and repeated.
@ -0,0 +66,4 @@
async def hello(self, interaction: discord.Interaction):
await interaction.response.send_message("Hello World!", ephemeral=True)
One of the more useful decorators is the :func:`app.commands.choices` decorator.
Flame442 (Migrated from github.com) commented 2023-03-30 06:00:37 +02:00

I don't think your example uses this decorator, rather it uses the enum method of specifying choices. There's like 3 different ways that all work for specifying choices, this is just one of them.

I don't think your example uses this decorator, rather it uses the `enum` method of specifying choices. There's like 3 different ways that all work for specifying choices, this is just one of them.
Flame442 (Migrated from github.com) requested changes 2023-04-06 17:08:52 +02:00
Flame442 (Migrated from github.com) left a comment

The two remaining unresolved review comments from the last review still need to be addressed, but this otherwise looks good to go.

The two remaining unresolved review comments from the last review still need to be addressed, but this otherwise looks good to go.
Flame442 (Migrated from github.com) reviewed 2023-04-28 00:17:49 +02:00
@ -0,0 +1,333 @@
.. Slash Commands and Interactions
Flame442 (Migrated from github.com) commented 2023-04-28 00:14:19 +02:00

.. code-block:: python

    from redbot.core import commands, app_commands

    class MyCog(commands.Cog):
        def __init__(self, bot):
            self.bot = bot

        @app_commands.command()
        @app_commands.describe(color="The color you want to choose")
        @app_commands.choices(color=[
             app_commands.Choice(name="Red', value="red"),
             app_commands.Choice(name="Blue', value="blue"),
        ])
        async def color(self, interaction: discord.Interaction, color: Color):
            await interaction.response.send_message(f"Your color is {str(color)}", ephemeral=True)

The user will be shown the ``name`` of the choice, and the argument will be passed the
``value`` associated with that choice. 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
user-facing label.

.. code-block:: python

    from redbot.core import commands, app_commands
    from typing import Literal

    class MyCog(commands.Cog):
        def __init__(self, bot):
            self.bot = bot

        @app_commands.command()
        @app_commands.describe(color="The color you want to choose")
        async def color(self, interaction: discord.Interaction, color: Literal["Red", "Blue"]):
            await interaction.response.send_message(f"Your color is {str(color)}", ephemeral=True)

Finally, an ``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``.

.. code-block:: python

    from enum import Enum
    from redbot.core import commands, app_commands

    class Color(Enum):
        Red = "red"
        Blue = "blue"

    class MyCog(commands.Cog):
        def __init__(self, bot):
            self.bot = bot

        @app_commands.command()
        @app_commands.describe(color="The color you want to choose")
        async def color(self, interaction: discord.Interaction, color: Color):
            await interaction.response.send_message(f"Your color is {str(color.value)}", ephemeral=True)

```suggestion .. code-block:: python from redbot.core import commands, app_commands class MyCog(commands.Cog): def __init__(self, bot): self.bot = bot @app_commands.command() @app_commands.describe(color="The color you want to choose") @app_commands.choices(color=[ app_commands.Choice(name="Red', value="red"), app_commands.Choice(name="Blue', value="blue"), ]) async def color(self, interaction: discord.Interaction, color: Color): await interaction.response.send_message(f"Your color is {str(color)}", ephemeral=True) The user will be shown the ``name`` of the choice, and the argument will be passed the ``value`` associated with that choice. 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 user-facing label. .. code-block:: python from redbot.core import commands, app_commands from typing import Literal class MyCog(commands.Cog): def __init__(self, bot): self.bot = bot @app_commands.command() @app_commands.describe(color="The color you want to choose") async def color(self, interaction: discord.Interaction, color: Literal["Red", "Blue"]): await interaction.response.send_message(f"Your color is {str(color)}", ephemeral=True) Finally, an ``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``. .. code-block:: python from enum import Enum from redbot.core import commands, app_commands class Color(Enum): Red = "red" Blue = "blue" class MyCog(commands.Cog): def __init__(self, bot): self.bot = bot @app_commands.command() @app_commands.describe(color="The color you want to choose") async def color(self, interaction: discord.Interaction, color: Color): await interaction.response.send_message(f"Your color is {str(color.value)}", ephemeral=True) ```
Flame442 (Migrated from github.com) commented 2023-04-28 00:17:27 +02:00
        @app_commands.command()
        @app_commands.describe(channel="The channel you want to mention")
        async def mentionchannel(self, interaction: discord.Interaction, channel: discord.TextChannel):
            await interaction.response.send_message(f"That channel is {channel.mention}", ephemeral=True)

        @app_commands.command()
        @app_commands.describe(role="The role you want to mention")
        async def mentionrole(self, interaction: discord.Interaction, role: discord.Role):
            await interaction.response.send_message(f"That role is {role.mention}", ephemeral=True)

        @app_commands.command()
        @app_commands.describe(member="The member you want to mention")
        async def mentionmember(self, interaction: discord.Interaction, member: discord.Member):
            await interaction.response.send_message(f"That member is {member.mention}", ephemeral=True)

If you try out the mentionchannel command, you will see that it currently accepts any type of channel,
however let's say we want to limit this to voice channels only.
We can do so by adjusting our type hint to :class:`discord.VoiceChannel` instead of :class:`discord.TextChannel`.

.. code-block:: python

    import discord

    from redbot.core import commands, app_commands

    class MyCog(commands.Cog):
        def __init__(self, bot):
            self.bot = bot

        @app_commands.command()
        @app_commands.describe(channel="The channel you want to mention")
        async def mentionchannel(self, interaction: discord.Interaction, channel: discord.VoiceChannel):
            await interaction.response.send_message(f"That channel is {channel.mention}", ephemeral=True)

With integer and float arguments, we can also specify a minimum and maximum value.
This can also be done to strings to set a minimum and maximum length.
These limits will be reflected within Discord when the user is filling out the command.

.. code-block:: python

    import discord

    from redbot.core import commands, app_commands

    class MyCog(commands.Cog):
        def __init__(self, bot):
            self.bot = bot

        @app_commands.command()
        @app_commands.describe(number="The number you want to say, max 10")
        async def saynumber(self, interaction: discord.Interaction, number: app_commands.Range[int, None, 10]):
            await interaction.response.send_message(f"Your number is {number}", ephemeral=True)
```suggestion @app_commands.command() @app_commands.describe(channel="The channel you want to mention") async def mentionchannel(self, interaction: discord.Interaction, channel: discord.TextChannel): await interaction.response.send_message(f"That channel is {channel.mention}", ephemeral=True) @app_commands.command() @app_commands.describe(role="The role you want to mention") async def mentionrole(self, interaction: discord.Interaction, role: discord.Role): await interaction.response.send_message(f"That role is {role.mention}", ephemeral=True) @app_commands.command() @app_commands.describe(member="The member you want to mention") async def mentionmember(self, interaction: discord.Interaction, member: discord.Member): await interaction.response.send_message(f"That member is {member.mention}", ephemeral=True) If you try out the mentionchannel command, you will see that it currently accepts any type of channel, however let's say we want to limit this to voice channels only. We can do so by adjusting our type hint to :class:`discord.VoiceChannel` instead of :class:`discord.TextChannel`. .. code-block:: python import discord from redbot.core import commands, app_commands class MyCog(commands.Cog): def __init__(self, bot): self.bot = bot @app_commands.command() @app_commands.describe(channel="The channel you want to mention") async def mentionchannel(self, interaction: discord.Interaction, channel: discord.VoiceChannel): await interaction.response.send_message(f"That channel is {channel.mention}", ephemeral=True) With integer and float arguments, we can also specify a minimum and maximum value. This can also be done to strings to set a minimum and maximum length. These limits will be reflected within Discord when the user is filling out the command. .. code-block:: python import discord from redbot.core import commands, app_commands class MyCog(commands.Cog): def __init__(self, bot): self.bot = bot @app_commands.command() @app_commands.describe(number="The number you want to say, max 10") async def saynumber(self, interaction: discord.Interaction, number: app_commands.Range[int, None, 10]): await interaction.response.send_message(f"Your number is {number}", ephemeral=True) ```
Flame442 (Migrated from github.com) approved these changes 2023-04-28 00:51:04 +02:00
Sign in to join this conversation.
No reviewers
No labels
Automated PR
Blocked
Blocked By: Damage Control
Blocked By: Dependency
Blocked By: Other PR
Breaking Change
Category: CI
Category: Cogs - Admin
Category: Cogs - Alias
Category: Cogs - Audio
Category: Cogs - Bank
Category: Cogs - Cleanup
Category: Cogs - CustomCommands
Category: Cogs - Dev
Category: Cogs - Downloader
Category: Cogs - Economy
Category: Cogs - Filter
Category: Cogs - General
Category: Cogs - Image
Category: Cogs - Mod
Category: Cogs - Modlog
Category: Cogs - Mutes
Category: Cogs - Permissions
Category: Cogs - Reports
Category: Cogs - Streams
Category: Cogs - Trivia
Category: Cogs - Trivia - Lists
Category: Cogs - Warnings
Category: Core - API - App Commands Package
Category: Core - API - Audio
Category: Core - API - Bank
Category: Core - API - Commands Package
Category: Core - API - Config
Category: Core - API - Other
Category: Core - API - Utils Package
Category: Core - Bot Class
Category: Core - Bot Commands
Category: Core - Command-line Interfaces
Category: Core - Downloader
Category: Core - Help
Category: Core - i18n
Category: Core - Modlog
Category: Core - Other Internals
Category: Core - RPC/ZMQ
Category: Docker
Category: Docs - Changelogs
Category: Docs - For Developers
Category: Docs - Install Guides
Category: Docs - Other
Category: Docs - User Guides
Category: Meta
Category: RPC/ZMQ methods
Category: Vendored Packages
Changelog Entry: Added
Changelog Entry: Pending
Changelog Entry: Skipped
Closed: Duplicate
Closed: External
Closed: Invalid
Closed: User Error
Closed: Won't Fix
Complex Issue
Docs-only
Feature Branch
Good First Issue
Help Wanted
High Priority
Needs Backport To stable-docs
No Activity
No Repro
QA: Bypassed
QA: Changes Requested
QA: Passed
Release Blocker
Status: Accepted
Status: Frozen
Status: In Progress
Status: Needs Discussion
Status: Needs Info
Status: Needs Triage
Status: PRs Welcome
Type: Bug
Type: Dependency Update
Type: Deprecation
Type: Enhancement
Type: Feature
Type: Informational
Type: Merge
Type: Optimisation
Type: Question
Type: Removal
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
Mrcoolgamer11/Red-DiscordBot!6008
No description provided.