@discord-rose/flags-middleware
TypeScript icon, indicating that this package has built-in type declarations

1.0.2 • Public • Published

Flags Middleware

Flags middleware for discord-rose

Install

npm i @discord-rose/flags-middleware

Usage

Using the middleware will automatically parse traditional argv CLI options into CommandContext.flags, and change CommandContext.args to non-flags arguments. Using yargs

WARNING: Doing this will edit your ctx.args, which can cause unexpected behavior. It does this by removing any non-flagged arguments

const flagsMiddleware = require('@discord-rose/flags-middleware')

worker.commands
  .middleware(flagsMiddleware())
  .add({
    command: '!hello',
    exec: (ctx) => {
      if (ctx.flags.goodbye) ctx.reply('Bye!')
      else ctx.reply('World!')

      if (ctx.flags.abc === 'def') ctx.reply('Secret found ;)')
    }
  })

In this situation, if a user were to run !hello --goodbye will respond with the "Bye!" reply. And if someone runs !hello --abc def it will send the secret.

Anything without a value with by set to a boolean, check yargs for more info on how everything is parsed.

For example !hello world -abc --def ghi -j k Will result in

ctx.flags:

{
  a: true,
  b: true,
  c: true,
  def: 'ghi',
  j: 'k'
}

ctx.args: ['world'] (this is due to the afformentioned args editing)

Readme

Keywords

Package Sidebar

Install

npm i @discord-rose/flags-middleware

Weekly Downloads

0

Version

1.0.2

License

ISC

Unpacked Size

2.48 kB

Total Files

4

Last publish

Collaborators

  • jpbberry