【问题标题】:Why do permissions not apply (muting)?为什么权限不适用(静音)?
【发布时间】:2020-11-09 23:18:03
【问题描述】:

我正在尝试做一个基本的 Discord.js 机器人,我想添加一个静音命令。

我已经尝试过了(下面的代码)。这分配了一个“foca muted you oof”角色(我知道名字很奇怪。)这不允许你做任何事情。但是在我的测试帐户上,即使它分配了该角色,我仍然可以非常好地输入消息。

main.js:

    } else if(cmd === `${prefix}mute`) {
        if(!msg.member.hasPermission(["MANAGE_MESSAGES"])) return msg.channel.send("no no no, you can't manage messages. ok? alright?");
        let toMute = msg.guild.member(msg.mentions.users.first());
        if(toMute.user.username === msg.author.username) return msg.channel.send("oof. u can't mute urself.");
        if(!toMute) return msg.channel.send("idk who to mute. does that person exist????");

        let role = msg.guild.roles.find(r => r.name === "foca muted you oof");
        if(!role) {
            try {
                role = await msg.guild.createRole({
                    name: "foca muted you oof",
                    color: "#000000",
                    permissions: []
                });
                msg.guild.channels.forEach(async (channel, id) => {
                    await channel.overwritePermissions(role, {
                        "SEND_MESSAGES": false,
                        "ADD_REACTIONS": false
                    });
                })
            } catch(e) {
                console.log(e.stack);
            }
        }
        if(toMute.roles.has(role.id)) return msg.channel.send("oof. this user is already muted. tried to do a bravery, huh?");
        await toMute.addRole(role);
        msg.channel.send(`${toMute.user.username} has been muted.`);
    }

你能告诉我有什么问题吗?

【问题讨论】:

  • 也许被静音的用户有另一个角色覆盖较低的角色权限覆盖?
  • 好吧,以前可以工作(并且用户有额外的角色),但现在即使用户有 0 个角色(除了静音的角色)它也不起作用
  • 现在不行了。
  • @everyone 角色的覆盖情况如何?
  • 您是否在 Discord 服务器中的实际频道上设置了权限以禁止该角色的所有内容?因为如果您只是取消选中角色本身的权限,那将不会做任何事情。另外验证 @everyone 默认角色没有设置任何权限来明确允许通道本身。

标签: javascript node.js bots discord.js


【解决方案1】:
}else if(cmd === `${prefix}mute`){
  if(!msg.member.hasPermission(["MANAGE_MESSAGES"])) return msg.channel.send("You don't have permission to use this command."); //Check for permission.
  const toMute = msg.mentions.members.first(); //Get first mention of member.
  if(!toMute) return msg.reply("Member doesen't exist."); //If there's no member.
  if(toMute.user.id == msg.author.id) return msg.reply("You can't mute yourself."); //If member is author.
  const role = msg.guild.roles.get('Muted'); //Fetch the role.
  if(!role){ //If role doesen't exist create one.
    try{ //Try block which stops code in case of error.
      await msg.guild.creatRole({ //Create the role.
        name: 'Muted',
        color: '#F87666',
        permissions: ['SEND_MESSAGES': false, 'ADD_REACTIONS': false]
      });
      await msg.guild.channels.forEach(async (channel) => { //Overwrite every permission in every channel.
        'SEND_MESSAGES': false,
        'ADD_REACTIONS': false
      });
      await toMute.removeRoles(toMute.roles); //Remove every role member has.
      await toMute.addRole(role.id); //If everything went good add muted role to member.
      return msg.reply('Member muted successfully!')
    }catch(e){ //In case of error execute the code inside.
      msg.reply(`Error: ${e.message}`); //Tell author about error.
    }
  }else{ //If role exists
    if(toMute.roles.has(role.id)) return msg.reply('Member is already muted.'); //If member is muted reply.
    await toMute.removeRoles(toMute.roles);
    await toMute.addRole(role.id);
    return msg.reply('Member muted successfully!')
  }
}

我还没有测试它,它应该可以工作。享受

hasPermission: https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=hasPermission

Get 函数而不是 find,因为它已弃用(即将删除):https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/get

createRole: https://discord.js.org/#/docs/main/stable/class/Guild?scrollTo=createRole

removeRole: https://discord.js.org/#/docs/main/stable/class/GuildMemberscrollTo=removeRoles

添加角色: https://discord.js.org/#/docs/main/stable/class/GuildMember?scrollTo=addRole

有: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/has

forEach: https://www.w3schools.com/jsref/jsref_foreach.asp

角色的随机红色: https://coolors.co/

【讨论】:

  • 谢谢,但是我取消静音的时间呢?如何知道最高角色?
  • 呃,toMute.highestRole 应该这样做。如果您仍需要帮助,请访问我的个人资料并在 Discord 上给我发消息,以便我为您提供帮助。
  • 'SEND_MESSAGES': false not working 请回复说冒号错误
  • 我不明白你的意思。
猜你喜欢
  • 2022-01-21
  • 2020-03-12
  • 2020-06-15
  • 2014-11-26
  • 1970-01-01
  • 2020-05-23
  • 2021-10-31
  • 1970-01-01
  • 2010-11-26
相关资源
最近更新 更多