设置权限并赋予用户角色

时间:2021-05-03 19:36:36

标签: javascript discord bots mute

我正在尝试为我的机器人发出静音命令,但遇到了这些问题。我已经得到它们很长时间了。

const { Guild } = require("discord.js");

module.exports = {
    name: "mute",
    description: "Mutes a member",
    execute(message,args,Mention){
      if (message.member.hasPermission("ADMINISTRATOR") || message.author.username === "creepersaur"){
          let targetMember = message.mentions.members.first();
          const guild = message.guild
          
          let myRole = message.guild.roles.cache.find(role => role.name === "Muted");
          if (!myRole) {
            
             newRole = message.guild.roles.create({
                data: {
                  name: 'Muted',
                  color: 'BLUE',
                  Permissions: '',
                },
                reason: `Make a muted role if it doesn't exist.`
             })
            .then(console.log)
            .catch(console.error); 

            //message.guild.roles.Muted.setPermissions({'SEND_MESSAGES': true})

            targetMember.roles.add(newRole).catch(console.error);
            message.channel.send(`${targetMember.username} has been muted!`);

          } else if (myRole) {
            targetMember.roles.add(myRole).catch(console.error);
            message.channel.send(`${targetMember.username} has been muted!`);
          }
      }
  }
};

它会查看服务器中是否有静音角色,如果没有,那么它会创建一个静音角色给用户。除了权限不起作用。正因为如此,它并没有将角色本身赋予我想要的用户。 :( 凸点 x1

1 个答案:

答案 0 :(得分:0)

我真的没有看到角色部分有什么不好的地方,但是这段代码应该修复了权限。

const { Guild } = require("discord.js");

module.exports = {
    name: "mute",
    description: "Mutes a member",
    execute(message,args,Mention){
      if (message.member.hasPermission("ADMINISTRATOR") || message.author.username === "creepersaur"){
          let targetMember = message.mentions.members.first();
          const guild = message.guild
          
          let myRole = message.guild.roles.cache.find(role => role.name === "Muted");
          if (!myRole) {
            
             newRole = message.guild.roles.create({
                data: {
                  name: 'Muted',
                  color: 'BLUE',
                  Permissions: '',
                },
                reason: `Make a muted role if it doesn't exist.`
             })
            .then(console.log)
            .catch(console.error); 

            message.guild.channels.cache.forEach(ch => 
        {
        if(ch.type == "text") {
          ch.updateOverwrite(
          newRole, {
              SEND_MESSAGES: false
          }

            targetMember.roles.add(newRole).catch(console.error);
            message.channel.send(`${targetMember.username} has been muted!`);

          } else if (myRole) {
            targetMember.roles.add(myRole).catch(console.error);
            message.channel.send(`${targetMember.username} has been muted!`);
          }
      }
  }
};