Using subcommands

Setting up the subcommand

This is the tempate command we are going to use.

const { Command, ArgumentType } = require('gcommands');

module.exports = class extends Command {
    constructor(client) {
        super(client, {
            name: 'perms',
            description: 'Permissions for a user or role',
        });
    }
}
1
2
3
4
5
6
7
8
9
10

Next we need to create the subcommand. For this example we will need the argument "target" in both subcommands.

args: [
    {
        name: 'user',
        description: 'View permissions of a user',
        type: ArgumentType.SUB_COMMAND,
        arguments: [
            {
                name: 'target',
                description: 'The user to target',
                type: ArgumentType.USER,
            }
        ]
    },
    {
        name: 'role',
        description: 'View permissions of a role',
        type: ArgumentType.SUB_COMMAND,
        arguments: [
            {
                name: 'target',
                description: 'The role to target',
                type: ArgumentType.ROLE,
                required: true,
            }
        ]
    }
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

This creates a command with the "user" and "role" subcommand. The subcommands both have the argument "target".

Using the subcommand

Next we want to get the permissions of a role of member. We start by checking if the "role" or "user" subcommand is used.

run({ respond, guild, objectArgs, member }) {
    if (objectArgs.user) {
        member = objectArgs.user.target
            ? objectArgs.user.target.match(/[0-9]+/g)
                ? guild.members.cache.get(objectArgs.user.target.match(/[0-9]+/g)[0]) || member
                : member
            : member;

        const perms = member.permissions.toArray()
            .join(', ')
            .replaceAll('_', ' ')
            .toLowerCase();

        respond({ content: perms });

    } else if (objectArgs.role) {
        const role = objectArgs.role.target
            ? objectArgs.role.target.match(/[0-9]+/g)
                ? guild.roles.cache.get(objectArgs.role.target.match(/[0-9]+/g)[0]) || null
                : null
            : null;

        if (!role) return respond({ content: 'Could not find that role' });

        const perms = role.permissions.toArray()
            .join(', ')
            .replaceAll('_', ' ')
            .toLowerCase();

        respond({ content: perms });
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

Resulting code

const { Command, ArgumentType } = require('gcommands');

module.exports = class extends Command {
    constructor(client) {
        super(client, {
            name: 'perms',
            description: 'Permissions for a user or role',
            args: [
                {
                    name: 'user',
                    description: 'View permissions of a user',
                    type: ArgumentType.SUB_COMMAND,
                    arguments: [
                        {
                            name: 'target',
                            description: 'The user to target',
                            type: ArgumentType.USER,
                        }
                    ]
                },
                {
                    name: 'role',
                    description: 'View permissions of a role',
                    type: ArgumentType.SUB_COMMAND,
                    arguments: [
                        {
                            name: 'target',
                            description: 'The role to target',
                            type: ArgumentType.ROLE,
                            required: true,
                        }
                    ]
                }
            ]
        });
    }
    run({ respond, guild, objectArgs, member }) {
        if (objectArgs.user) {
            member = objectArgs.user.target
                ? objectArgs.user.target.match(/[0-9]+/g)
                    ? guild.members.cache.get(objectArgs.user.target.match(/[0-9]+/g)[0]) || member
                    : member
                : member;

            const perms = member.permissions.toArray()
                .join(', ')
                .replaceAll('_', ' ')
                .toLowerCase();

            respond({ content: perms });

        } else if (objectArgs.role) {
            const role = objectArgs.role.target
                ? objectArgs.role.target.match(/[0-9]+/g)
                    ? guild.roles.cache.get(objectArgs.role.target.match(/[0-9]+/g)[0]) || null
                    : null
                : null;

            if (!role) return respond({ content: 'Could not find that role' });

            const perms = role.permissions.toArray()
                .join(', ')
                .replaceAll('_', ' ')
                .toLowerCase();

            respond({ content: perms });
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69