Untitled

 avatar
unknown
plain_text
2 years ago
2.6 kB
10
Indexable
const { Client, Intents, MessageActionRow, MessageButton, MessageSelectMenu } = require('discord.js');
const client = new Client({ 
  intents: [
    Intents.FLAGS.GUILDS,
    Intents.FLAGS.GUILD_MESSAGES,
  ]
});
const config = require('./config.json');

client.once('ready', () => {
  console.log(`Logged in as ${client.user.tag}`);
});

client.on('messageCreate', (message) => {
  if (message.content === '!select') {
    const embed = {
      title: 'Select an Option',
      description: 'Click the "Show Menu" button to select an option:',
      color: 0x3498db, // Blue color (you can customize this)
    };

    // Create a "Show Menu" button
    const showMenuButton = new MessageButton()
      .setCustomId('show_menu')
      .setLabel('Show Menu')
      .setStyle('PRIMARY');

    // Add the "Show Menu" button to an action row
    const buttonRow = new MessageActionRow().addComponents(showMenuButton);

    message.channel.send({ embeds: [embed], components: [buttonRow] });
  }
});

client.on('interactionCreate', async (interaction) => {
  if (!interaction.isButton() && !interaction.isSelectMenu()) return;

  if (interaction.customId === 'show_menu') {
    const selectOptions = [
      { label: 'Option 1', value: 'option1' },
      { label: 'Option 2', value: 'option2' },
    ];

    // Create a select menu
    const selectMenu = new MessageSelectMenu()
      .setCustomId('select_menu')
      .setPlaceholder('Select an option')
      .addOptions(selectOptions);

    // Create an action row with the select menu
    const row = new MessageActionRow().addComponents(selectMenu);

    try {
      // Defer the interaction first
      await interaction.deferReply();

      // Send the select menu as a new message
      await interaction.editReply({ content: 'Here is the select menu:', components: [row] });
    } catch (error) {
      console.error(`Error replying to interaction: ${error}`);
    }
} else if (interaction.customId === 'select_menu') {
    const selectedOption = interaction.values[0];
    let response = '';

    if (selectedOption === 'option1') {
      response = 'You selected Option 1.';
    } else if (selectedOption === 'option2') {
      response = 'You selected Option 2.';
    }

    try {
      // Send the response as a follow-up message
      await interaction.followUp({ content: response });
    } catch (error) {
      console.error(`Error sending follow-up message: ${error}`);
    }
}
})
client.login(config.token);
Editor is loading...