Skip to content

Examples

ccjmk edited this page Mar 18, 2022 · 4 revisions

These are several Command examples you can use as reference or template to create your own. Remember that you can get the api and helpers by doing:

let { api, helpers } = game.modules.get('commander');
api.register({ command data.. });
// you can now open the command-line and type, or..
api.execute(commandInput);

Boolean Argument

Shows a notification with the boolean argument the user inputs

const booleanArgCommand = {
  name: 'bool',
  namespace: 'mynamespace',
  schema: 'bool $bool',
  args: [
    { name: 'bool', type: 'boolean' },
  ],
  handler: ({ bool }) => {
    ui.notifications?.info(`[${bool}]`);
  },
};

Number Argument

Shows a notification with the number the user inputs

const numberArgCommand = {
  name: 'number',
  namespace: 'mynamespace',
  schema: 'number $num',
  args: [
    { name: 'num', type: 'number' },
  ],
  handler: ({ num }) => {
    ui.notifications?.info(`[${num}]`);
  },
};

String Argument

Shows a notification with the text the user inputs

const stringArgCommand = {
  name: 'str',
  namespace: 'mynamespace',
  schema: 'str $text',
  args: [
    { name: 'text', type: 'string' },
  ],
  handler: ({ text }) => {
    ui.notifications?.info(`[${text}]`);
  },
};

Raw Argument

Shows a notification with the string content of whatever is after the command name

const rawArgCommand = {
  name: 'raw',
  namespace: 'mynamespace',
  schema: 'raw $value',
  args: [
    { name: 'value', type: 'raw' },
  ],
  handler: ({ value }) => {
    ui.notifications?.info(`[${value}]`);
  },
};

Multiple Arguments (sum)

Shows a notification with the sum of two numbers

const sumCommand = {
    name: "sum",
    namespace: "mynamespace",
    schema: "sum $a $b", // this is what you write to use this command, replacing $a and $b for numbers
    args: [
        { name: "a", type: "number" },
        { name: "b", type: "number" },
    ],
    handler: ({a, b}) => { // param names need to match the names or the args[]
      ui.notifications?.info("the sum is "+(a+b))
    }
}

Restricted access using Helpers (allow function)

Log on the console the user input (except for the command), but only available for ASSISTANT and GAMEMASTER roles

const logInputCommand = {
    name: "loginput",
    namespace: "mynamespace",
    schema: "loginput $text $args", // remember RAW arguments last.
    args: [
        { name: "text", type: "string" },
        { name: "args", type: "raw" },
    ],
    allow: () => helpers.hasRole('ASSISTANT'), // we use a helper to define the allow function
    handler: ({text, args}) => { 
      console.log(`The text argument is: ${text}`)
      console.log(args)
    }
}

Restricted access using custom Allow functions

Shows a notification with the token name, but only appears if you have a token selected

const logInputCommand = {
    name: "token",
    namespace: "mynamespace",
    schema: "token",
    args: [],
    allow: () => game.canvas.tokens.controlled.length,
    handler: () => { 
      ui.notifications?.info(`The token is called ${game.canvas.tokens.controlled[0].name}`)
    }
}

Argument suggestions

Asks for a player name, then a level 1-20, then anything, and a boolean at the end.

const suggestionsCommand = {
  name: 'sug',
  namespace: 'mynamespace',
  schema: 'sug $player $level $stuff $bool',
  args: [
    { 
      name: 'player',
      type: 'string',
      suggestions: () => {
        const users = game.users?.values();
        if (!users) return [];
        return [...users].map((u) => ({ content: u.name! }));
      },
    },
    {
      name: 'level',
      type: 'number',
      suggestions: () => {
        return Array.fromRange(20).map((n) => ({ content: n + 1 + '' }));
      },
    },
    {
      name: 'stuff',
      type: 'string',
    },
    {
      name: 'bool',
      type: 'boolean',
    },
  ],
  handler: ({ player, level, bool, stuff }) => {
    ui.notifications?.info(`player: [${player}] - level: [${level}] - stuff: [${stuff}] - bool: [${bool}]`);
  },
};