【问题标题】:Grabbing the permissions from a TextChannel - Discord.js从 TextChannel 获取权限 - Discord.js
【发布时间】:2019-10-29 09:18:58
【问题描述】:

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

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

【问题讨论】:

    标签: permissions discord discord.js channel


    【解决方案1】:

    要直接回答您的问题,您可以使用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);
    

    【讨论】:

    • 我不知道克隆,真的很好!
    • 唯一要记住的是它不保留父级和位置,所以这就是为什么你必须在之后设置它们。
    • 谢谢!你是救生员。
    猜你喜欢
    • 2018-12-21
    • 2020-08-16
    • 2022-11-29
    • 2020-12-25
    • 1970-01-01
    • 1970-01-01
    • 2020-01-28
    • 2021-01-01
    • 2021-09-25
    相关资源
    最近更新 更多