1

The command I've written will take a set of role names, find their role IDs and then creates a channel that only the role names will be able to see.

const members = memberNames.map((name) => {
  let role = interaction.guild.roles.cache.find(
    (r) => r.name.toLowerCase() === name.toLowerCase(),
  )
  if (!role) {
    return null
  }
  console.log(role.id)

  return { id: role.id, allow: ['VIEW_CHANNEL'] }
})
 try {
  const channel = await interaction.guild.channels.create({
    name: allianceNameInput,
    type: 'text',
    parent: alliancesCategory.id,
    permissionOverwrites: [
      ...members,
      {
        id: interaction.guild.roles.everyone.id,
        deny: ['VIEW_CHANNEL'],
      },
    ],
  })

  await interaction.reply(`Created alliance channel: ${channel}`)
} catch (error) {
  console.error('Could not create alliance channel.', error)

  await interaction.reply({
    content: 'Could not create alliance channel.',
    ephemeral: true,
  })
}

I keep getting the following error message:

Could not create alliance channel. RangeError [BitFieldInvalid]: Invalid bitfield flag or number: VIEW_CHANNEL.

I've read through the documentation and some other peoples code and I can't find any other way to make it work so I don't know exactly what to do here.

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57
kris
  • 11
  • 2

1 Answers1

0

In v14, you can't use VIEW_CHANNEL or other strings, you should use the enums from PermissionFlagsBits. Also, the channel type should not be a string, you should use ChannelType.GuildText:

const { ChannelType, PermissionFlagsBits } = require('discord.js')

// ...

return { id: role.id, allow: [PermissionFlagsBits.ViewChannel] }

// ...

const channel = await interaction.guild.channels.create({
  name: allianceNameInput,
  type: ChannelType.GuildText,
  parent: alliancesCategory.id,
  permissionOverwrites: [
    ...members,
    {
      id: interaction.guild.roles.everyone.id,
      deny: [PermissionFlagsBits.ViewChannel],
    },
  ],
})

You can view all the available permissions here: PermissionFlagsBits

Related: Errors with enums in discord.js v14

Zsolt Meszaros
  • 21,961
  • 19
  • 54
  • 57