dotnize / Dodong

An easy-to-setup music bot made with discord.js and discord-player
MIT License
59 stars 15 forks source link

search command error #28

Closed TapsHTS closed 2 years ago

TapsHTS commented 2 years ago
[22/05 - 12:08:09] [WARN] unhandledRejection DiscordAPIError: Invalid Form Body
data.components[0].components[5]: The specified component exceeds the maximum width
data.components[0].components[6]: The specified component exceeds the maximum width
data.components[0].components[7]: The specified component exceeds the maximum width
data.components[0].components[8]: The specified component exceeds the maximum width
data.components[0].components[9]: The specified component exceeds the maximum width
data.components[0].components[10]: The specified component exceeds the maximum width
data.components[0].components[11]: The specified component exceeds the maximum width
data.components[0].components[12]: The specified component exceeds the maximum width
data.components[0].components[13]: The specified component exceeds the maximum width
data.components[0].components[14]: The specified component exceeds the maximum width
data.components[0].components[15]: The specified component exceeds the maximum width
data.components[0].components[16]: The specified component exceeds the maximum width
data.components[0].components[17]: The specified component exceeds the maximum width
data.components[0].components[18]: The specified component exceeds the maximum width
data.embeds: Must be 10 or fewer in length.
Promise {
  <rejected> DiscordAPIError: Invalid Form Body
  data.components[0].components[5]: The specified component exceeds the maximum width
  data.components[0].components[6]: The specified component exceeds the maximum width
  data.components[0].components[7]: The specified component exceeds the maximum width
  data.components[0].components[8]: The specified component exceeds the maximum width
  data.components[0].components[9]: The specified component exceeds the maximum width
  data.components[0].components[10]: The specified component exceeds the maximum width
  data.components[0].components[11]: The specified component exceeds the maximum width
  data.components[0].components[12]: The specified component exceeds the maximum width
  data.components[0].components[13]: The specified component exceeds the maximum width
  data.components[0].components[14]: The specified component exceeds the maximum width
  data.components[0].components[15]: The specified component exceeds the maximum width
  data.components[0].components[16]: The specified component exceeds the maximum width
  data.components[0].components[17]: The specified component exceeds the maximum width
  data.components[0].components[18]: The specified component exceeds the maximum width
  data.embeds: Must be 10 or fewer in length.
      at RequestHandler.execute (D:\Users\legot\Visual\NeotuxBot-MAJ\node_modules\discord.js\src\rest\RequestHandler.js:350:13)
      at processTicksAndRejections (node:internal/process/task_queues:96:5)
      at async RequestHandler.push (D:\Users\legot\Visual\NeotuxBot-MAJ\node_modules\discord.js\src\rest\RequestHandler.js:51:14)
      at async CommandInteraction.reply (D:\Users\legot\Visual\NeotuxBot-MAJ\node_modules\discord.js\src\structures\interfaces\InteractionResponses.js:99:5)
      at async Object.runInteraction (D:\Users\legot\Visual\NeotuxBot-MAJ\commands\music\search.js:63:98) {
    method: 'post',
    path: '/interactions/977875518370222110/aW50ZXJhY3Rpb246OTc3ODc1NTE4MzcwMjIyMTEwOlhpb3J3cWlKaXRWVHRNc2RjZ0RiMm5wRHB3UGJpSG1ld3lRdWVTYTlQUkpuYkhSbU14UjE2WnhGYTNTbjl5bXBiUVRJcUtmTlVVWDFJMURkbG5rWHpsbzUxdUExakFKMkRiQjMxZVV3ZW5ONWloc05CdTNIY090N0Zxc244Ym5W/callback',
    code: 50035,
    httpStatus: 400,
    requestData: { json: [Object], files: [] }
  }
}

I have this error when I try the search command:

async runInteraction(client, interaction, slash) {
        const titre = interaction.options.getString('song');

        if (!interaction.member.voice.channelId)
            return interaction.reply({ embeds: [{ description: `You are not in a voice channel!`, color: 0xb84e44 }], ephemeral: true });
        if (interaction.guild.me.voice.channelId && interaction.member.voice.channelId !== interaction.guild.me.voice.channelId)
            return interaction.reply({ embeds: [{ description: `You are not in my voice channel!`, color: 0xb84e44 }], ephemeral: true });

        if (!interaction.guild.me.permissionsIn(interaction.member.voice.channel).has(client.requiredVoicePermissions)) return;

        if (slash) await message.deferReply();
        let query = titre
        const searchResult = await client.player.search(query, { requestedBy: interaction.author })
        if (!searchResult || !searchResult.tracks.length) {
            const reply = { embeds: [{ description: `No results found!`, color: 0xb84e44 }], ephemeral: true };
            interaction.editReply(reply)
            return;
        }
        if (searchResult.playlist) {
            const reply = { embeds: [{ description: `This command does not support playlists.\nUse **${client.prefix}play** instead.`, color: 0xb84e44 }], ephemeral: true };
            interaction.editReply(reply)
            return;
        }
        const buttons = [];
        const embeds = [];
        for (let i = 0; i < searchResult.tracks.length; i++) {

            const button = new MessageButton()
                .setCustomId(`search_${i}`)
                .setLabel(`${i+1}`)
                .setStyle('SECONDARY')
                .setDisabled(false);
            buttons.push(button);

            embeds.push({
                thumbnail: {
                    url: `${searchResult.tracks[i].thumbnail}`
                },
                description: `**${i+1}.** [${searchResult.tracks[i].title}](${searchResult.tracks[i].url})\n${searchResult.tracks[i].author} - ${searchResult.tracks[i].duration}`,
                color: 0x44b868
            });
        }
        const row = new MessageActionRow().addComponents(buttons);

        const msg = slash ? await interaction.editReply({ embeds: embeds, components: [row] }) : await interaction.reply({ embeds: embeds, components: [row] });
        const sMessage = slash ? await interaction.fetchReply() : msg;

        const filter = (button) => button.customId.startsWith('search_');
        const collector = await sMessage.createMessageComponentCollector({ filter, time: 30000 });

        collector.on("collect", async(button) => {
            if (button.member !== interaction.member) return;
            await button.deferUpdate();
            const queue = await client.player.createQueue(interaction.guild, {
                metadata: { channel: interaction.channel },
                bufferingTimeout: 1000,
                disableVolume: false,
                leaveOnEnd: true,
                leaveOnStop: true,
                spotifyBridge: false
            });
            let justConnected;
            try {
                if (!queue.connection) {
                    justConnected = true;
                    await queue.connect(interaction.member.voice.channel);
                }
            } catch {
                client.player.deleteQueue(interaction.guild);
                sMessage.edit({ embeds: [{ description: `Could not join your voice channel!`, color: 0xb84e44 }], components: [] });
                return collector.stop("messageDelete");
            }

            queue.addTrack(searchResult.tracks[parseInt(button.customId.split("_").pop())]);
            sMessage.edit({
                embeds: [{
                    description: `Queued **[${searchResult.tracks[parseInt(button.customId.split("_").pop())].title}](${searchResult.tracks[parseInt(button.customId.split("_").pop())].url})**`,
                    color: 0x44b868
                }],
                components: []
            });
            if (justConnected) queue.play();
            collector.stop("messageDelete");
        });

        collector.on("end", (_, reason) => {
            if (reason !== "messageDelete" && sMessage.editable) {
                row.setComponents(buttons.map(b => b.setDisabled(true)));
                sMessage.edit({
                    embeds: embeds,
                    components: [row]
                }).catch(error => {});
            }
        });
    }
dotnize commented 2 years ago

Each search result is placed in an embed, and discord only allows up to 10 embeds per message.

It looks like your search method returns more than 10 results, so you should find a way to limit it, or just put a max limit on how many embeds you can show.

This is how the bot does it (limit to 5 results): https://github.com/nizeic/Dodong/blob/7b06232e118c72fa3471966019993d22bca08d43/utils/extractor.js#L180