所以基本上,我已经选择了哪个命令应该由哪个角色使用,我已经创建了一些代码,但我的问题是,无论出于何种原因,如果用户对任何命令有任何要求,能够使用所有命令。我一直有这个问题超过一个星期。它需要基于角色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(bot创建者ID是包含的ID [这是我顺便说一下])
只允许服务器和服务器所有者以及bot创建者的admin角色使用“set”命令。
每个拥有一般机器人使用角色的人都应该被允许使用“config”,“help”,“invite”,“patchNotes”和“ticket”。
你的问题是:
inputtedCMD === "config","help","invite","patchNotes","ticket"
由于字符串始终为true,因此它将给出:
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发布的内容,然后重新安排反命令拒绝。