Search code examples
discordpermissionsdiscord.js

Grabbing the permissions from a TextChannel - Discord.js


Basically, I need to grab the permissions from the current text channel the user is in. I have already got the channel name, if I need to get the ID that should be pretty easy to do.

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

The expected results are that the channel should have the same permissions as the old one.


Solution

  • To answer your question directly, you can use GuildChannel#permissionOverwrites to create the new channel with the same permissions as the old one. For example...

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

    However, it looks like you're trying to clone a channel. To help make that easier, there's a method built into Discord.js for it - GuildChannel#clone(). You can use it like so...

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