Bot不会编辑每个频道的角色权限

时间:2020-10-20 10:17:34

标签: javascript discord.js

我正在尝试制作反垃圾邮件,并且当该漫游器应该为每个频道编辑静音角色的权限时,

所以我首先创建角色

const mute_role = message.guild.roles.cache.find(r => r.name == "Muted");
  if (!mute_role) {
    try {
      mute_role = await message.guild.roles.create({
        data: {
          name: "Muted",
          color: "#000000",
          permissions: []
        }
      })

然后我在所有公会频道上编辑角色的权限

message.guild.channels.cache.forEach(async (channel, id) => {
        await channel.updateOverwrite(mute_role, 
          {
            SEND_MESSAGES: false,
            ADD_REACTIONS: false,
          }
        );
      });
    } catch (e) {
      console.log(e.stack);
    }
  }

执行完此操作后,它应该继续执行其余代码,但似乎跳过了这一部分,因为它继续处理反垃圾邮件,并在触发垃圾邮件时添加了角色

这是反垃圾邮件的完整代码

bot.on('message', async message => {
  if (message.author.bot) return;
  const mute_role = message.guild.roles.cache.find(r => r.name == "Muted");
  if (!mute_role) {
    try {
      mute_role = await message.guild.roles.create({
        data: {
          name: "Muted",
          color: "#000000",
          permissions: []
        }
      })
      message.guild.channels.cache.forEach(async (channel, id) => {
        await channel.updateOverwrite(mute_role, 
          {
            SEND_MESSAGES: false,
            ADD_REACTIONS: false,
          }
        );
      });
    } catch (e) {
      console.log(e.stack);
    }
  }
  if (usersMap.has(message.author.id)) {
    const userData = usersMap.get(message.author.id);
    const { lastMessage, timer } = userData;
    const difference = message.createdTimestamp - lastMessage.createdTimestamp;
    let msgCount = userData.msgCount;
    console.log(difference);

    if (difference > DIFF) {
      clearTimeout(timer);
      console.log('Cleared timeout');
      userData.msgCount = 1;
      userData.lastMessage = message;
      userData.timer = setTimeout(() => {
        usersMap.delete(message.author.id);
        console.log('Removed from RESET');
      }, TIME);
      usersMap.set(message.author.id, userData);
    }
    else {
      msgCount++;
      console.log(msgCount)
      if (parseInt(msgCount) === LIMIT) {
        message.member.roles.add(mute_role);
        message.channel.send(`${message.author} has been muted for spamming`);
        setTimeout(() => {
          message.member.roles.remove(mute_role);
          message.channel.send(`${message.author} has been unmuted`);
        }, TIME);
      }
      else {
        userData.msgCount = msgCount;
        usersMap.set(message.author.id, userData);
      }
    }
  }
  else {
    let fn = setTimeout(() => {
      usersMap.delete(message.author.id);
      console.log('Removed from map');
    }, TIME);
    usersMap.set(message.author.id, {
      msgCount: 1,
      lastMessage: message,
      timer: fn
    })
  }
})

1 个答案:

答案 0 :(得分:0)

好的,所以我才发现为什么它不起作用

代替使用

const mute_role = message.guild.roles.cache.find(r => r.name == "Muted");

我只需要使用

let mute_role = message.guild.roles.cache.find(r => r.name == "Muted");

现在它可以工作并覆盖频道权限enter image description here