Discord.js禁止用户权限命令

时间:2020-09-25 00:09:54

标签: javascript discord.js

因此,只要服务器上的主持人使用ban命令,他们就可以禁止主持人和管理员。有没有一种方法可以解决此问题?基本上,我想要这样做,所以当主持人使用ban命令时,他们不能禁止管理员。有人告诉我一个解决方案是将角色调低,但我想让角色保持在管理员角色之上。这是我的禁令代码:

const { MODLOGCHANNELNAME, MUTEROLE, MAINROLE, PREFIX, BOTNAME} = require("../config.json");
var ms = require('ms');
var Discord = require('discord.js');

module.exports ={
  name: 'ban',
  aliases: ['b',],//'@Knife Guard 2.0'],
  description: 'Bans a user from this server',
  usage: 'ban [mention] [reason]',
  category: "moderation",

  execute: async (message, args, client) => {
    
    const user = message.mentions.users.first();

    if(!message.member.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send("You do not have permission to perform this command!");

    let banMember = message.mentions.members.first() || message.guild.members.cache.get(args[0]);
    if(!banMember) return message.channel.send("Please provide a user to ban!");

    var reason = args.splice(1).join(' ');
    if(!reason) return message.reply('You need to give a reason!'); 

    if(!message.guild.me.hasPermission(["BAN_MEMBERS", "ADMINISTRATOR"])) return message.channel.send("I dont have permission to perform this command");
    //message.delete();

    var bot_name = BOTNAME;

    let dmEmbed = new Discord.MessageEmbed()
      .setColor('#FF0000')
      .setTitle('You Are Banned')
      .addField('Reason:', reason, true)
      // eslint-disable-next-line no-undef
      .setFooter(bot_name)
      .setTimestamp();

    message.channel.send(`**${banMember.user.tag}** has been banned`);//.then(m => m.delete(5000));

    banMember.send(dmEmbed).then(() => //dms the user they r banned
    // banMember.send(`Hello, you have been banned from ${message.guild.name} for: ${reason}`).then(() => //dms the user they r banned
    message.guild.member(banMember).ban(banMember, {reason: reason, days: 1})); //once DM done, Ban member
   

    // eslint-disable-next-line no-undef
    var banEmbed = new Discord.MessageEmbed()
    .setColor('#FF0000')
    .setAuthor(`${message.guild.name} Modlogs`, message.guild.iconURL)
    .addField('Moderation:', "Ban")
    .addField("Moderated On:", `${user.username} (${user.id})`)
    .addField("Moderator", message.author.username)
    .addField("Reason:", reason)
    .setTimestamp()
    // eslint-disable-next-line no-undef
    .setFooter(bot_name, message.author.avatarURL());
    
    let modlogchannel = (`${MODLOGCHANNELNAME}`);
    var channel = message.guild.channels.cache.find(c => c.name === modlogchannel); 
    channel.send(banEmbed);

  }
};

1 个答案:

答案 0 :(得分:0)

您可以使用Role.comparePositionsTo()

比较两个成员的最高角色

如果此角色的职位较低(其他角色的职位较高),则返回负数;如果此角色较高(其他角色的职位较低),则返回正数;如果相等,则返回0

// message author's highest role
const authorRole = message.member.roles.highest;

// target's highest role
const targetRole = banMember.roles.highest;

// if targetRole's position is higher or equal to authorRole's
if (targetRole.comparePositionsTo(authorRole) <= 0)
  return message.channel.send(...);