具有用户权限,客户端权限和限制的Discord.js命令处理程序

时间:2020-03-12 05:55:31

标签: node.js discord discord.js commando

我如何检查用户权限,客户端权限,添加限制并像Commando一样在导出中定义args

我的消息事件看起来像这样

client.on('message', async (message) => {

    if (message.author.bot) return;
    if (!message.guild) return;
    let prefix
    await db.collection('guilds').doc(message.guild.id).get().then((q) => {
        if (q.exists) {
            prefix = q.data().prefix || config1.prefix_mention;
        } else {
            prefix = "." || config1.prefix_mention;
        }
    })
    const prefixRegex = new RegExp(`^(<@!?${client.user.id}>|${escapeRegex(prefix)})\\s*`);
    if (!prefixRegex.test(message.content)) return;

    const [ matchedPrefix ] = message.content.match(prefixRegex);
    const args = message.content.slice(matchedPrefix.length).trim().split(/ +/g);

    const cmd = args.shift().toLowerCase();
    if (cmd.length === 0) return;

    let command = client.commands.get(cmd);
    if (!command) command = client.commands.get(client.aliases.get(cmd));
    if (!message.channel.permissionsFor(client.user).has("SEND_MESSAGES")) return;

    if (command)
        command.run(client, message, args, db);
})

我如何检查其他权限,例如

name: 'osu',
group: 'search',
memberName: 'osu',
description: 'Responds with information on an osu! user.',
clientPermissions: ["EMBED_LINKS","SEND_MESSAGES"],
userPermissions:['VIEW_CHANNEL'],
args: [
 {
  key: 'user',
  prompt: 'What user would you like to get information on?',
  type: 'string'
 }
],
async run(client ,message ,args) {
  //some code here
}

1 个答案:

答案 0 :(得分:0)

您可以获得client.user的{​​{1}}权限和message.member权限,然后使用message.channel进行检查

关于节流,您可以使用has.。这是很好的guide使用方法。

cooldowns