Home > Blockchain >  (Discord.js) Is there any way to add a string input after/in a subcommand?
(Discord.js) Is there any way to add a string input after/in a subcommand?

Time:12-06

Basically, I recently got into making discord bots and I have an issue on adding string inputs after a subcommand. My bot has a 'blacklist' function that will allow a user to add/delete/view the contents of the blacklisted words list.

Here is the syntax of how I wanted it to be if that'd help
add a word: /blacklist add *word*
remove word: /blacklist remove *word*
view list: /blacklist list


When I added a main command 'blacklist' and added sub-commands for 'add', 'remove' and 'list', I was given an error after I added a string input field underneath.

At first I tried:
The error I got was
DiscordAPIError[50035]: Invalid Form Body
options[3][APPLICATION_COMMAND_OPTIONS_TYPE_INVALID]: Sub-command and sub-command group option types are mutually exclusive to all other types

const { SlashCommandBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('blacklist')
        .setDescription('Add, remove or view the list of blacklisted words!')
        .addSubcommand(subcommand =>
            subcommand
                .setName('add')
                .setDescription('add a word to the blacklist list'))
        .addSubcommand(subcommand =>
            subcommand
                .setName('remove')
                .setDescription('remove a word to the blacklist list')) 
        .addSubcommand(subcommand =>
            subcommand
                .setName('list')
                .setDescription('view the blacklist list'))

        .addStringOption(option =>
            option.setName('input')
                .setDescription('The word you want to add/delete (leave empty if list)')),
        
    async execute(interaction) {
        await interaction.reply(interaction.options.getString("input")})
    },
};

I then tried to add the string input inside each of the sub-commands, but unfortunately I got the same result.

const { SlashCommandBuilder } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('blacklist')
        .setDescription('Add, remove or view the list of blacklisted words!')

        .addSubcommand(subcommand =>
            subcommand
                .setName('add')
                .setDescription('add a word to the blacklist list'))
                .addStringOption(option =>
                    option.setName('adds')
                        .setDescription('The word you want to add'))
        .addSubcommand(subcommand =>
            subcommand
                .setName('remove')
                .setDescription('remove a word to the blacklist list'))
                .addStringOption(option =>
                    option.setName('del')
                        .setDescription('The word you want to remove'))
        .addSubcommand(subcommand =>
            subcommand
                .setName('list')
                .setDescription('view the blacklist list')),
    async execute(interaction) {
        await interaction.reply(interaction.option.getString("input"))
    },
};

DiscordAPIError[50035]: Invalid Form Body
options[1][APPLICATION_COMMAND_OPTIONS_TYPE_INVALID]: Sub-command and sub-command group option types are mutually exclusive to all other types
options[3][APPLICATION_COMMAND_OPTIONS_TYPE_INVALID]: Sub-command and sub-command group option types are mutually exclusive to all other types

Also I am not sure why it said there was an issue with the first and third, as opposed to first and second

I understand why the code does not work, as said by the error message, but is there a way to go around it? And why can't we add other option types?

CodePudding user response:

I think your issue is just around where you are closing parentheses. It looks like you're closing the subcommand before adding the option to said subcommand.

module.exports = {
  data: new SlashCommandBuilder()
    .setName("blacklist")
    .setDescription("Add, remove or view the list of blacklisted words!")
    .addSubcommand((subcommand) =>
      subcommand
        .setName("add")
        .setDescription("add a word to the blacklist list")
        .addStringOption((option) =>
          option.setName("word").setDescription("The word you want to add")
        )
    )
    .addSubcommand((subcommand) =>
      subcommand
        .setName("remove")
        .setDescription("remove a word to the blacklist list")
        .addStringOption((option) =>
          option.setName("word").setDescription("The word you want to remove")
        )
    )
    .addSubcommand((subcommand) =>
      subcommand.setName("list").setDescription("view the blacklist list")
    ),
  async execute(interaction) {
    await interaction.reply(interaction.options.getString("word"));
  },
};

CodePudding user response:

I would not use subcommand in this rather I would use the Choises for easier coding:

const { SlashCommandBuilder } = require("discord.js");

module.exports = {
    data: new SlashCommandBuilder()
        .setName("blacklist")
        .setDescription("Add, remove or view the list of blacklisted words!")
        .addStringOption((option) =>
            option
                .setName("list")
                .setDescription("Add, remove or view the list of blacklisted words!")
                .setRequired(true)
                .addChoices(
                    { name: "add a word to the blacklist list", value: "add" },
                    { name: "remove a word to the blacklist list", value: "remove" },
                    { name: "From which list should the user be removed?", value: "list" }
                )
        )
        .addStringOption((option) =>
            option
                .setName("input")
                .setDescription("The word you want to add/delete (leave empty if list)")
        ),

    async execute(interaction) {
        let list = interaction.options.getString("list");
    //do / return whatever you want 
        await interaction.reply(interaction.options.getString("input"));
    },
};
  • Related