如何使静音角色优先于他人

时间:2020-05-28 11:19:39

标签: discord.js

我有一个不和谐的机器人,它有一个静音命令。静音命令在理论上是可行的,但是当我在适当的服务器上对其进行测试时,我注意到了一个问题。 “静音”角色位于角色列表的底部,这意味着其上方的所有其他角色都将忽略静音,从而使其实际上无用。是否有一种方法可以使创建的“静音”角色达到机器人可以分配的最高级别,从而使静音生效?

代码如下:

if (command === 'mute') {
  if (!message.member.hasPermission("MANAGE_ROLES") || !message.guild.owner) return message.channel.send("You don't have enough authority to do this.");

  if (!message.guild.me.hasPermission(["MANAGE_ROLES", "ADMINISTRATOR"])) 
    return message.channel.send("Give me the permissions first.")

  let role = message.guild.roles.find(x => x.name === "Muted");
  let member = message.mentions.members.first() || message.guild.members.get(args[0]);
  if (!member) return message.reply(`I can't find the user ` + member)

  let duration = ms(args[1]);
  let reason = args.slice(2).join(' ');
  if (!reason) reason = "No reason provided";
  if (!role) {
    try {
      role = await message.guild.createRole({
        name: "Muted",
        color: "#000000",
        permissions: []
      })
      message.guild.channels.forEach(async(channel, id) => {
        message.channel.overwritePermission(role, {
          SEND_MESSAGES: false,
          ADD_REACTIONS: false,
          SEND_TTS_MESSAGES: false,
          ATTACH_FILES: false,
          SPEAK: false
        });
      });
    } catch (e) {
      console.log(e);
    }
  }

  member.addRole(role).catch(console.error);
  message.channel.send(`<@${member.id}> , you have been muted for ${duration}.`)

  setTimeout(function() {
    member.removeRole(role.id);
    message.channel.send(`${member} has been automatically unmuted.`);
  }, duration);
}

1 个答案:

答案 0 :(得分:0)

免责声明:您似乎正在使用discord.js@v11,我将提供该版本的代码。

在创建角色时,应使用JSONValue['Artists'] || "No artists for this concert" 选项:

position

您可以找到创建角色here时可以使用的所有选项的列表。