简体   繁体   English

如何在 Discord.js v14 中创建只有用户和机器人可以看到的私人频道?

[英]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.我尝试在 Discord.js v14 中创建私人频道,但我的代码返回错误。

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

There are a couple of errors in your code.您的代码中有几个错误。 In v14, create() accepts an options object and "text" is not a valid type anymore.在 v14 中, create()接受options object 并且"text"不再是有效类型。 You should import the ChannelType enums and use ChannelType.GuildText for this.您应该导入ChannelType枚举并为此使用ChannelType.GuildText

There is no interaction.author .没有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.有一个interaction.userinteraction.member ,所以如果你想使用作者的 ID,你可以使用它们。

There is no client.id either.也没有client.id You can grab the bot's ID, using client.user.id though.不过,您可以使用client.user.id获取机器人的 ID。

Also, you could add the permissions inside the create method as an option so you don't have to use that then .此外,您可以在create方法中添加权限作为选项,这样您就不必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],
      },
    ],
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM