Skip to content
Advertisement

Discord Bot Error; Uncaught DiscordAPIError: Invalid Form Body

//Show all channel IDs
if (command == 'allchannelids') {
  const channelnames = msg.guild.channels.cache.map(g => g.name)
  for (let i=0; i < 2; i++) { channelnames.shift(1) }
  const channelids = msg.guild.channels.cache.map(g => g.id)
  for (let i=0; i < 2; i++) { channelids.shift(1) }

  const allchannelembeds = new MessageEmbed()
  .setTitle(`${msg.guild.name}'s IDs and Channels`)
  .setColor('GOLD')
  .setFields(
    {name: `Names:`, value: `${channelnames.join(`n`)}`, inline:true},
    {name: 'IDs:', value: `${channelids.join(`n`)}`, inline:true},
  )
  
  msg.channel.send({embeds:[allchannelembeds]})
}

This is the code for my discord bot to show all my channel names and ids for the guild it’s in. I’m still a beginner in terms of learning JavaScript and Discord JS, can someone help me solve this issue being that in my test server the code works as shown by the screenshot but in my actual server, the program keeps producing the error “Uncaught DiscordAPIError: Invalid Form Body” I’ve tried searching through Google for what’s causing the issue and can’t seem to find a solution anywhere as to why it is producing this.

Screenshot of Discord Embed In Test Server

Advertisement

Answer

Refer to initial post’s comments for context

Given the requirement of both channel names and channel IDs, the easiest method would to loop through the channel names/ids and create 2 arrays for each of the fields for the embeds.

let limitedChannelNames = [""], limitedChannelIDs = [""];
let currentEmbedIndex = 0;
channelnames.forEach((val, index) => {
    if (limitedChannelNames[currentEmbedIndex].length + val.length > 1022 ||
        limitedChannelIDs[currentEmbedIndex].length + channelids[index].length > 1022) {
        currentEmbedIndex++;
        limitedChannelNames[currentEmbedIndex] = "";
        limitedChannelIDs[currentEmbedIndex] = "";
    }
    limitedChannelNames[currentEmbedIndex] += val + `n`;
    limitedChannelIDs[currentEmbedIndex] += channelids[index] + `n`;
});

The arrays are used for storing each of the field values for when you create the embeds. The currentEmbedIndex is so the loop knows which one to append to.

You then loop through the channel names, keeping the index of the array so you can then grab the same value from the channel IDs list. You check if the current embed value for either the names or the IDs would become too long (1022 not 1024 as you append n to the end of each of them later).

You increment the embed index as needed and define the next string, appending the channel names and ids to their respective lists.

This is where you make all the embeds based on the lists you made earlier.

let allchannelembeds = [];
limitedChannelNames.forEach((channelName, index) => {
    allchannelembeds.push(new Discord.MessageEmbed()
            .setTitle(`${msg.guild.name}'s IDs and Channels`)
            .setColor("GOLD")
            .setFields(
                { name: `Names:`, value: `${channelName}`, inline: true },
                { name: "IDs:", value: `${limitedChannelIDs[index]}`, inline: true }
            )
    );
});

You create an array for the embeds to be stored and then loop through each string created by the previous code, again grabbing the index to keep the IDs linked. You create the embed the same as before but using the new lists.

You then finish by sending the embeds. You may have to create a check to make sure you aren’t sending more than 10 embeds per message as that is the maximum.

msg.channel.send({embeds: allchannelembeds});

You don’t want the [] around the embeds this time as it is already a list.

Full code:

let limitedChannelNames = [""], limitedChannelIDs = [""];
let currentEmbedIndex = 0;
channelnames.forEach((val, index) => {
    if (limitedChannelNames[currentEmbedIndex].length + val.length > 1022 ||
        limitedChannelIDs[currentEmbedIndex].length + channelids[index].length > 1022) {
        currentEmbedIndex++;
        limitedChannelNames[currentEmbedIndex] = "";
        limitedChannelIDs[currentEmbedIndex] = "";
    }
    limitedChannelNames[currentEmbedIndex] += val + `n`;
    limitedChannelIDs[currentEmbedIndex] += channelids[index] + `n`;
});
let allchannelembeds = [];
limitedChannelNames.forEach((channelName, index) => {
    allchannelembeds.push(
        new Discord.MessageEmbed()
            .setTitle(`${msg.guild.name}'s IDs and Channels`)
            .setColor("GOLD")
            .setFields(
                { name: `Names:`, value: `${channelName}`, inline: true },
                { name: "IDs:", value: `${limitedChannelIDs[index]}`, inline: true }
            )
    );
});
msg.channel.send({ embeds: allchannelembeds });
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement