command.js

1.0.0

Changelog

1.4.3 - Unreleased

  • added checks to prevent erros on old versions

1.4.2 - 08.11.2019

  • added command usage logging
  • respect ignore settings for server/channel/private chat

1.4.1 - 23.10.2019

  • fixed missing permission check
  • added tests to prevent missing permission checks

1.4.0 - 20.10.2019

  • refactoring of the code base see REFACTOR_NOTE.md
  • checkPermissions now allows Promise responses
  • show alias inside man command
  • commands are now case insensitive
  • teamspeak now sends less seperat chat commands when using the help command
  • added a few tests

1.3.1 - 03.08.2019

  • added new possibility to create arguments
  createCommand("foo")
    .addArgument(args => args.number.setName("bar").positive())

1.3.0 - 11.06.2019

  • added better support for message events from discord

1.2.3 - 14.04.2019

  • fixed GroupArguments

1.2.2 - 10.02.2019

  • reworked responses especially for discord
  • ClientArgument should now work with discord

1.2.1 - 09.02.2019

  • improved error handling messages
  • improved man command (the command !man !help will now resolve to the help command aswell)

1.2.0 - 05.02.2019

  • implemented command throttling
  • added method #getVersion() to retrieve the current semantic version string

1.1.3 - 04.02.2018

  • changed help output for discord

1.1.2 - 30.01.2018

  • changed error handling for PermissionError

1.1.1 - 17.01.2019

  • changed error handling for failing permission checks
  • fixed client object not passed to the checkPermission Parent of a CommandGroup

1.1.0 - 17.01.2019

  • added createCommandGroup to create a CommandGroup with multiple SubCommands for simpler handling of complex commands
  • refactored command handling
  • set default config value for NOT_FOUND_MESSAGE to "1"
  • moved getCommandByName to CommandCollector
  • moved getAvailableCommands to CommandCollector
  • moved multiple log messages to VERBOSE logging
  • fixed documentation
  • removed alias from commands for the sake of simplicity
  • removed method ignoreOptionalArgs to enforce a more strict parsing

1.0.1 - 14.01.2019

  • fixed a bug when the prefix has been removed from the instance settings

1.0.0 - 10.12.2018

  • initial release

Examples

Examples
Static Members
▸ How to load the library
▸ Command come
▸ Command ping [amount]
▸ Command mass [chat|poke] [message]
▸ Command greet [client]
▸ Async example
▸ parse args by yourself
▸ CommandGroup example

Classes

Definition for Classes

Command

Command
Instance Members
▸ getUsage()
▸ hasPermission(client)
▸ addArgument(arg)
▸ getArguments()
▸ validate(args)
▸ dispatch(args, ev)
▸ validateArgs(args)

CommandGroup

CommandGroup
Instance Members
▸ getUsage()
▸ hasPermission(client)
▸ addCommand(name)
▸ findCommandByName(name)
▸ getAvailableCommands(client?, cmd?)
▸ dispatch(args, ev)

BaseCommand

BaseCommand
Instance Members
▸ _collector
▸ _execHandler
▸ getUsage()
▸ hasPermission(client)
▸ validate(args)
▸ dispatch(args, ev)
▸ alias(alias)
▸ isEnabled()
▸ enable()
▸ disable()
▸ getCommandName()
▸ getAlias()
▸ getFullCommandName()
▸ getFullAlias()
▸ getCommandNames()
▸ getFullCommandNames()
▸ getHelp()
▸ help(text)
▸ hasHelp()
▸ getManual()
▸ hasManual()
▸ forcePrefix(prefix)
▸ getPrefix()
▸ manual(text)
▸ clearManual()
▸ exec(callback)
▸ addThrottle(throttle)
▸ checkPermission(callback)
▸ isAllowed(client)
▸ _dispatchCommand(ev)

Collector

Collector
Static Members
▸ getCommandPrefix()
▸ createThrottle()
▸ getReplyOutput(event)
▸ checkPermissions(commands, client)
▸ isValidCommandName(name)
Instance Members
▸ getAvailableCommands(name)
▸ getAvailableCommandsByPermission(client)
▸ getAvailableCommandsWithPrefix(name)
▸ isPossibleCommand(text)
▸ registerCommand(name)
▸ registerCommandGroup(name)
▸ isSaveCommand(cmd)

Throttle

Throttle
Instance Members
▸ tickRate(duration)
▸ penaltyPerCommand(amount)
▸ restorePerTick(amount)
▸ initialPoints(initial)
▸ throttle(client)
▸ isThrottled(client)
▸ timeTillNextCommand(client)

Argument Types

Available Argument Types which can be added to a Command

Argument

Argument
Static Members
▸ createArgumentLayer()
Instance Members
▸ validate(args)
▸ optional(fallback?, displayDefault = true)
▸ getDefault()
▸ hasDefault()
▸ getManual()
▸ isOptional()
▸ setName(name, display?)
▸ getName()

NumberArgument

NumberArgument
Instance Members
▸ validate(args)
▸ min(min)
▸ max(max)
▸ integer()
▸ positive()
▸ negative()

StringArgument

StringArgument
Instance Members
▸ validate(args)
▸ _validate(arg, rest)
▸ match(regex)
▸ max(len)
▸ min(len)
▸ forceUpperCase()
▸ forceLowerCase()
▸ whitelist(words)

ClientArgument

Class representing a ClientArgument this Argument is capable to parse a Client UID or a simple UID inside the exec function it will resolve the found uid

ClientArgument
Instance Members
▸ validate(args)

RestArgument

RestArgument
Instance Members
▸ validate(args)

GroupArgument

GroupArgument
Instance Members
▸ validate(args)
▸ addArgument(arg)

Callbacks

all callbacks existing

permissionHandler

callback for the command event

permissionHandler(invoker: Client)

Type: Function

Parameters
invoker (Client)

createArgumentHandler

callback for the command event

createArgumentHandler(arg: ArgType): Argument

Type: Function

Parameters
arg (ArgType)
Returns
Argument:

execHandler

callback for the command event

execHandler(invoker: Client, args: Record<string, any>, event: MessageEvent)

Type: Function

Parameters
invoker (Client)
args (Record<string, any>)
event (MessageEvent)

Interfaces

type declarations

ArgType

ArgType

Type: object

Properties
string (StringArgument)
number (NumberArgument)
client (ClientArgument)
rest (RestArgument)

Exports

These Functions gets exported as object when imported through require("command.js")

createCommand

createCommand(cmd: string): Command
Parameters
cmd (string) the command which should be added
Returns
Command: returns the created Command

createCommandGroup

createCommandGroup(cmd: string): CommandGroup
Parameters
cmd (string) the command which should be added
Returns
CommandGroup: returns the created CommandGroup instance

createArgument

createArgument(type: any): Argument
Parameters
type (any)
Returns
Argument: returns the created Argument

createGroupedArgument

createGroupedArgument(type: ("or" | "and")): GroupArgument
Parameters
type (("or" | "and")) the argument type which should be created either "or" or "and" allowed
Returns
GroupArgument: returns the created Group Argument

getCommandPrefix

getCommandPrefix(): string
Returns
string: returns the command prefix

collector

collector

getVersion

getVersion(): string
Returns
string: returns the semantic version of this script

createThrottle

createThrottle(): Throttle
Returns
Throttle: returns the created Throttle

Type: string

Type: Client