Creating your command with arguments

DANGER

We're only going to show the new Command method here, but of course the class extends method works as well. What?

Normal arguments

If you want to add arguments to the command, you have more options. Now, we'll show you how to add simple arguments and different types.
First, you need to import the ArgumentType enum and Argument class, which we will be using a lot.

const { MessageEmbed } = require('discord.js');
const { Command, CommandType, Argument, ArgumentType } = require('gcommands');

new Command({
    name: 'userinfo',
    description: 'Check user informations',
    type: [ CommandType.SLASH ],
    arguments: [
        new Argument({
            name: 'user',
            description: 'Select user',
            type: ArgumentType.USER,
            required: true,
        })
    ],
    run: (ctx) => {
        // We will use the getMember method because we want to get a straight GuildMember from the argument.
        // We'll put `user` in this method because that's what our argument is called.
        const member = ctx.arguments.getMember('user');

        const embed = new MessageEmbed()
            .setAuthor({
                name: member.user.tag.toString(),
                iconURL: member.user.displayAvatarURL({ dynamic: true }),
            })
            .addFields([
                {
                    name: 'Username',
                    value: member.user.username.toString(),
                    inline: true,
                },
                {
                    name: 'Discriminator',
                    value: member.user.discriminator.toString(),
                    inline: true,
                },
                {
                    name: 'Nickname',
                    value: member.nickname || 'none',
                    inline: true,
                },
                {
                    name: 'Timeout',
                    value: member.isCommunicationDisabled ? member.communicationDisabledUntil : '❌',
                    inline: true,
                },
                {
                    name: 'Joined At',
                    value: member.joinedAt.toString(),
                    inline: true,
                },
                {
                    name: 'Created At',
                    value: member.user.createdAt.toString(),
                    inline: true,
                }
            ])
            .setColor(member.displayHexColor || "RANDOM")
            .setThumbnail(member.user.displayAvatarURL({ dynamic: true }))
            .setTimestamp();

        ctx.reply({
            content: `Informations about ${member.toString()}`,
            embeds: [ embed ],
        })
    }
})
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

TIP

You don't have to use the Argument class, but you can write the arguments directly into the arguments object.

Sub Commands

What is it? What is it for? Why is it? You may have a lot of questions, but we'll explain it to you right away.
Sub commands are for when you want to have a sub command. For example, /money give <name>.
It doesn't pay to do a /moneygive command when the limit of commands for interaction is 100 so far, and it doesn't look pretty either.

Again, it's very simple, as in the first example.

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

new Command({
    name: 'money',
    description: 'Give/remove/check money',
    type: [ CommandType.SLASH ],
    autoDefer: AutoDeferType.NORMAL,
    arguments: [
        new Argument({
            name: 'add',
            description: 'Add money for user',
            type: ArgumentType.SUB_COMMAND,
            arguments: [
                new Argument({
                    name: 'user',
                    description: 'Select user',
                    type: ArgumentType.USER,
                    required: true,
                })
            ]
        }),
        new Argument({
            name: 'remove',
            description: 'Remove money from user',
            type: ArgumentType.SUB_COMMAND,
            arguments: [
                new Argument({
                    name: 'user',
                    description: 'Select user',
                    type: ArgumentType.USER,
                    required: true,
                })
            ]
        }),
        new Argument({
            name: 'check',
            description: 'Check user balance',
            type: ArgumentType.SUB_COMMAND,
            arguments: [
                new Argument({
                    name: 'user',
                    description: 'Select user',
                    type: ArgumentType.USER,
                    required: false,
                })
            ]
        })
    ],
    run: (ctx) => {
        // Now let's detect what sub command was used.
        const sub = ctx.arguments.getSubcommand();

        if (sub === 'add') {
            // add code
        } else if (sub === 'remove') {
            // remove code
        } else  {
            const user = ctx.arguments.getMember('user');

            // Why safeReply? We have `autoDefer` in the command, if by chance there is a problem with the database, so that the command doesn't fail and the discord doesn't throw "Interaction failed".
            // This will allow your command to last a bit longer, and then the reply will automatically modify itself. So `safeReply` is a function of `reply` and `editReply`.
            ctx.safeReply({
                content: `${user ? `${user.toString()}'s` : `Your`} balance is 0€.`,
                ephemeral: 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
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

TIP

If you want advice on the database, use prisma

Sub Command Group

What is a Sub Command Group? After all, we have Sub Command, so why Sub Command Group? This is a very useful type to make your life easier.
If you want to have a command such as /role mass add <role> then you are in the right place.

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

new Command({
    name: 'role',
    description: 'Add/remove role',
    type: [ CommandType.SLASH ],
    arguments: [
        new Argument({
            name: 'mass',
            description: 'Role for everyone',
            type: ArgumentType.SUB_COMMAND_GROUP,
            arguments: [
                new Argument({
                    name: 'add',
                    description: 'Add role (everyone)',
                    type: ArgumentType.SUB_COMMAND,
                    arguments: [
                        new Argument({
                            name: 'role',
                            description: 'Select role',
                            type: ArgumentType.ROLE,
                            required: true,
                        })
                    ]
                }),
                new Argument({
                    name: 'remove',
                    description: 'Remove role (everyone)',
                    type: ArgumentType.SUB_COMMAND,
                    arguments: [
                        new Argument({
                            name: 'role',
                            description: 'Select role',
                            type: ArgumentType.ROLE,
                            required: true,
                        })
                    ]
                })
            ]
        }),
    	new Argument({
      		name: 'single',
      		description: 'Role for single user',
      		type: ArgumentType.SUB_COMMAND_GROUP,
      		arguments: [
        		new Argument({
          			name: 'add',
          			description: 'Add role for user',
          			type: ArgumentType.SUB_COMMAND,
          			arguments: [
            			new Argument({
              				name: 'role',
              				description: 'Select role',
              				type: ArgumentType.ROLE,
              				required: true,
            			}),
           				new Argument({
              				name: 'user',
              				description: 'Select user',
              				type: ArgumentType.USER,
              				required: true,
            			})
          			]
        		}),
        		new Argument({
         			name: 'remove',
          			description: 'Remove role from user',
          			type: ArgumentType.SUB_COMMAND,
          			arguments: [
            			new Argument({
              				name: 'role',
              				description: 'Select role',
              				type: ArgumentType.ROLE,
              				required: true,
            			}),
            			new Argument({
              				name: 'user',
              				description: 'Select user',
              				type: ArgumentType.USER,
              				required: true,
            			})
          		    ]
        		})
      		]
    })
    ],
    run: async(ctx) => {
        // Now let's detect what sub command group was used.
        const subgroup = ctx.arguments.getSubcommandGroup();
        const sub = ctx.arguments.getSubcommand();

        const role = ctx.arguments.getRole('role');

        if (subgroup === 'mass') {
            if (sub === 'add') {
                // Add role (everyone)
                let members = (await ctx.guild.members.fetch());
                members = [...members.values()];
                members = members.filter(m => !m.roles.cache.has(role.id));

                members.forEach((member) => {
                    member.roles.add(role);
                })

                ctx.reply({
                    content: `ETA: ${ms(members.length * 1000)}`
                })
            } else {
                // Remove role (everyone)
                let members = (await ctx.guild.members.fetch());
                members = [...members.values()];
                members = members.filter(m => m.roles.cache.has(role.id));

                members.forEach((member) => {
                    member.roles.remove(role);
                })

                ctx.reply({
                    content: `ETA: ${ms(members.length * 1000)}`
                })
            }
        } else {
            const member = ctx.arguments.getMember('user');

            if (sub === 'add') {
                // Add role to user
		if (member.roles.cache.has(role.id)) {
                    ctx.reply({
                        content: 'You already have this role!',
                        ephemeral: true,
                    })
                    return;
                }
                member.roles.add(role)
                    .then(() => {
                        ctx.reply({
                            content: 'Added!',
                            ephemeral: true,
                        })
                    })
                .catch(e => {
                    ctx.reply({
                        content: e,
                        ephemeral: true,
                    })
                });
            } else {
               // Remove role from user
                 if (!member.roles.cache.has(role.id)) {
                    ctx.reply({
                        content: 'You don\'t have this role!',
                        ephemeral: true,
                    })
                    return;
                }
                member.roles.remove(role)
                    .then(() => {
                        ctx.reply({
                            content: 'Removed!',
                            ephemeral: true,
                    })
                })
                .catch(e => {
                    ctx.reply({
                        content: e,
                        ephemeral: 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
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173

Autocomplete

What is this? Surely you know that with an argument of type STRING there is an option to add choices.
Autocomplete is the same as choices, but with the added benefit of being dynamic.

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

new Command({
    name: 'autocomplete',
    description: 'The autocomplete command',
    type: [ CommandType.SLASH ],
    arguments: [
        new Argument('string', {
            description: 'String input',
            type: ArgumentType.STRING,
            run: (ctx) => { // dynamic choices, autocomplete
                const guild = ctx.guild;

                ctx.respond([
                    {
                        name: 'Red',
                        value: 'red',
                    },
                    {
                        name: `Your guild id: ${guild.id}`,
                        value: guild.id
                    }
                ]);
            }
        })
    ],
    run: async (ctx) => {
        await ctx.reply({content: ctx.arguments.getString('string')});
    }
});
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