簡體   English   中英

如何在 Discord.js v14 中創建只有用戶和機器人可以看到的私人頻道?

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

我嘗試在 Discord.js v14 中創建私人頻道,但我的代碼返回錯誤。

這是我的代碼的相關部分:

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);

   }
};

和錯誤:

DiscordAPIError\[50035\]: Invalid Form Body

您的代碼中有幾個錯誤。 在 v14 中, create()接受options object 並且"text"不再是有效類型。 您應該導入ChannelType枚舉並為此使用ChannelType.GuildText

沒有interaction.author 有一個interaction.userinteraction.member ,所以如果你想使用作者的 ID,你可以使用它們。

也沒有client.id 不過,您可以使用client.user.id獲取機器人的 ID。

此外,您可以在create方法中添加權限作為選項,這樣您就不必then使用它了。 請參閱下面的工作代碼:

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