简体   繁体   English

在 DiscordJS v14 中创建动态 StringSelectMenu

[英]Creating a dynamic StringSelectMenu in DiscordJS v14

I want to create a dynamic StringSelectMenu that is being generated when a specific command is used.我想创建一个在使用特定命令时生成的动态 StringSelectMenu。 Is there a way to do this?有没有办法做到这一点? Because I am getting the problem at the.addOptions() of the StringSelectMenuBuilder().因为我在 StringSelectMenuBuilder() 的 .addOptions() 中遇到了问题。

Relevant Code (Looping trough the Database entries (at the moment just 1)) The relevant object is stringselect, that should create the input for the.addOptions()相关代码(循环遍历数据库条目(目前只有 1 个))相关的 object 是 stringselect,应该为 the.addOptions() 创建输入

                console.log(result);
                if (result) {
                    let string = "";
                    let stringselect = "";

                    for (entry in result) {
                        id = result[entry].id;
                        name = result[entry].name;
                        money1 = result[entry].money1;
                        money2 = result[entry].money2;
                        amount = result[entry].amount;

                        string += `${name} (Item id = ${id}). The price is ${money1} Coin(s), ${money2} Diamond(s). There are ${amount} availible of this item.

`;
                        stringselect += `{label: "${name}",description: "Of the item ${name1} with the id ${id} are ${amount} availible. The price is ${money1} Coin(s), ${money2} Diamond(s).",value: ${id}},`;
                    }

This is the code of the SelectMenu creation这是 SelectMenu 创建的代码

                    const select = new ActionRowBuilder().addComponents(
                        new StringSelectMenuBuilder()
                            .setCustomId("reward-buy")
                            .setPlaceholder("Nothing selected")
                            .addOptions(stringselect)
                    );

I have tried different techniques, with JSON, other formats and everything but I am stuck at the error that it is missing the requried properties.我尝试了不同的技术,使用 JSON、其他格式和所有内容,但我被错误困住了,因为它缺少所需的属性。 I do not know and did not find an answer how to create the options input dynamically.我不知道也没有找到如何动态创建选项输入的答案。 I would not need the specific code to finish it, just how I should pass the values.我不需要特定的代码来完成它,只需要我应该如何传递值。

The error is:错误是:

CombinedPropertyError: Received one or more errors
    at ObjectValidator.handleIgnoreStrategy (/home/test/test/node_modules/@sapphire/shapeshift/dist/index.js:1212:70)
    at ObjectValidator.handleStrategy (/home/test/test/node_modules/@sapphire/shapeshift/dist/index.js:1068:47)
    at ObjectValidator.handle (/home/test/test/node_modules/@sapphire/shapeshift/dist/index.js:1165:17)
    at ObjectValidator.parse (/home/test/test/node_modules/@sapphire/shapeshift/dist/index.js:106:88)
    at /home/test/test/node_modules/@discordjs/builders/dist/index.js:654:134
    at Array.map (<anonymous>)
    at StringSelectMenuBuilder.addOptions (/home/test/test/node_modules/@discordjs/builders/dist/index.js:653:18)
    at StringSelectMenuBuilder.addOptions (/home/test/test/node_modules/discord.js/src/structures/StringSelectMenuBuilder.js:48:18)
    at Query.<anonymous> (/home/test/test/src/commands/reward-shop.js:67:9)
    at Query.<anonymous> (/home/test/test/node_modules/mysql/lib/Connection.js:526:10) {
  errors: [
    [
      'label',
      MissingPropertyError: A required property is missing
          at ObjectValidator.handleIgnoreStrategy (/home/test/test/node_modules/@sapphire/shapeshift/dist/index.js:1187:27)
          at ObjectValidator.handleStrategy (/home/test/test/node_modules/@sapphire/shapeshift/dist/index.js:1068:47)
          at ObjectValidator.handle (/home/test/test/node_modules/@sapphire/shapeshift/dist/index.js:1165:17)
          at ObjectValidator.parse (/home/test/test/node_modules/@sapphire/shapeshift/dist/index.js:106:88)
          at /home/test/test/node_modules/@discordjs/builders/dist/index.js:654:134
          at Array.map (<anonymous>)
          at StringSelectMenuBuilder.addOptions (/home/test/test/node_modules/@discordjs/builders/dist/index.js:653:18)
          at StringSelectMenuBuilder.addOptions (/home/test/test/node_modules/discord.js/src/structures/StringSelectMenuBuilder.js:48:18)
          at Query.<anonymous> (/home/test/test/src/commands/reward-shop.js:67:9)
          at Query.<anonymous> (/home/test/test/node_modules/mysql/lib/Connection.js:526:10) {
        property: 'label'
      }
    ],
    [
      'value',
      MissingPropertyError: A required property is missing
          at ObjectValidator.handleIgnoreStrategy (/home/test/test/node_modules/@sapphire/shapeshift/dist/index.js:1187:27)
          at ObjectValidator.handleStrategy (/home/test/test/node_modules/@sapphire/shapeshift/dist/index.js:1068:47)
          at ObjectValidator.handle (/home/test/test/node_modules/@sapphire/shapeshift/dist/index.js:1165:17)
          at ObjectValidator.parse (/home/test/test/node_modules/@sapphire/shapeshift/dist/index.js:106:88)
          at /home/test/test/node_modules/@discordjs/builders/dist/index.js:654:134
          at Array.map (<anonymous>)
          at StringSelectMenuBuilder.addOptions (/home/test/test/node_modules/@discordjs/builders/dist/index.js:653:18)
          at StringSelectMenuBuilder.addOptions (/home/test/test/node_modules/discord.js/src/structures/StringSelectMenuBuilder.js:48:18)
          at Query.<anonymous> (/home/test/test/src/commands/reward-shop.js:67:9)
          at Query.<anonymous> (/home/test/test/node_modules/mysql/lib/Connection.js:526:10) {
        property: 'value'
      }
    ]
  ]
}

The .addOptions() takes an array, so you'd need to do something along these lines .addOptions()接受一个数组,所以你需要按照这些行做一些事情

if (result) {
    const stringselect = [];

    for (const entry in result) {
        const id = result[entry].id;
        const name = result[entry].name;
        const money1 = result[entry].money1;
        const money2 = result[entry].money2;
        const amount = result[entry].amount;

        stringselect.push({
            label: name,
            description: `Of the item ${name} with the id ${id} are ${amount} availible. The price is ${money1} Coin(s), ${money2} Diamond(s).`,
            value: id.toString(),
        });
    }
    
    const select = new ActionRowBuilder().addComponents(
        new StringSelectMenuBuilder()
            .setCustomId('reward-buy')
            .setPlaceholder('Nothing selected')
            .addOptions(stringselect),
    );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM