从数据库中获取ID的特定于角色的命令

时间:2019-04-02 23:27:52

标签: node.js visual-studio-code discord.js

因此,基本上,我已经选择了哪个角色应该使用哪些命令,我​​创建了一些代码,但是我的问题是,无论出于何种原因,如果用户对任何一个命令有任何要求,命令,他们能够使用所有命令。我已经有超过一个星期的问题了。它必须基于角色ID,因为用户必须使用角色ID在数据库中设置允许的角色。

这是针对我的Discord Bot,数据库是Firebase,并且我在VSC上运行Discord.JS / Node.JS。我尝试过格式化它,以便角色过滤器排在第一位,但这变得非常混乱。我不认为该问题是从数据库引用该角色的,因为我已记录了控制台中请求的ID,并且它返回了正确的字符串。同样,当用户没有任何允许的角色时,他们将无法使用命令。

let msg_array = msg.content.split(" ");
    let command = msg_array[0];
    let args = msg_array.slice(1);
    let prefix = "t!"
    let inputtedCMD = msg.content.slice(prefix.length).split(" ")
    let cmd = bot.commands.get(inputtedCMD[0])

    let allowedR = null

    await firebaseDB.collection('roles').doc(msg.guild.id).get('role_id').then((r) => {
        if (!r.exists){
            msg.channel.send("You havn't chosen an allowed admin role.")
        } else {
            allowedR = `${r.data().role_id}`;
        }
    })

    let genRole = null

    await firebaseDB.collection('generalRoles').doc(msg.guild.id).get('generalRole_id').then((h) => {
        if (!h.exists){
            msg.channel.send("You havn't chosen an allowed general role.")
        } else {
            genRole = `${h.data().generalRole_id}`;
        }
    })

    let guildOwner = null

    await firebaseDB.collection('guilds').doc(msg.guild.id).get('generalRole_id').then((q) => {
        if (!q.exists){
            msg.channel.send("Cannot access guild owner data.")
        } else {
            guildOwner = `${q.data().guildOwnerID}`;
        }
    })

    let generalRole = msg.guild.roles.get(genRole)
    let adminRole = msg.guild.roles.get(allowedR)


    if (!command.startsWith(prefix)) return;


    if (inputtedCMD === "databaseReset") {
        if (guildOwner === msg.author.id || msg.author.id === "198590136684904448") {
            cmd.run(bot, msg, args, firebaseDB).then(() => {
                console.log("[COMMAND] User w/ permission ran '" + inputtedCMD + "'")
                return;
            })
        } else {
            const notAllowed = new Discord.RichEmbed()
                .setAuthor("Hey, " + msg.author.tag, bot.user.displayAvatarURL)
                .setDescription("**__No access__** \n *You need the required role to be able to use that command.*")
                .setColor("#00A6ff")
                .setTimestamp()
                .setFooter("Ticket Bot | TBE")
            msg.channel.send(notAllowed)
            return;
        }
    }

    if (inputtedCMD === "set") {
        if (msg.member.roles.has(adminRole.id) || guildOwner === msg.author.id || msg.author.id === "198590136684904448") {
            cmd.run(bot, msg, args, firebaseDB).then(() => {
                console.log("[COMMAND] User w/ permission ran '" + inputtedCMD + "'")
                return;
            });
        } else {
            const notAllowed = new Discord.RichEmbed()
                .setAuthor("Hey, " + msg.author.tag, bot.user.displayAvatarURL)
                .setDescription("**__No access__** \n *You need the required role to be able to use that command.*")
                .setColor("#00A6ff")
                .setTimestamp()
                .setFooter("Ticket Bot | TBE")
            msg.channel.send(notAllowed)
            return;
        }
    } 

    if (inputtedCMD === "config","help","invite","patchNotes","ticket") {
        if (msg.member.roles.has(adminRole.id) || msg.member.roles.has(generalRole.id) || guildOwner === msg.author.id || msg.author.id === "198590136684904448") {
            cmd.run(bot, msg, args, firebaseDB).then(() => {
                console.log("[COMMAND] User w/ permission ran '" + inputtedCMD + "'")
                return;
            }) 
        } else {
            const notAllowed = new Discord.RichEmbed()
                .setAuthor("Hey, " + msg.author.tag, bot.user.displayAvatarURL)
                .setDescription("**__No access__** \n *You need the required role to be able to use that command.*")
                .setColor("#00A6ff")
                .setTimestamp()
                .setFooter("Ticket Bot | TBE")
            msg.channel.send(notAllowed)
            return;
        }  
    }

我希望只有服务器所有者和机器人创建者才能使用databaseReset(机器人创建者ID是所包含的ID [是我的顺便说一句])

仅应允许服务器的管理员角色,服务器所有者和机器人创建者使用“ set”命令。

每个具有一般机器人使用角色的人都应被允许使用“ config”,“ help”,“ invite”,“ patchNotes”和“ ticket”。

2 个答案:

答案 0 :(得分:0)

您的问题是:

inputtedCMD === "config","help","invite","patchNotes","ticket"

由于字符串始终为真,因此它将给出:

false, true, true, true, true

Wich等于true,所以它总是true

所以使用

 if(['config', 'help', 'invite', 'patchNotes', 'ticket'].includes(inputtedCMD)) {

代替

要替换的另一件事是:

let msg_array = msg.content.split(" ");
let command = msg_array[0];
let args = msg_array.slice(1);
let prefix = "t!"
let inputtedCMD = msg.content.slice(prefix.length).split(" ")
let cmd = bot.commands.get(inputtedCMD[0])

使用:

let args = msg.content.slice(prefix.length).split(" ");
let prefix = "t!"
let inputtedCMD = args.shift();
let cmd = bot.commands.get(inputtedCMD);

在这样做之前,您需要:

if (inputtedCMD[0] === "databaseReset") {

因此,支票将始终转到其他 我写的东西解决了这个问题

答案 1 :(得分:0)

 let prefix = "t!"
    if (!msg.content.startsWith(prefix)) return;
    let args = msg.content.slice(prefix.length).split(" ");
    let inputtedCMD = args.shift();
    let cmd = bot.commands.get(inputtedCMD);

    let allowedR = null

    await firebaseDB.collection('roles').doc(msg.guild.id).get('role_id').then((r) => {
        if (!r.exists){
            msg.channel.send("You havn't chosen an allowed admin role.")
        } else {
            allowedR = `${r.data().role_id}`;
        }
    })

    let genRole = null

    await firebaseDB.collection('generalRoles').doc(msg.guild.id).get('generalRole_id').then((h) => {
        if (!h.exists){
            msg.channel.send("You havn't chosen an allowed general role.")
        } else {
            genRole = `${h.data().generalRole_id}`;
        }
    })

    let guildOwner = null

    await firebaseDB.collection('guilds').doc(msg.guild.id).get('generalRole_id').then((q) => {
        if (!q.exists){
            msg.channel.send("Cannot access guild owner data.")
        } else {
            guildOwner = `${q.data().guildOwnerID}`;
        }
    })

    let generalRole = msg.guild.roles.get(genRole)
    let adminRole = msg.guild.roles.get(allowedR)

    if(['databaseReset'].includes(inputtedCMD)) {
        if (guildOwner === msg.author.id || msg.author.id === "198590136684904448") {
            cmd.run(bot, msg, args, firebaseDB).then(() => {
                console.log("[COMMAND] User w/ permission ran '" + inputtedCMD + "'")
                return;
            })
        } else {
            const notAllowed = new Discord.RichEmbed()
                .setAuthor("Hey, " + msg.author.tag, bot.user.displayAvatarURL)
                .setDescription("**__No access__** \n *You need the required role to be able to use that command.*")
                .setColor("#00A6ff")
                .setTimestamp()
                .setFooter("Ticket Bot | TBE")
            msg.channel.send(notAllowed)
            return;
        }
    }

    if(['set'].includes(inputtedCMD)) {
        if (msg.member.roles.has(adminRole.id) || guildOwner === msg.author.id || msg.author.id === "198590136684904448") {
            cmd.run(bot, msg, args, firebaseDB).then(() => {
                console.log("[COMMAND] User w/ permission ran '" + inputtedCMD + "'")
                return;
            });
        } else {
            const notAllowed = new Discord.RichEmbed()
                .setAuthor("Hey, " + msg.author.tag, bot.user.displayAvatarURL)
                .setDescription("**__No access__** \n *You need the required role to be able to use that command.*")
                .setColor("#00A6ff")
                .setTimestamp()
                .setFooter("Ticket Bot | TBE")
            msg.channel.send(notAllowed)
            return;
        }
    } 

    if(['config', 'help', 'invite', 'patchNotes', 'ticket'].includes(inputtedCMD)) {
        if (msg.member.roles.has(adminRole.id) || msg.member.roles.has(generalRole.id) || guildOwner === msg.author.id || msg.author.id === "198590136684904448") {
            cmd.run(bot, msg, args, firebaseDB).then(() => {
                console.log("[COMMAND] User w/ permission ran '" + inputtedCMD + "'")
                return;
            }) 
        } else {
            const notAllowed = new Discord.RichEmbed()
                .setAuthor("Hey, " + msg.author.tag, bot.user.displayAvatarURL)
                .setDescription("**__No access__** \n *You need the required role to be able to use that command.*")
                .setColor("#00A6ff")
                .setTimestamp()
                .setFooter("Ticket Bot | TBE")
            msg.channel.send(notAllowed)
            return;
        }  
    }

所以基本上,这是对我有用的代码。我需要做的是实现@PLASMA小鸡发布的内容,然后重新排列反命令拒绝。