有没有办法检查通道覆盖? (Discord.js v12)

时间:2020-08-16 11:43:10

标签: javascript node.js discord.js

我正在创建用于锁定频道的命令,并使用.updateOverwrite()方法执行此操作。使用下面的代码,我试图检查通道是否已经将VIEW_CHANNEL设置为false,并因此以错误消息响应。但是,即使通道已被锁定,它仍然会再次“锁定”它并跳过检查,只有在通道解锁时,该检查才似乎被触发。会有其他方法检查频道覆盖吗?

    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 个答案:

答案 0 :(得分:1)

您需要检查频道的权限 ,当前正在检查全局角色的权限。这是更新的代码:

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