首页 > 解决方案 > 从 TextChannel 获取权限 - Discord.js

问题描述

基本上,我需要从用户所在的当前文本频道中获取权限。我已经获得了频道名称,如果我需要获取应该很容易做到的 ID。

const Discord = require("discord.js");

module.exports.run = async (client, message, args) => {
  let currentChannel = message.channel.name;
  let category = message.channel.parent;;
  message.guild.createChannel(currentChannel).then(mchannel => {
    mchannel.setParent(category).then(() => {
      message.channel.delete();
    });
  });
}

module.exports.help = {
    name: "a.cleanchannel"
}
// Need the channel permissions to overwrite the new channel's permissions with the old ones

预期的结果是该通道应该具有与旧通道相同的权限。

标签: permissionsdiscorddiscord.jschannel

解决方案


要直接回答您的问题,您可以使用GuildChannel#permissionOverwrites创建具有与旧频道相同权限的新频道。例如...

message.guild.createChannel(message.channel.name, {
  type: 'text',
  permissionOverwrites: message.channel.permissionOverwrites
});

但是,您似乎正在尝试克隆频道。为了让这更容易,Discord.js 中内置了一个方法 - GuildChannel#clone(). 你可以像这样使用它...

message.channel.clone(undefined, true, true) // Same name, same permissions, same topic 
  .then(async clone => {
    await clone.setParent(message.channel.parent);
    await clone.setPosition(message.channel.position);
    await message.channel.delete();

    console.log(`Cloned #${message.channel.name}`);
  })
  .catch(console.error);

推荐阅读