Search code examples
javascriptarraysdiscorddiscord.js

How can I call the options of a SubCommand using discord.js? (v13)


I've recently been working on a Discord bot using discord.js's lastest verison, v13, and slash commands. After reading through the documentation on GitHub, I was able to get a subcommand working in my bot:

enter image description here

However, when trying to get the options array from the arguments, I do not know how I can get arguments after getting the SubCommand. To my disappointment, the only function I found was .getSubcommand(), which returns a string oddly enough. I was hoping it would return on object so I can get the options array from it.

How can I get the options object that's within a subcommand? Does this even exist? How can I use the string that is returned?

Thanks!


Solution

  • SubCommands are just subject to the slash command itself you may access the arguments of options provided within it like so:

    client.on('interactionCreate', async (interaction) => {
        if (!interaction.isCommand()) return;
    
        if (interaction.commandName === 'settings') {
            if (interaction.options.getSubcommand() === 'moderation_logs') {
                const user = interaction.options.getChannel('logging_channel');
            }
        }
    });
    

    Treat them as normal message commands with args but better since all args correspond to the options provided even if they are different sub commands since they fall under the same interaction ( slash command ).

    Slash commands are awesome 😇!