javascript 具有特定目的地嵌入式的公交车报告系统

efzxgjgh  于 2023-08-02  发布在  Java
关注(0)|答案(1)|浏览(79)

我正在开发一个Discord机器人,它允许用户提交公交报告,我需要一些交互过程的帮助。我们的目标是拥有一个动态系统,用户可以从第一个下拉菜单中选择不同的目的地,然后从第二个下拉菜单中选择司机的数量。根据他们的选择,机器人应该发送一个相应的嵌入与选定的目的地和驱动程序的数量。

const { SlashCommandBuilder, EmbedBuilder, ButtonBuilder, ActionRowBuilder, SelectMenuBuilder  } = require('discord.js');

module.exports = {
  data: new SlashCommandBuilder()
    .setName('bus-test')
    .setDescription('Just testing')
    .addAttachmentOption(option => option.setName('screenshot').setDescription('Upload a screenshot').setRequired(true))
    .addStringOption(option => option.setName('other_drivers').setDescription('Enter all drivers with @').setRequired(true))
    .addStringOption(option => option.setName('passengers').setDescription('Enter all passengers with @').setRequired(true)),

  async execute(interaction) {
    const author = interaction.user;
    const drivers = interaction.options.getString('other_drivers');
    const passengers = interaction.options.getString('passengers');
    const attachment = interaction.options.get('screenshot').value;

    const passengersWithDots = passengers.split(' ').map(passenger => `• ${passenger}`).join('\n');
    const driversWithDots = drivers.split(' ').map(driver => `• ${driver}`).join('\n');
    const driversWithMention = `• ${author}\n${driversWithDots}`;

    const embed = new EmbedBuilder()
      .setTitle('**Report System Rules**')
      // .setColor('YELLOW')
      .setDescription(`${driversWithMention}\n\nPassenger(s)\n${passengersWithDots}`)
      .setImage(attachment.url);

    const destinationmenu = new SelectMenuBuilder()
      .setCustomId('destination_menu')
      .setPlaceholder('Select destination')
      .addOptions([
        {
          label: 'Option 1',
          value: 'option_1',
        },
        {
          label: 'Option 2',
          value: 'option_2',
        },
      ]);

    const drivermenu = new SelectMenuBuilder()
      .setCustomId('drivernum_menu')
      .setPlaceholder('Select number of drivers')
      .addOptions([
        {
          label: '1 driver(s)',
          value: 'driver_num_1',
        },
        {
          label: '2 driver(s)',
          value: 'driver_num_2',
        },
        {
          label: '3 driver(s)',
          value: 'driver_num_3',
        },
        {
          label: '4 driver(s)',
          value: 'driver_num_4',
        },
        {
          label: '5 driver(s)',
          value: 'driver_num_5',
        },
        {
          label: '6 driver(s)',
          value: 'driver_num_6',
        },
      ]);

    const Preview = new ButtonBuilder()
      .setCustomId('Preview_button')
      .setLabel('Preview')
      .setStyle('Primary');

    const Cancel = new ButtonBuilder()
      .setCustomId('Cancel_button')
      .setLabel('Cancel')
      .setStyle('Danger');

    const row1 = new ActionRowBuilder().addComponents(destinationmenu);
    const row2 = new ActionRowBuilder().addComponents(drivermenu);
    const row3 = new ActionRowBuilder().addComponents(Cancel, Preview);

    const reply = await interaction.reply({
      embeds: [embed],
      ephemeral: true,
      components: [row1, row2, row3],
    });

    const message = interaction.isMessageComponent() ? interaction.message : reply;
    const collector = message.createMessageComponentCollector({ time: 30000 });

    collector.on('collect', async (buttonInteraction) => {
      if (buttonInteraction.user.id === interaction.user.id) {
        if (buttonInteraction.customId === 'Preview_button') {
          if (buttonInteraction.values && buttonInteraction.values.length >= 2) {
            const selectedDestination = buttonInteraction.values[0];
            const selectedDriverNum = buttonInteraction.values[1];
    
            // Check which option was selected and create a corresponding embed
            let newEmbed;
            if (selectedDestination === 'option_1' || selectedDestination === 'option_2') {
              // Get the label of the selected driver number option
              const selectedDriverLabel = driverMenu.options.find(option => option.value === selectedDriverNum).label;    
            newEmbed = new MessageEmbed()
              .setTitle('Preview')
              .setColor('GREEN')
              .setDescription(`Selected Destination: ${selectedDestination}\nSelected Driver(s): ${selectedDriverLabel}`)
              .setImage(attachment.url);
          } else {
            // Handle the case if no valid option was selected
            newEmbed = new MessageEmbed()
              .setTitle('Invalid Selection')
              .setDescription('Please select a valid option.')
              .setColor('RED');
          }

          // Update the original embed with the new one
          await buttonInteraction.update({
            embeds: [newEmbed],
            components: [],
          });
        } else if (buttonInteraction.customId === 'Cancel_button') {
          await buttonInteraction.update({
            content: 'Report canceled.',
            embeds: [],
            components: [],
          });
        }
      }
    }});

    collector.on('end', async () => {
      // Remove the original embed and components after the collector ends
      await message.edit({
        content: 'This report is no longer active.',
        embeds: [],
        components: [],
      });
    });
  },
};

字符串
我需要的帮助是更新destinationMenu以包含其他目的地选项。当用户选择一个特定的目的地时,我希望机器人显示一个嵌入,其中包括选定的目的地和他们以前从driverMenu中选择的驱动程序的数量。例如,如果用户从目标菜单中选择“选项1”,从驱动程序菜单中选择“2个驱动程序”,则机器人应该发送如下嵌入:每个目的地都有它自己的嵌入与自己的图像在嵌入但相同的是在所有嵌入的驱动程序编号一样

Selected Destination: Option 1
Selected Driver(s): 2 driver(s)


如果有人能在这个动态交互过程中帮助我,我将不胜感激。谢谢你,谢谢

8yparm6h

8yparm6h1#

请注意以下事项:

1.ButtonInteractions没有values!了解更多
1.确保在collect事件中添加ComponentType检查
1.请使用正确的名称约定

  1. SelectMenuBuilder已弃用,请改用StringSelectMenuBuilder
  2. MessageEmbed是一个discord.js v13类,您的代码大部分是v14,因此请使用EmbedBuilder

修复

const { ComponentType } = require("discord.js");
// ...
collector.on("collect", async (interaction) => {
    if (interaction.type === ComponentType.Button) {
        // handle button click events here
    } else if (interaction.type === ComponentType.StringSelect) {
        // handle string select menu events here
    }
})

字符串

  • 其他变更:
// change this
newEmbed = new MessageEmbed()
// to this
newEmbed = new EmbedBuilder()

// and this
const drivermenu = new SelectMenuBuilder()
// to this
const drivermenu = new StringSelectMenuBuilder()

相关问题