discordjs / Commando

Official command framework for discord.js
Apache License 2.0
497 stars 243 forks source link

[Question] Command pattern matching and arg parsing #406

Closed gzcharleszhang closed 3 years ago

gzcharleszhang commented 3 years ago

Suppose I want to build a command such as .emoji add <imageUrl> where . is the prefix and <imageUrl> is the only argument. Since default handling doesn't support commands with a space in its name, so I am using regex for command matching. However, the only one I know how to parse the argument is through using a regex capturing group like so:

patterns: [/^.emoji add (.*)/],
args: [
  {
    key: 'imageUrl',
    prompt: 'specify the emoji you want to add',
    type: 'string',
    ...
  }
],

To get the args I can only do args[1] which is the capturing group I specified in the regex. Is there a way to use both patterns matching and leverage Commando's built-in argument parsing/validation, so that I can do args.imageUrl or enable prompts when an argument isn't specified like in commands with default handling?

S222em commented 3 years ago

What you could do is make an argument for the "add". Depends if you want to make commands like edit and remove

const { Command } = require('discord.js-commando');

module.exports = class MeowCommand extends Command {
    constructor(client) {
        super(client, {
            ...
            args: [
                {
                    key: 'action',
                    prompt: 'specify the action you want to peform',
                    type: 'string',
                    oneOf: ['add', 'remove'],
                },
                {
                    key: 'imageUrl',
                    prompt: 'specify the emoji you want to add',
                    type: 'string'
                },
            ],
        });
    }
    run(message, { action }) {
        switch (action) {
            case 'add':
                //code
                break;
            case 'remove':
                //code
                break;
        }
    }
};