Search code examples
pythondiscorddiscord.pynextcord

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


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.").


Solution

  • 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))