【问题标题】:(Discord.js) Is there any way to add a string input after/in a subcommand?(Discord.js)有没有办法在子命令之后/中添加字符串输入?
【发布时间】:2022-12-18 15:34:17
【问题描述】:

基本上,我最近开始制作不和谐的机器人,并且在子命令后添加字符串输入时遇到问题。我的机器人有一个“黑名单”功能,允许用户添加/删除/查看列入黑名单的单词列表的内容。

如果有帮助的话,这是我希望它成为的语法
加一句话:/blacklist add *word*
删除单词:/blacklist remove *word*
查看列表:/blacklist list


当我添加一个主命令“黑名单”并添加“添加”、“删除”和“列表”的子命令时,在下面添加一个字符串输入字段后出现错误。

起初我试过:
我得到的错误是
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")})
    },
};

然后我尝试在每个子命令中添加字符串输入,但不幸的是我得到了相同的结果。

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

我也不确定为什么它说第一个和第三个有问题,而不是第一个和第二个

正如错误消息所述,我明白为什么代码不起作用,但有没有办法解决这个问题?为什么我们不能添加其他选项类型?

【问题讨论】:

    标签: node.js discord discord.js


    【解决方案1】:

    我认为你的问题就在你关闭括号的地方。看起来您在将选项添加到所述子命令之前关闭了子命令。

    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"));
      },
    };
    

    【讨论】:

      【解决方案2】:
      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('input')
                      .setDescription('The word you want to add'))
              )
              .addSubcommand(subcommand =>
                  subcommand
                  .setName('remove')
                  .setDescription('remove a word to the blacklist list')
                  .addStringOption(option =>
                      option.setName('input')
                      .setDescription('The word you want to remove'))
              )
              .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)'))
              ), // the addStringOption() should be inside addSubcommand()
          async execute(interaction) {
              // you should start a subcommand like this
              switch (interaction.options.getSubcommand()) {
                  case "add":
                  default:
                      {
                          // and then define its options like you would in a normal command . e.x. :
                          const input = interaction.options.getString('input');
                          interaction.reply({ content: input });
                          break;
                      }
      
                  case "remove":
                      {
                          // and then define its options like you would in a normal command . e.x. :
                          const input = interaction.options.getString('input');
                          interaction.reply({ content: input });
                          break;
      
                      }
      
                  case "list":
                      {
                          // and then define its options like you would in a normal command . e.x. :
                          const input = interaction.options.getString('input');
                          interaction.reply({ content: input });
                          break;
      
                      }
      
      
              }
      
      
          },
      };
      ``` I think u can do it now ;)
      

      【讨论】:

        【解决方案3】:

        我不会在这里使用子命令,而是使用 Choices 来简化编码:

        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"));
            },
        };
        

        【讨论】:

          猜你喜欢
          • 2018-11-25
          • 1970-01-01
          • 2020-04-17
          • 2022-08-18
          • 2021-08-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-06
          相关资源
          最近更新 更多