Is there a way to check for channel overwrites? (Discord.js v12)

Viewed 484

I was creating a command for locking a channel and used the .updateOverwrite() method to do this. With the code below I was trying to check if the channel already has VIEW_CHANNEL set to false and therefore respond with an error message. However, even if the channel has already been locked, it still 'locks' it again and skips the check, this check only seems to get triggered when the channel is unlocked. Would there be some other way to check for the channel overwrites?

    if (message.member.hasPermission('MANAGE_CHANNELS')) {
      if (!message.guild.roles.everyone.permissions.has('VIEW_CHANNEL')) {
        const errorEmbed = new Discord.MessageEmbed()
          .setDescription(`❌ '${message.channel.name}' is already locked.`)
          .setColor('RED');
        message.channel.send(errorEmbed);
      }
      else{
        message.channel.updateOverwrite(message.channel.guild.roles.everyone, { VIEW_CHANNEL: false }).then(() => {
          const msgEmbed = new Discord.MessageEmbed()
            .setDescription(`✅ '${message.channel.name}' has been locked.`)
            .setColor('GREEN');
          message.channel.send(msgEmbed);
        });
      }
    }
    else {
      const warningEmbed = new Discord.MessageEmbed()
        .setDescription(' Sorry, you do not have sufficient permissions to do this.')
        .setColor('YELLOW');
      message.channel.send(warningEmbed);
    }
1 Answers

You need to check the permissions for the channel, you're currently checking global role permissions. Here is the updated code:

if (message.member.hasPermission('MANAGE_CHANNELS')) {
      if (!message.channel.permissionsFor(message.guild.roles.everyone).has('VIEW_CHANNEL')) {
        const errorEmbed = new Discord.MessageEmbed()
          .setDescription(`❌ '${message.channel.name}' is already locked.`)
          .setColor('RED');
        message.channel.send(errorEmbed);
      }
      else{
        message.channel.updateOverwrite(message.channel.guild.roles.everyone, { VIEW_CHANNEL: false }).then(() => {
          const msgEmbed = new Discord.MessageEmbed()
            .setDescription(`✅ '${message.channel.name}' has been locked.`)
            .setColor('GREEN');
          message.channel.send(msgEmbed);
        });
      }
    }
    else {
      const warningEmbed = new Discord.MessageEmbed()
        .setDescription(' Sorry, you do not have sufficient permissions to do this.')
        .setColor('YELLOW');
      message.channel.send(warningEmbed);
    }
Related