Discord.js禁止/踢命令可用于所有用户。我怎样才能解决这个问题?

时间:2019-02-26 19:12:01

标签: javascript discord discord.js

我正在制作私有的Discord API,因为我不信任较大的API(Dyno,Hime,NosoBot等),而我的机器人已经完成了很多工作。唯一的问题是我的代码允许所有成员使用这些命令。我希望所有人都可以访问除这两个命令之外的所有命令。该代码有效,但是如何使它只允许具有踢脚/禁止权限的人使用?

    if (message.content.startsWith("$kick")) {
        // Easy way to get member object though mentions.
        var member= message.mentions.members.first();
        // Kick
        member.kick().then((member) => {
            // Successmessage
            message.channel.send(":wave: " + member.displayName + " has been successfully kicked :point_right: ");
        }).catch(() => {
             // Failmessage
            message.channel.send("Access Denied");
        });
    }
});
 client.on("message", (message) => {
    if (message.content.startsWith("$ban")) {
        // Easy way to get member object though mentions.
        var member= message.mentions.members.first();
        // ban
        member.ban().then((member) => {
            // Successmessage
            message.channel.send(":wave: " + member.displayName + " has been successfully banned https://gfycat.com/playfulfittingcaribou :point_right: ");
        }).catch(() => {
             // Failmessage
            message.channel.send("Access Denied");
        });
    }
});

7 个答案:

答案 0 :(得分:1)

在服务器中担任角色,然后检查代码中的角色

例如

if(!message.member.roles.find("name", "Admin"))
   return;

如果用户没有角色,将退出方法

所以代码看起来像这样

 if (message.content.startsWith("$kick")) {

    if (!message.member.roles.find("name", "Admin"))
        return;
    // Easy way to get member object though mentions.
    var member = message.mentions.members.first();
    // Kick
    member.kick().then((member) => {
        // Successmessage
        message.channel.send(":wave: " + member.displayName + " has been successfully kicked :point_right: ");
    }).catch(() => {
        // Failmessage
        message.channel.send("Access Denied");
    });
}
});
client.on("message", (message) => {
    if (message.content.startsWith("$ban")) {

        if (!message.member.roles.find("name", "Admin"))
            return;

        // Easy way to get member object though mentions.
        var member = message.mentions.members.first();
        // ban
        member.ban().then((member) => {
            // Successmessage
            message.channel.send(":wave: " + member.displayName + " has been successfully banned https://gfycat.com/playfulfittingcaribou :point_right: ");
        }).catch(() => {
            // Failmessage
            message.channel.send("Access Denied");
        });
    }
});

一种更好的方法是制作一个函数,该函数返回用户是否具有权限并在您的代码中使用它

答案 1 :(得分:1)

我将此用于角色权限:

if(message.member.roles.find(r => r.name === 'name of role')){
            //Enter command coding here
            }else{
            return message.channel.send('Enter Speech here')
            }
            break;

我不知道这也许对您有帮助。

答案 2 :(得分:1)

嘿,我有更好的东西给你

const Discord = require('discord.js');

if (message.content.startsWith(${prefix}BAN)) => {
    if (!message.guild.member(message.author).hasPermission('BAN_MEMBERS')) { return message.channel.send('You do not have the permission for ban users"  !'); }

if (!message.guild.member(client.user).hasPermission('BAN_MEMBERS')) { return message.channel.send('I don\'t have the permission for ban users" !'); }

if (message.mentions.users.size === 0) { return message.channel.send('You need to ping a user !'); }
let banMember = message.guild.member(message.mentions.users.first());
if (!banMember) { return message.channel.send('User not found!'); }

        banMember.ban().then((member) => {
            message.channel.send(member.displayName + " has left the server")
            message.channel.send(member.displayName + " has been successfully banned by " + message.author);
        })
    }

只需删除BAN并用KICK代替

答案 3 :(得分:0)

要做:

if (message.member.hasPermission("ADMINISTRATOR") {
  // Your code that kicks/bans.
  // All members with the role that has administrator 
  // permission will be able to kick/ban 
}

答案 4 :(得分:0)

if(message.content.startsWith(${prefix}kick)){

    let member = message.mentions.members.first();
    member.kick().then((member) => {
        message.channel.send(`:wave: ${member.displayName} has been kicked`);
    }).catch(() => {
        if (!message.member.hasPermission(['KICK_MEMBERS', 'ADMINISTRATOR'])) {
            message.reply("You cannot kick members");
        } else if (member.hasPermission(['KICK_MEMBERS', 'BAN_MEMBERS', 'ADMINISTRATOR'])) {
            message.reply("You cannont kick this member");
        }
    })
}
if (message.content.startsWith(`${prefix}ban`)) {

    let member = message.mentions.members.first();
    member.ban().then((member) => {
        message.channel.send(`:wave: ${member.displayName} has been kicked`);
    }).catch(() => {
        if (!message.member.hasPermission(['BAN_MEMBERS', 'ADMINISTRATOR'])) {
            message.reply("You cannot ban members");
        } else if (member.hasPermission(['KICK_MEMBERS', 'BAN_MEMBERS', 'ADMINISTRATOR'])) {
            message.reply("You cannont ban this member");
        }
    })
}

答案 5 :(得分:0)

    if (message.content.startWith(`${prefix}ban`)){
let buser = message.guild.member(message.mentions.users.first() || message.guild.members.get(args[0]));
if (!buser) return message.channel.send("Please provid a user to ban")
let reason = args.join(" ").slice(22)
if (!reason) return message.channel.send("Please provide a reason")
if(!message.member.hasPermission("BAN_MEMBERS")) return message.channel.send("You don't have acces to this command")
if (buser.hasPermission("BAN_MEMBERS")) return message.channel.send("This user has the perm ban user")
let embed = new Discord.RichEmbed()
buser.ban()
.setAuthor("Ban")
.setDescription(`The user ${buser} has been ban for: ${reason}\n by the admin ${message.author.tag}`)
bot.channels.get("an channel id").send(embed);

}

答案 6 :(得分:0)

if (msg.content.startsWith("$kick ")) {
if (msg.author.hasPermission(`BANNING_MEMBERS`) {
    if (msg.mentions.members.first()) {
        msg.mentions.members.first.kick().then((member) => {
            msg.channel.send(":wave: " + member.displayName + " has been successfully kicked :point_right: ");
        }).catch(() => {
            msg.channel.send("I do not have permissions to do this");
        });
    }
}else if (msg.content.startsWith("$ban ")) {
   if (!message.member.hasPermission("MANAGE_MESSAGES")) return;
    if (msg.mentions.members.first()) {
        msg.mentions.members.first.ban().then((member) => {
            msg.channel.send(":wave: " + member.displayName + " has been successfully banned :point_right: ");
        }).catch(() => {
            msg.channel.send("I do not have permissions to do this");
        });
    }
    } else {
msg.reply(`you can't ban members ask owner for admin`)
};

}

我建议您使用 prettierc(如果您使用 vs 代码进行编码,例如内部人员)