Context Menus

GCommands also brings context menu support with slash and message commands.
You must enable context menus in new GCommandsClient.

new GCommandsClient({
  /* ... */
  commands: {
    context: "both",
  },
});
1
2
3
4
5
6

Here are all the options:

TYPEDESCRIPTION
bothMessage + User
messageOnly message
userOnly user
falseNothing

Here's a basic example:

const { Command } = require("gcommands");

module.exports = class extends Command {
  constructor(client) {
    super(client, {
      name: "parse",
      slash: false,
    });
  }

  async run({ respond, interaction, objectArgs }) {
    if (objectArgs.user) {
      respond(`Name: ${objectArgs.user.username}`);
    } else if (objectArgs.message) {
      respond(`Message: ${objectArgs.message.content}`);
    }
  }
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

Here's an example with every command type mixed:

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

module.exports = class extends Command {
  constructor(client) {
    super(client, {
      name: "parse",
      descriptions: "Parses user/message info",
      args: [
        {
          name: "message",
          description: "Message Info",
          type: ArgumentType.STRING,
          required: true,
        },
        {
          name: "user",
          description: "User Info",
          type: ArgumentType.USER,
          required: true,
        },
      ],
    });
  }

  async run({ client, interaction, respond, channel, args, objectArgs }) {
    if (interaction && interaction.isContextMenu()) {
      if (objectArgs.user)
        respond({
          content: `${objectArgs.user.username}`,
          ephemeral: true,
        });
      if (objectArgs.message)
        respond({
          content: `${objectArgs.message.content}`,
          ephemeral: true,
        });

      return;
    }

    respond({
      content: [
        `**User:** ${client.users.cache.get(objectArgs.user).username}`,
        `**Message:** ${
          (await channel.messages.fetch(objectArgs.message)).content
        }`,
      ].join("\n"),
      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

WARNING

Note that the context menus do not accept any arguments.