How to add a description for slash command arguments in nextcord?

275 Views Asked by At

I've been trying to add a description for my slash command arguments in nextcord, I can't seem to firgure out how.

This is my code:

@__Bot__.slash_command(description="Retrieve a Quran verse.")
async def hello(interaction: nextcord.Interaction, start: str, verse: str, end: Optional[int]):
    VerseCommand = [(start, verse, end)]
....

This is what I am hoping to achieve. "Page Number"

I tried using from nextcord import Options but it doesn't work.

3

There are 3 best solutions below

0
On BEST ANSWER

There are multiple ways to achieve this.

Option 1 - SlashOption

You can add additional settings for slash command options including the description, choices, and value ranges by setting a default value using the SlashOption class. See the Application Command Docs and more examples.

@__Bot__.slash_command(description="Retrieve a Quran verse.")
async def hello(
    interaction: nextcord.Interaction,
    start: str = nextcord.SlashOption(description="Enter Start")
    verse: str = nextcord.SlashOption(description="Enter Verse"),
    end: Optional[int] = nextcord.SlashOption(description="Enter End"),
):
    VerseCommand = [(start, verse, end)]

Option 2 - Numpy-style, Google-style, or Sphinx-style docstrings

You can find examples of each supported style in PR 480.

Example using Google-style docstrings (replace "Enter start value", "Enter verse value", etc with your descriptions)

@__Bot__.slash_command(description="Retrieve a Quran verse.")
async def hello(interaction: nextcord.Interaction, start: str, verse: str, end: Optional[int]):
    """
    Your command description goes here

    Args:
        interaction: The interaction object
        start: Enter start value
        verse: Enter verse value
        end: Enter end value
    """
    VerseCommand = [(start, verse, end)]

Example using Numpy-style docstrings:

@__Bot__.slash_command(description="Retrieve a Quran verse.")
async def hello(interaction: nextcord.Interaction, start: str, verse: str, end: Optional[int]):
    """
    Your command description goes here

    Parameters
    ----------
    interaction:
        The interaction object
    start:
        Enter start value
    verse:
        Enter verse value
    end:
        Enter end value
    """
    VerseCommand = [(start, verse, end)]

Example using Sphinx-style docstrings:

@__Bot__.slash_command(description="Retrieve a Quran verse.")
async def hello(interaction: nextcord.Interaction, start: str, verse: str, end: Optional[int]):
    """
    Your command description goes here

    :param interaction: The interaction object
    :param start: Enter start value
    :param verse: Enter verse value
    :param end: Enter end value
    """
    VerseCommand = [(start, verse, end)]
2
On

You can use the double quotes to add a description to each argument:

@__Bot__.slash_command(description="Retrieve a Quran verse.")
async def hello(interaction: nextcord.Interaction, start: str, verse: str, end: Optional[int]):
    """
    Parameters
    ----------
    interaction: Interaction
    start: str
        description1
    verse: str
        description2
    end: str
        description3
    """
    VerseCommand = [(start, verse, end)]
0
On

I just did this for my bot. Your example appears to be from a different slash_command because I don't see page as an argument anywhere in your code. Nevertheless, this works straight from the nextcord documentation.

@bot.slash_command(guild_ids=[TESTING_GUILD_ID])
async def hi(interaction: nextcord.Interaction, user: nextcord.Member):
    """Say hi to a user

    Parameters
    ----------
    interaction: Interaction
        The interaction object
    user: nextcord.Member
        The user to say hi to.
    """
    await interaction.response.send_message(
        f"{interaction.user} just said hi to {user.mention}"
    )

Reference: https://docs.nextcord.dev/en/stable/interactions.html#slash-command-options