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

Role specific commands from ID's fetched from a database

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

这是我的 Discord Bot,数据库是 Firebase,我是 运行 Discord.JS/Node.JS on VSC。我曾尝试对其进行格式化,以便角色过滤器排在第一位,但这变得非常混乱。我不认为问题是引用数据库中的角色,因为我已经在控制台中记录了请求的 ID,并且 returns 是正确的字符串。此外,当用户具有 none 个允许的角色时,他们将无法使用这些命令。

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;
        }  
    }

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

只有服务器的管理员角色、服务器所有者和 bot 创建者才可以使用 "set" 命令。

并且每个具有一般机器人使用角色的人都应该被允许使用 "config"、"help"、"invite"、"patchNotes" 和 "ticket."

您的问题是:

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

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

false, true, true, true, true

等于 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") {

所以支票总是会转到其他地方 我写的东西修复了

 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 chicken 发布的内容,然后重新安排反命令拒绝。