Home > Back-end >  ErrorName => DiscordAPIError: Invalid Form Body
ErrorName => DiscordAPIError: Invalid Form Body

Time:03-21

I could not solve the problem with this code, although I tried in several ways. I hope someone can help me with this

CODE:

client.on("messageCreate", async message => {
if (message.author.bot || !message.guild) return;
if (message.content.startsWith(prefix   "get-roles")) {
    let roles_notifications = new Discord.MessageActionRow()
    .addComponents(
        new Discord.MessageButton()
        .setCustomId("notification_1")
        .setLabel("All Notifications")
        .setStyle("PRIMARY"),
        new Discord.MessageButton()
        .setCustomId("notification_2")
        .setLabel("YouTube Uploads")
        .setStyle("PRIMARY"),
        new Discord.MessageButton()
        .setCustomId("notification_3")
        .setLabel("Twitter Uploads")
        .setStyle("PRIMARY"),
        new Discord.MessageButton()
        .setCustomId("notification_4")
        .setLabel("Server News")
        .setStyle("PRIMARY"),
        new Discord.MessageButton()
        .setCustomId("notification_5")
        .setLabel("Server Updates")
        .setStyle("PRIMARY"),
        new Discord.MessageButton()
        .setCustomId("notification_6")
        .setLabel("Anime News")
        .setStyle("PRIMARY"),
        new Discord.MessageButton()
        .setCustomId("notification_7")
        .setLabel("Anime Episodes")
        .setStyle("PRIMARY"),
    )
    let embed = new Discord.MessageEmbed()
    .setDescription("Select role")
    let msg = await message.channel.send({ embeds: [embed], components: [roles_notifications] })
    let iFilter = i => i.user.id === message.author.id;
    let collector = msg.createMessageComponentCollector({ filter: iFilter, time: 3600000 })

    collector.on("collect", async r => {
        if (r.customId === "notification_1") {
            let role_1 = message.guild.roles.cache.get(config.roles.all_notifications)
            if (message.member.roles.cache ?.has(role_1.id)) {
                message.member.roles.remove(role_1.id)
                r.reply({ content: `Removed The ${role_1} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_1.id)
                r.reply({ content: `Added The ${role_1} Role!`, ephemeral: true })
            }
        } else if (r.customId === "notification_2") {
            let role_2 = message.guild.roles.cache.get(config.roles.youTube_uploads)
            if (message.member.roles.cache ?.has(role_2.id)) {
                message.member.roles.remove(role_2.id)
                r.reply({ content: `Removed The ${role_2} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_2.id)
                r.reply({ content: `Added The ${role_2} Role!`, ephemeral: true })
            }
        } else if (r.customId === "notification_3") {
            let role_3 = message.guild.roles.cache.get(config.roles.twitter_uploads)
            if (message.member.roles.cache ?.has(role_3.id)) {
                message.member.roles.remove(role_3.id)
                r.reply({ content: `Removed The ${role_3} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_3.id)
                r.reply({ content: `Added The ${role_3} Role!`, ephemeral: true })
            }
        } else if (r.customId === "notification_4") {
            let role_4 = message.guild.roles.cache.get(config.roles.server_news)
            if (message.member.roles.cache ?.has(role_4.id)) {
                message.member.roles.remove(role_2.id)
                r.reply({ content: `Removed The ${role_2} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_4.id)
                r.reply({ content: `Added The ${role_4} Role!`, ephemeral: true })
            }
        }  else if (r.customId === "notification_5") {
            let role_5 = message.guild.roles.cache.get(config.roles.server_updates)
            if (message.member.roles.cache ?.has(role_5.id)) {
                message.member.roles.remove(role_5.id)
                r.reply({ content: `Removed The ${role_5} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_5.id)
                r.reply({ content: `Added The ${role_5} Role!`, ephemeral: true })
            }
        } else if (r.customId === "notification_6") {
            let role_6 = message.guild.roles.cache.get(config.roles.anime_news)
            if (message.member.roles.cache ?.has(role_6.id)) {
                message.member.roles.remove(role_6.id)
                r.reply({ content: `Removed The ${role_6} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_6.id)
                r.reply({ content: `Added The ${role_6} Role!`, ephemeral: true })
            }
        } else if (r.customId === "notification_7") {
            let role_7 = message.guild.roles.cache.get(config.roles.anime_episodes)
            if (message.member.roles.cache ?.has(role_7.id)) {
                message.member.roles.remove(role_7.id)
                r.reply({ content: `Removed The ${role_7} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_7.id)
                r.reply({ content: `Added The ${role_7} Role!`, ephemeral: true })
            }
        }
    })
}

});

ERROR:

/home/runner/System-Bot/node_modules/discord.js/src/rest/RequestHandler.js:350
  throw new DiscordAPIError(data, res.status, request);
        ^

DiscordAPIError: Invalid Form Body components[0].components[5]: The specified component exceeds the maximum width components[0].components[6]: The specified component exceeds the maximum width at RequestHandler.execute (/home/runner/System-Bot/node_modules/discord.js/src/rest/RequestHandler.js:350:13) at processTicksAndRejections (node:internal/process/task_queues:96:5) at async RequestHandler.push (/home/runner/System-Bot/node_modules/discord.js/src/rest/RequestHandler.js:51:14) at async TextChannel.send (/home/runner/System-Bot/node_modules/discord.js/src/structures/interfaces/TextBasedChannel.js:175:15) { method: 'post', path: '/channels/947233067129516093/messages', code: 50035, httpStatus: 400, requestData: { json: { content: undefined, tts: false, nonce: undefined, embeds: [ { title: null, type: 'rich', description: 'Select role', url: null, timestamp: 0, color: null, fields: [], thumbnail: null, image: null, author: null, footer: null } ], components: [ { components: [ [Object], [Object], [Object], [Object], [Object], [Object], [Object] ], type: 1 } ], username: undefined, avatar_url: undefined, allowed_mentions: undefined, flags: undefined, message_reference: undefined, attachments: undefined, sticker_ids: undefined }, files: [] } }

CodePudding user response:

According to discord.js, you can only have up to five buttons within an MessageActionRow, so all you have to do is remove the last two MessageButtons to remove the error. But if you still want all the buttons to be displayed, you can add another 'MessageActionRow` in your code having the last two buttons and send them together so that your code looks like this:

client.on("messageCreate", async message => {
if (message.author.bot || !message.guild) return;
if (message.content.startsWith(prefix   "get-roles")) {
    let first_roles_notifications = new Discord.MessageActionRow()
    .addComponents(
        new Discord.MessageButton()
        .setCustomId("notification_1")
        .setLabel("All Notifications")
        .setStyle("PRIMARY"),
        new Discord.MessageButton()
        .setCustomId("notification_2")
        .setLabel("YouTube Uploads")
        .setStyle("PRIMARY"),
        new Discord.MessageButton()
        .setCustomId("notification_3")
        .setLabel("Twitter Uploads")
        .setStyle("PRIMARY"),
        new Discord.MessageButton()
        .setCustomId("notification_4")
        .setLabel("Server News")
        .setStyle("PRIMARY"),
        new Discord.MessageButton()
        .setCustomId("notification_5")
        .setLabel("Server Updates")
        .setStyle("PRIMARY"),
    )
    let second_roles_notifications = new Discord.MessageActionRow()
    .addComponents(
        new Discord.MessageButton()
        .setCustomId("notification_6")
        .setLabel("Anime News")
        .setStyle("PRIMARY"),
        new Discord.MessageButton()
        .setCustomId("notification_7")
        .setLabel("Anime Episodes")
        .setStyle("PRIMARY"),
    )
    let embed = new Discord.MessageEmbed()
    .setDescription("Select role")
    let msg = await message.channel.send({ embeds: [embed], components: [first_roles_notifications, second_roles_notifications] })
    let iFilter = i => i.user.id === message.author.id;
    let collector = msg.createMessageComponentCollector({ filter: iFilter, time: 3600000 })

    collector.on("collect", async r => {
        if (r.customId === "notification_1") {
            let role_1 = message.guild.roles.cache.get(config.roles.all_notifications)
            if (message.member.roles.cache ?.has(role_1.id)) {
                message.member.roles.remove(role_1.id)
                r.reply({ content: `Removed The ${role_1} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_1.id)
                r.reply({ content: `Added The ${role_1} Role!`, ephemeral: true })
            }
        } else if (r.customId === "notification_2") {
            let role_2 = message.guild.roles.cache.get(config.roles.youTube_uploads)
            if (message.member.roles.cache ?.has(role_2.id)) {
                message.member.roles.remove(role_2.id)
                r.reply({ content: `Removed The ${role_2} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_2.id)
                r.reply({ content: `Added The ${role_2} Role!`, ephemeral: true })
            }
        } else if (r.customId === "notification_3") {
            let role_3 = message.guild.roles.cache.get(config.roles.twitter_uploads)
            if (message.member.roles.cache ?.has(role_3.id)) {
                message.member.roles.remove(role_3.id)
                r.reply({ content: `Removed The ${role_3} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_3.id)
                r.reply({ content: `Added The ${role_3} Role!`, ephemeral: true })
            }
        } else if (r.customId === "notification_4") {
            let role_4 = message.guild.roles.cache.get(config.roles.server_news)
            if (message.member.roles.cache ?.has(role_4.id)) {
                message.member.roles.remove(role_2.id)
                r.reply({ content: `Removed The ${role_2} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_4.id)
                r.reply({ content: `Added The ${role_4} Role!`, ephemeral: true })
            }
        }  else if (r.customId === "notification_5") {
            let role_5 = message.guild.roles.cache.get(config.roles.server_updates)
            if (message.member.roles.cache ?.has(role_5.id)) {
                message.member.roles.remove(role_5.id)
                r.reply({ content: `Removed The ${role_5} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_5.id)
                r.reply({ content: `Added The ${role_5} Role!`, ephemeral: true })
            }
        } else if (r.customId === "notification_6") {
            let role_6 = message.guild.roles.cache.get(config.roles.anime_news)
            if (message.member.roles.cache ?.has(role_6.id)) {
                message.member.roles.remove(role_6.id)
                r.reply({ content: `Removed The ${role_6} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_6.id)
                r.reply({ content: `Added The ${role_6} Role!`, ephemeral: true })
            }
        } else if (r.customId === "notification_7") {
            let role_7 = message.guild.roles.cache.get(config.roles.anime_episodes)
            if (message.member.roles.cache ?.has(role_7.id)) {
                message.member.roles.remove(role_7.id)
                r.reply({ content: `Removed The ${role_7} Role!`, ephemeral: true })
            } else {
                message.member.roles.add(role_7.id)
                r.reply({ content: `Added The ${role_7} Role!`, ephemeral: true })
            }
        }
    })
}

  • Related