Ask Your Question
0

What is the method for including more than 25 options to the autocomplete feature in discord.js v14?

asked 2021-09-19 11:00:00 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2022-07-27 22:00:00 +0000

david gravatar image

In Discord.js v14, the maximum number of options that can be shown in an autocomplete feature is still 25. It is not possible to increase this limit.

However, you can implement pagination to show more than 25 options. When the user reaches the end of the first 25 options, you can use the "deferred" response to send a message with a "next" button that allows the user to see more options. This process can then be repeated for each page of options you want to show.

Here is an example of how to implement pagination in an autocomplete command:

const { SlashCommandBuilder } = require('@discordjs/builders');
const { MessageActionRow, MessageButton } = require('discord.js');

module.exports = {
    data: new SlashCommandBuilder()
        .setName('autocomplete')
        .setDescription('Autocomplete command')
        .addStringOption(option =>
            option.setName('options')
                .setDescription('Options to autocomplete from')
                .setRequired(true)
                .addChoices([
                    ['Option 1', '1'],
                    ['Option 2', '2'],
                    ['Option 3', '3'],
                    ['Option 4', '4'],
                    ['Option 5', '5'],
                    ['Option 6', '6'],
                    ['Option 7', '7'],
                    ['Option 8', '8'],
                    ['Option 9', '9'],
                    ['Option 10', '10'],
                    ['Option 11', '11'],
                    ['Option 12', '12'],
                    ['Option 13', '13'],
                    ['Option 14', '14'],
                    ['Option 15', '15'],
                    ['Option 16', '16'],
                    ['Option 17', '17'],
                    ['Option 18', '18'],
                    ['Option 19', '19'],
                    ['Option 20', '20'],
                    ['Option 21', '21'],
                    ['Option 22', '22'],
                    ['Option 23', '23'],
                    ['Option 24', '24'],
                    ['Option 25', '25'],
                    ['Option 26', '26'],
                    ['Option 27', '27'],
                    ['Option 28', '28'],
                    ['Option 29', '29'],
                    ['Option 30', '30'],
                ])),
    async execute(interaction) {
        const options = interaction.options.getString('options').split(/,\s*/);

        let currentPage = 0;
        const maxPages = Math.ceil(options.length / 25);
        const currentOptions = options.slice(currentPage * 25, currentPage * 25 + 25);

        const row = new MessageActionRow().addComponents(
            currentOptions.map(o => new MessageButton()
                .setCustomId(o)
                .setLabel(o)
                .setStyle('PRIMARY'))
        );

        await interaction.deferReply({ ephemeral: true });

        const message = await interaction.editReply({ content: `Page ${currentPage + 1} of ${maxPages}`, components: [row] });

        const collector = message.createMessageComponentCollector({
            componentType: 'BUTTON',
            time: 60000,
        });

        collector.on('collect', async i => {
            if (i.customId === 'next') {
                currentPage += 1;
                const currentOptions = options.slice(currentPage * 25, currentPage * 25 + 25);

                row.components = currentOptions.map(o => new MessageButton()
                    .setCustomId(o)
                    .setLabel(o)
                    .setStyle('PRIMARY'));

                if (currentPage + 1 === maxPages) {
                    row.components.push(new MessageButton()
                        .setCustomId('end')
                        .setLabel('End')
                        .setStyle('DANGER'));
                }

                await i.update({ content: `Page ${currentPage + 1} of ${maxPages}`, components: [row] });
            } else if (i.customId === 'end') {
                await i.update({ content: 'End of options', components: [] });
                collector.stop();
            } else {
                await interaction.editReply({ content: `You selected ${i.customId}`, components: [] });
                collector.stop();
            }
        });
    },
};

This command takes a comma-separated list of up to 30 options as input and shows a row of up to 25 buttons as autocomplete options. If there are more than 25 options, a "next" button is added to show the next page of options. If the last page is reached, an "end" button is shown to stop the pagination. When a button is clicked, the command replies with the selected option and stops the collector.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2021-09-19 11:00:00 +0000

Seen: 12 times

Last updated: Jul 27 '22