How would I add spaces in Discord application command names (slash commands)? [nextcord/discord.py]

165 Views Asked by At

I'm trying to create a command "/channellink" that links messages between text channels. The command has three options: "initiate", "terminate" and "list", but I can't seem to figure out how I'd add spaces to the command like "/channellink list". I can't add the options as choice arguments for "/channellink" because each of the 3 options has different choice arguments.

I've seen other bots like Dyno use non-choice app command options with spaces, but I can't seem to figure out how it's done (attempting to add spaces to the command name throws "String value did not match validation regex.").

2

There are 2 best solutions below

0
On BEST ANSWER

Slash commands with spaces are actually subcommands. According to the nextcord documentation, subcommands can be implemented like this:

from nextcord import Interaction, slash_command
from nextcord.ext.commands import Bot
from nextcord.ext.commands import Cog

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

    @slash_command(guild_ids=[...])
    async def channellink(self, interaction: Interaction):
        # Will never be executed as it contains subcommands
        pass
    
    @channellink.subcommand
    async def initiate(self, interaction: Interaction):
        pass
    
    @channellink.subcommand
    async def initiate(self, interaction: Interaction):
        pass

    @channellink.subcommand
    async def list(self, interaction: Interaction):
        pass

def setup(bot: Bot):
    bot.add_cog(MyCog(bot))
0
On

I guess it is not exactly what you are looking for but a super simple way to do it would be like this:

@bot.tree.command(name="channellink", description='select between "initiate", "terminate" and "list"')
async def channellink(interaction: discord.Interaction, type: str):
    if type == "initiate":
        await interaction.response.send_message("Do your initiation")
    if type == "terminate":
        await interaction.response.send_message("Do your termination")
    if type == "list":
        await interaction.response.send_message("Do your list stuff")

You would see it like this:

picture1

picture2

A fancier solution would be to create a select menu where you can select between the 3 sub commands. Thats how I would do it.