Creating a new channel with a Discord bot command

Hey everyone! I’m stuck trying to get my Discord bot to make a new text channel called ‘General 2’ when I use the !setup command. The command works fine, but I can’t figure out how to make it create a new channel.

I’ve looked at some YouTube tutorials, but they either caused errors or didn’t do anything. I keep getting this error:

DiscordAPIError[50035]: Invalid Form Body
name[BASE_TYPE_REQUIRED]: This field is required

Here’s what I’ve tried so far:

const { Client, IntentsBitField } = require('discord.js')

const bot = new Client({
  intents: [
    IntentsBitField.Flags.Guilds,
    IntentsBitField.Flags.GuildMessages,
    IntentsBitField.Flags.MessageContent,
  ]
})

bot.on('ready', () => {
  console.log('Bot is online')
})

bot.on('messageCreate', (msg) => {
  if (msg.content === '!setup') {
    const guild = msg.guild
    const channelName = msg.author.username
    guild.channels.create(channelName, { type: 'text' })
  }
})

bot.login(process.env.BOT_TOKEN)

Can anyone help me figure out what I’m doing wrong? Thanks!

The issue you’re encountering stems from changes in the Discord.js library. In newer versions, the channel creation method has been updated. Try modifying your code as follows:

guild.channels.create({
  name: 'General 2',
  type: Discord.ChannelType.GuildText
})

This approach should resolve the error you’re experiencing. Make sure you’ve imported the necessary components from discord.js. Also, ensure your bot has the required permissions to create channels in the server. If you’re still facing issues, double-check your bot’s role hierarchy within the server settings.

I ran into a similar issue when I was setting up my first Discord bot. The problem is likely related to permissions. Make sure your bot has the ‘Manage Channels’ permission in the server. You can check this in the server settings under ‘Roles’.

Also, try wrapping your channel creation in a try-catch block to get more detailed error information:

try {
  await guild.channels.create({ name: 'General 2', type: 'GUILD_TEXT' });
  msg.reply('Channel created successfully!');
} catch (error) {
  console.error('Error creating channel:', error);
  msg.reply('Failed to create channel. Check bot permissions.');
}

This will help you pinpoint if it’s a permissions issue or something else. Don’t forget to make your command handler async if you use await. Hope this helps!

hey liam, looks like ur missing the ‘name’ parameter in the create method. try this instead:

guild.channels.create({ name: ‘General 2’, type: ‘text’ })

that should fix the error ur getting. lemme know if it works!