Search code examples
node.jsdiscorddiscord.jscommando

Discord.js command handler with user permission ,client permission and throttling


How can i check user permission ,client permission , add throttling and define args in exports like commando does

my message event looks like this

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

how can i check other permission like example

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
}

Solution

  • You can get client.user permissions and message.member permissions for message.channel and then check it with has.

    About throttling , you can use cooldowns. This is the nice guide how to use it.

    const { Permissions } = require('discord.js');
    
    if(command.clientPermissions.length > 0) {
        let clientChannelPermissions = message.channel.permissionsFor(message.guild.me);
        clientChannelPermissions = new Permissions(clientChannelPermissions.bitfield);
        if(!clientChannelPermissions.has(command.clientPermissions)) {
            let missingPermissions = command.clientPermissions.filter(perm => clientChannelPermissions.has(perm) === false).join(', ')
            return message.reply(`I can`t execute this command, missing permissions for ${missingPermissions}`)
        }
    }
    
    if(command.userPermissions.length > 0) {
        let memberChannelPermissions = message.channel.permissionsFor(message.member);
        memberChannelPermissions = new Permissions(memberChannelPermissions.bitfield);
        if(!memberChannelPermissions.has(command.clientPermissions)) {
            let missingPermissions = command.clientPermissions.filter(perm => memberChannelPermissions.has(perm) === false).join(', ')
            return message.reply(`I can`t execute this command, you missing permissions for ${missingPermissions}`)
        }
    }