从数据库中获取的来自 ID 的角色特定命令
Posted
技术标签:
【中文标题】从数据库中获取的来自 ID 的角色特定命令【英文标题】:Role specific commands from ID's fetched from a database 【发布时间】:2019-08-24 09:01:31 【问题描述】:所以基本上,我已经选择了应该由哪个角色使用哪些命令,我已经创建了一些代码,但我的问题是,无论出于何种原因,如果用户对任何角色有任何要求命令,他们能够使用所有命令。我已经有一个多星期的问题了。它需要基于角色 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”。
【问题讨论】:
【参考方案1】:你的问题是:
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")
所以检查总是会转到 else 我写的东西解决了这个问题
【讨论】:
这样做会使任何命令都不会运行,无论用户角色如何。甚至 'databaseReset' 和 'set' 也不会运行。 实施您的更改并修复发生的错误(因为在使用前缀之后定义了前缀以及类似的简单内容)不会产生任何改进。这些命令仍然无法运行,并且没有说明原因的错误。【参考方案2】: 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 发布的内容,然后重新安排反命令拒绝。
【讨论】:
以上是关于从数据库中获取的来自 ID 的角色特定命令的主要内容,如果未能解决你的问题,请参考以下文章
如何从具有特定值的变量和数据库中获取数据以将其计入laravel上的图表