ART0022VI
ART0022VI

Reputation: 63

How to create a private channel only the user and the bot can see in Discord.js v14?

I tried to create a private channel in Discord.js v14 but my code returns an error.

Here is the relevant part of my code:

else if (interaction.customId === 'donatid') {
    if(interaction.values[0] == '01'){
     const everyoneRole = interaction.guild.roles.everyone;
     const name = interaction.user.tag;
     interaction.guild.channels.create(name, 'text') // i think problem over here
     .then(r => {
         r.overwritePermissions(interaction.author.id, { VIEW_CHANNEL: true });
         r.overwritePermissions(client.id, { VIEW_CHANNEL: true });
         r.overwritePermissions(everyoneRole, { VIEW_CHANNEL: false });
       })
   .catch(console.error);

   }
};

And the error:

DiscordAPIError\[50035\]: Invalid Form Body

Upvotes: 1

Views: 1533

Answers (1)

Zsolt Meszaros
Zsolt Meszaros

Reputation: 23169

There are a couple of errors in your code. In v14, create() accepts an options object and "text" is not a valid type anymore. You should import the ChannelType enums and use ChannelType.GuildText for this.

There is no interaction.author. There is an interaction.user or an interaction.member, so you can use those if you want to use the author's ID.

There is no client.id either. You can grab the bot's ID, using client.user.id though.

Also, you could add the permissions inside the create method as an option so you don't have to use that then. See the working code below:

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

//...
  interaction.guild.channels.create({
    name: interaction.user.tag,
    type: ChannelType.GuildText,
    permissionOverwrites: [
      {
        id: interaction.guild.roles.everyone,
        deny: [PermissionFlagsBits.ViewChannel],
      },
      {
        id: interaction.user.id,
        allow: [PermissionFlagsBits.ViewChannel],
      },
      {
        id: client.user.id,
        allow: [PermissionFlagsBits.ViewChannel],
      },
    ],
  });

Upvotes: 2

Related Questions