Back to Top

ghastly master

A powerful and flexible library for modularizing Discord.js clients.

new Ghastly()

Returns

Void

Ghastly.constructor(options)

Constructor.

Parameters

Name Type Description
options ClientOptions
  • The options for the client.
options.prefix PrefixType
  • The prefix for the client's dispatcher. If a function is provided, it is given a received Message as an argument and must return a boolean indicating if it passes the filter.

Returns

Void

this.commands()

The command registry for the client.

Returns

Void

this.services()

The client's service container.

Returns

Void

this.dispatcher() private property

The client's dispatcher.

Returns

Void

new DeferredFilter()

Filters out messages based on the return value of a closure.

Returns

Void

DeferredFilter.constructor(filter)

Constructor.

Parameters

Name Type Description
filter Function
  • The filter.

Returns

Void

DeferredFilter.test(message)

Tests a message and determines if it passes the filter.

Parameters

Name Type Description
message Message
  • The message.

Returns

boolean RegExp

Returns false if the message does not pass the filter, else a RegExp which represents a prefix to test against the message.

RESPONSE_TYPES()

Response type strings.

Returns

Void

new Dispatcher()

Returns

Void

Dispatcher.constructor(options)

Constructor.

Parameters

Name Type Description
options Object
  • The configuration values for the dispatcher.
options.client Ghastly
  • The client this dispatcher is attached to.
options.prefix PrefixType
  • The prefix for this dispatcher.

Returns

Void

this.client()

The client this dispatcher is attached to.

Returns

Void

this.prefix() private property

The raw prefix given as an option.

Returns

Void

this.prefixFilter() private property

The prefix filter constructed from the raw prefix.

Returns

Void

resolveResponseType(response) private method

Resolves a given response value to a type.

Parameters

Name Type Description
response
  • The response to resolve.

Returns

string

The type of the response, or null if not recognized.

createPrefixFilter(prefix) private method

Generates a prefix filter.

Parameters

Name Type Description
prefix PrefixType
  • The prefix.

Returns

PrefixFilter

The prefix filter.

shouldFilterEvent(message[, newMessage]) private method

Determines if a message event should be filtered from the handler.

Parameters

Name Type Description
message Message
  • A Discord.js Message object.
newMessage Message
  • A Discord.js Message object. Should be received only when the message event was an update.
Optional

Returns

boolean

true if the message should be filtered, else false.

shouldFilterPrefix(prefix, message) private method

Determines if a message should be filtered from the handler based on the given prefix.

Parameters

Name Type Description
prefix RegExp
  • The prefix to test the message against.
message Message
  • The message contents.

Returns

boolean

true if the message should be filtered, else false.

new PrefixFilter()

Filters out messages based on a prefix.

Returns

Void

PrefixFilter.constructor(filter)

Constructor.

Parameters

Name Type Description
filter
  • The filter.

Returns

Void

this.filter()

The filter.

Returns

Void

test(message)

Tests a message and determines if it passes the filter.

Parameters

Name Type Description
message Message
  • The message.

Returns

boolean

true if the message passes the filter, else false.

new RegexFilter()

Filters out messages based on a RegEx.

Returns

Void

RegexFilter.constructor(filter)

Constructor.

Parameters

Name Type Description
filter RegExp
  • The filter.

Returns

Void

RegexFilter.test(message)

Tests a message and determines if it passes the filter.

Parameters

Name Type Description
message Message
  • The message.

Returns

boolean

true if the message passes the filter, else false.

new ConstructedService()

Returns

Void

ConstructedService.constructor(aliases, builder)

Constructor.

Parameters

Name Type Description
aliases Array.<string>
  • Aliases for this entry.
builder Function
  • The service builder.

Returns

Void

ConstructedService.service()

The service.

Returns

Void

new InstanceService()

Returns

Void

InstanceService.constructor(aliases, instance)

Constructor.

Parameters

Name Type Description
aliases Array.<string>
  • Aliases for this entry.
instance
  • The service instance.

Returns

Void

InstanceService.service()

The service.

Returns

Void

new Service()

Returns

Void

Service.constructor(options)

Constructor.

Parameters

Name Type Description
options Object
  • Options for the entry.
options.aliases= Array.<string>
  • Aliases for this entry.
Optional
options.builder=null Function
  • The service builder. Only applicable to constructed and singleton services.
Optional
options.instance=null
  • The service instance. Only applicable to singleton and instance services.
Optional

Returns

Void

this.aliases()

Aliases for this entry.

Returns

Void

this.builder()

The service builder. Only applicable to constructed and singleton services.

Returns

Void

this.instance()

The service instance. Only applicable to singleton and instance services.

Returns

Void

new ServiceContainer()

Returns

Void

ServiceContainer.constructor()

Constructor.

Returns

Void

this.services() private property

The services stored in this container.

Returns

Void

this.aliases() private property

The identifier aliases in this container.

Returns

Void

mainBindings()

Returns an iterator containing the main service bindings that have been registered.

Returns

Void

construct(identifier, builder)

Binds a service to the container which is rebuilt each time it is fetched.

Parameters

Name Type Description
identifier string Array.<string>
  • The identifier of the service. If given an array, additional identifiers are added as service aliases.
builder Function
  • The function which will build the service.

Returns

ServiceContainer

The instance this method was called on.

singleton(identifier, builder)

Binds a service to the container which is built once and cached for subsequent fetches.

Parameters

Name Type Description
identifier string Array.<string>
  • The identifier of the service. If given an array, additional identifiers are added as service aliases.
builder Function
  • The function which will build the service.

Returns

ServiceContainer

The instance this method was called on.

instance(identifier, value)

Binds a service to the container which is returned as a value each time it is fetched.

Parameters

Name Type Description
identifier string Array.<string>
  • The identifier of the service. If given an array, additional identifiers are added as service aliases.
value
  • The value to store as an instance.

Returns

ServiceContainer

The instance this method was called on.

bindProviders(providers)

Binds services to the service container via service providers.

Parameters

Name Type Description
providers serviceProvider
  • The service providers.

Returns

Dispatcher

The instance this method was called on.

unbind(identifier)

Removes a binding from the container, along with its aliases.

Parameters

Name Type Description
identifier string
  • The identifier or an alias for the service.

Returns

ServiceContainer

The instance this method was called on.

has(identifier)

Determines if an identifier has been bound in the container.

Parameters

Name Type Description
identifier string
  • The identifier.

Returns

boolean

True if the identifier was bound.

get(identifier)

Fetches a service from the container, constructing it if necessary.

Parameters

Name Type Description
identifier string
  • The identifier of the service to fetch.

Returns

The service requested, or null if no service was bound under that identifier.

getMainBinding(alias) private method

Returns the main bound identifier for an alias. If the alias is already a main bound identifier, that identifier is returned directly.

Parameters

Name Type Description
alias string
  • The alias.

Returns

string

The main bound identifier for the given alias, or undefined if this alias was not previously bound.

addService(type, identifier, service) private method

Adds a service to the container.

Parameters

Name Type Description
type string
  • The type of service being added.
identifier string Array.<string>
  • The identifier of the service. If given an array, additional identifiers are added as service aliases.
service
  • The service being added.

Returns

ServiceContainer

The instance this method was called on.

new SingletonService()

Returns

Void

SingletonService.constructor(aliases, builder)

Constructor.

Parameters

Name Type Description
aliases Array.<string>
  • Aliases for this entry.
builder Function
  • The service builder.

Returns

Void

SingletonService.service()

The service.

Returns

Void

this.instance()

Returns

Void

new CommandGroup()

Returns

Void

CommandGroup.constructor(name)

Constructor.

Parameters

Name Type Description
name string
  • The name of the group.

Returns

Void

this.name()

The name of the group.

Returns

Void

this.commands() private property

The commands that are a part of the group.

Returns

Void

this.middleware() private property

The middleware applied to the group.

Returns

Void

add(command)

Adds a command to the group.

Parameters

Name Type Description
command CommandObject
  • The command.

Returns

CommandGroup

The instance this method was called on.

applyMiddleware(layers)

Adds the given middleware to the current group middleware layers.

Parameters

Name Type Description
layers middlewareLayer
  • The layers to apply.

Returns

CommandGroup

The instance this method was called on.

new CommandObject()

Returns

Void

CommandObject.constructor(configuration)

Constructor.

Parameters

Name Type Description
configuration CommandConfiguration
  • The command configuration.

Returns

Void

this.name()

The main trigger of the command, also acting as its name.

Returns

Void

this.aliases()

An array of aliases for the command.

Returns

Void

this.parameters()

An array of parameter definitions for the command.

Returns

Void

this.group()

The name of the group this command is part of, if any.

Returns

Void

this.description()

The description for the command.

Returns

Void

this.dependencies()

A mapping between service names and the context names they should be injected under for the command handler.

Returns

Void

this.handler() private property

The command handler function with middleware applied to it.

Returns

Void

this.originalHandler() private property

The original command handler function.

Returns

Void

this.middleware() private property

The middleware applied to the handler function.

Returns

Void

generateHandler(handler, middleware) private method

Applies middleware to a command handler and returns the resulting handler function.

Parameters

Name Type Description
handler Function
  • The command handler.
middleware Array.<middlewareLayer>
  • The middleware to apply.

Returns

Function

The command handler with middleware applied to it.

handle(context)

Executes the command handler.

Parameters

Name Type Description
context Object
  • The command context.

Returns

Promise.<Object>

Promise resolving to the response object.

linkGroup(commandGroup)

Subscribes to updates from a command group.

Parameters

Name Type Description
commandGroup CommandGroup
  • The command group.

Returns

Void

new CommandRegistry()

Returns

Void

CommandRegistry.constructor()

Constructor.

Returns

Void

this.commands() private property

The commands stored in this registry.

Returns

Void

this.aliases() private property

The command aliases stored in this registry.

Returns

Void

this.groups() private property

The command groups stored in the registry.

Returns

Void

add(configurators)

Adds the given commands to the registry.

Parameters

Name Type Description
configurators commandConfigurator
  • The command configurators.

Returns

CommandRegistry

The instance this method was called on.

get(identifier)

Gets a command from the registry.

Parameters

Name Type Description
identifier string
  • The identifier of the command in the registry, or an alias of a command.

Returns

CommandObject

The CommandObject in the registry, or null if it couldn't be found.

applyGroupMiddleware(group, middleware)

Applies middleware to a command group.

Parameters

Name Type Description
group string
  • The command group to apply middleware to.
middleware Array.<middlewareLayer>
  • The middleware to apply.

Returns

CommandRegistry

The instance this method was called on.

addCommand(command) private method

Adds a command to the registry.

Parameters

Name Type Description
command CommandObject
  • The command to register.

Returns

CommandRegistry

The instance this method was called on.

getMainName(identifier) private method

Returns the main name associated with an identifier.

Parameters

Name Type Description
identifier string
  • The identifier.

Returns

string

The main name associated with this identifier, or undefined if none exists.

alias(name, aliases) private method

Adds command aliases to the registry.

Parameters

Name Type Description
name string
  • The name of the command to add aliases to.
aliases Array.<string>
  • The command aliases.

Returns

CommandRegistry

The instance this method was called on.

new ArgumentParser()

Returns

Void

ArgumentParser.parse(rules, args)

Parses an array of arguments for a command call.

Parameters

Name Type Description
rules Array.<ParsedParameter>
  • An array of rules for the command being evaluated whose order matches that of the arguments.
args string
  • A string containing the arguments for this command.

Returns

Object

The parsed arguments given in a mapping between argument names and values.

ArgumentParser.normalizeArgumentType(type, argument)

Converts an argument to the given type, ignoring string arguments.

Parameters

Name Type Description
type string
  • The type the argument should be converted to.
argument string
  • The argument to convertType.

Returns

ParameterType

The converted argument.

new CommandParser()

Returns

Void

CommandParser.parse(message, prefix)

Parses the given message for a command.

Parameters

Name Type Description
message Message
  • The Discord.js Message object.
prefix RegExp
  • The prefix to remove from the message.

Returns

ParsedCommand

Data parsed from the message content.

new ParameterParser()

Returns

Void

ParameterParser.validate(parameters)

Validates a set of parameters.

Parameters

Name Type Description
parameters ParameterDefinition string
  • The parameters to validate.

Returns

Array.<ParsedParameter>

The validated parameters.

ParameterParser.parse(parameters)

Parses a set of parameters into ParsedParameter objects.

Parameters

Name Type Description
parameters ParameterDefinition string
  • The parameters to parse.

Returns

Array.<ParsedParameter>

The parsed parameters.

ParameterParser.parseParameter(parameter)

Parses the given command parameter definition and returns an object containing data on it.

Parameters

Name Type Description
parameter String
  • The command parameter definition.

Returns

ParameterDefinition

Object containing data on the command parameter.

ParameterParser.parseDefinition(definition)

Parses the definition portion of a parameter string.

Parameters

Name Type Description
definition string
  • The definition portion of a parameter string.

Returns

Object

An object containing data on the parsed parameter definition.

new ParsedCommand()

Returns

Void

ParsedCommand.constructor(values)

Constructor.

Parameters

Name Type Description
values Object
  • The parsed data.
values.raw string
  • The raw message content.
values.trimmed string
  • The raw message content with any command prefix trimmed from the start.
values.identifier string
  • The name of the command being invoked.
values.args Array.<string>
  • A space-delimited array of arguments that were given as part of the command.
values.rawArgs string
  • The delimited arguments joined together as a string.

Returns

Void

this.raw()

The raw message content.

Returns

Void

this.trimmed()

The raw message content with any command prefix trimmed from the start.

Returns

Void

this.identifier()

The name of the command being invoked.

Returns

Void

this.args()

A space-delimited array of arguments that were given as part of the command.

Returns

Void

this.rawArgs()

The delimited arguments joined together as a string.

Returns

Void

new ParsedParameter()

Returns

Void

ParsedParameter.constructor(values)

Constructor.

Parameters

Name Type Description
values ParameterDefinition
  • The parsed data.

Returns

Void

this.name()

The name of the parameter.

Returns

Void

this.optional()

Whether or not the parameter is optional.

Returns

Void

this.description()

A description of the parameter.

Returns

Void

this.type()

The expected type of the parameter.

Returns

Void

this.repeatable()

Whether or not the parameter accepts a variable number of input arguments.

Returns

Void

this.literal()

Whether or no the parameter is a literal string, i.e. takes the value of the input as given. Can only be applied to string parameters.

Returns

Void

this.defaultValue()

The default value of the parameter. This is non-null only if the parameter is optional. The default value for a repeatable parameter will be an array of values while non-repeatable parameters store a single primitive as a default value. The types of these values are determined by the parameters's type declaration defaulting to strings.

Returns

Void

new ArbitraryVoiceResponse()

Returns

Void

ArbitraryVoiceResponse.constructor(context, input[, options={}])

Constructor.

Parameters

Name Type Description
context Object
  • The handler context.
input string
  • The arbitrary input.
options={} StreamOptions
  • The options for playing the stream.
Optional

Returns

Void

new BroadcastVoiceResponse()

Returns

Void

BroadcastVoiceResponse.constructor(context, broadcast[, options={}])

Constructor.

Parameters

Name Type Description
context Object
  • The handler context.
broadcast VoiceBroadcast
  • The broadcast.
options={} StreamOptions
  • The options for playing the stream.
Optional

Returns

Void

new ConvertedVoiceResponse()

Returns

Void

ConvertedVoiceResponse.constructor(context, stream[, options={}])

Constructor.

Parameters

Name Type Description
context Object
  • The handler context.
stream ReadableStream
  • The stream.
options={} StreamOptions
  • The options for playing the stream.
Optional

Returns

Void

new FileVoiceResponse()

Returns

Void

FileVoiceResponse.constructor(context, file[, options={}])

Constructor.

Parameters

Name Type Description
context Object
  • The handler context.
file string
  • The absolute path to the file.
options={} StreamOptions
  • The options for playing the stream.
Optional

Returns

Void

new OpusVoiceResponse()

Returns

Void

OpusVoiceResponse.constructor(context, stream[, options={}])

Constructor.

Parameters

Name Type Description
context Object
  • The handler context.
stream ReadableStream
  • The Opus stream.
options={} StreamOptions
  • The options for playing the stream.
Optional

Returns

Void

new Response()

Returns

Void

Response.constructor(executor)

Constructor.

Parameters

Name Type Description
executor Function
  • The function which will handle the message response.

Returns

Void

this.executor() private property

The function handling the message response.

Returns

Void

respond()

Executes the message response handler.

Returns

The returned value from the handler.

new StreamVoiceResponse()

Returns

Void

StreamVoiceResponse.constructor(context, stream[, options={}])

Constructor.

Parameters

Name Type Description
context Object
  • The handler context.
stream ReadableStream
  • The audio stream.
options={} StreamOptions
  • The options for playing the stream.
Optional

Returns

Void

new VoiceResponse()

Returns

Void

VoiceResponse.constructor(context, inputMethod, payload[, options={}])

Constructor.

Parameters

Name Type Description
context Object
  • The handler context.
inputMethod VoiceInputMethod
  • The method of voice input to use.
payload
  • The source of the voice input.
options={} StreamOptions
  • The options for playing the stream.
Optional

Returns

Void

compose(functions)

Composes a set of functions into a chain of middleware. The last function acts as the innermost part of the middleware chain. For instance, compose(f, g) returns a function with contract (...args) => f(g, ...args).

Parameters

Name Type Description
functions Function
  • The set of functions to compose.

Returns

Function

The composed chain of middleware.

apply(middleware)

Takes a set of middleware functions and returns a function which consumes a single command handler function and produces a new handler function with the given middleware applied to it.

Parameters

Name Type Description
middleware middlewareLayer
  • The set of middleware to apply to the input handler.

Returns

Function

Function consuming a single command handler function and returns a new handler function with the given middleware applied to it.

DEFAULT_PROPERTIES()

Properties which are automatically handled when using object configurations.

Returns

Void

configure(configuration)

Provides a bridge to provide configuration data for command configurators. Consumes a config object containing the configuration data and produces a function. This function consumes a configurator and returns another configurator with the given configuration applied to it. Several configure() calls can be made successively on the returned configurator, with the outer config objects overwriting the keys in the innermost configs.

Parameters

Name Type Description
configuration Object
  • An object containing the configuration data to pass to the command configurator.

Returns

Function

A function consuming a configurator and producing another configurator with the provided config applied to it.

new ArgumentParserError()

Returns

Void

new CommandError()

Returns

Void

new CommandParserError()

Returns

Void

new ParameterParserError()

Returns

Void

expectDM()

Middleware which filters out messages not received in a DM context.

Returns

middlewareLayer

The layer which filters the messages.

expectGroupDM()

Middleware which filters out messages not received in a group DM context.

Returns

middlewareLayer

The layer which filters the messages.

expectGuild()

Middleware which filters out messages not received in a guild context.

Returns

middlewareLayer

The layer which filters the messages.

expectPermissions(permissions)

Middleware which filters out messages based on user permissions. Members must possess all of the given permissions to pass the filter. Only filters out messages if in a guild context.

Parameters

Name Type Description
permissions PermissionResolvable
  • The permissions required to pass the filter.

Examples

expectPermissions(
  'MUTE_MEMBERS',
  'DEAFEN_MEMBERS',
);

Returns

middlewareLayer

The layer which filters the messages.

expectRole(identifiers)

Middleware which filters out messages based on user role IDs or names. Only filters out messages if in a guild context.

Parameters

Name Type Description
identifiers string
  • The role IDs or names which are allowed to pass the filter.

Examples

expectRole(
  '750315226641451132',
  'Batman',
);

Returns

middlewareLayer

The layer which filters the messages.

expectUser(identifiers)

Middleware which filters out messages based on user IDs or usernames and discriminators.

Parameters

Name Type Description
identifiers string
  • The user IDs or username/discriminator values that are allowed to pass the filter.

Examples

expectUser(
  '275532051244111663',
  'LiamNeeson#5364',
);

Returns

middlewareLayer

The layer which filters the messages.

expectChannelType(type)

Middleware which filters out messages based on the channel type.

Parameters

Name Type Description
type string
  • The channel type required to pass the filter.

Returns

middlewareLayer

The layer which filters the messages.

new BaseResolver()

Returns

Void

BaseResolver.constructor()

Constructor.

Returns

Void

this.resolver()

The resolver.

Returns

Void

resolve([options={}])

Validates the given options object and returns a promise.

Parameters

Name Type Description
options={} Object
  • Options object to resolve.
Optional

Returns

Object

new CommandObjectResolver()

Returns

Void

CommandObjectResolver.constructor()

Constructor.

Returns

Void

new ParameterResolver()

Returns

Void

ParameterResolver.constructor()

Constructor.

Returns

Void

createResolver()

Custom version of krachot/options-resolver that supports synchronous resolution.

Returns

Object

new MarkdownFormatter()

Returns

Void

MarkdownFormatter.italic(text)

Formats a string as italic text.

Parameters

Name Type Description
text string
  • The text to format.

Returns

string

The formatted Markdown string.

MarkdownFormatter.bold(text)

Formats a string as bold text.

Parameters

Name Type Description
text string
  • The text to format.

Returns

string

The formatted Markdown string.

MarkdownFormatter.strikeout(text)

Formats a string as strikeout text.

Parameters

Name Type Description
text string
  • The text to format.

Returns

string

The formatted Markdown string.

MarkdownFormatter.underline(text)

Formats a string as underlined text.

Parameters

Name Type Description
text string
  • The text to format.

Returns

string

The formatted Markdown string.

MarkdownFormatter.code(text)

Formats a string as inline code.

Parameters

Name Type Description
text string
  • The text to format. Single backticks in the text do not need to be escaped.

Returns

string

The formatted Markdown string.

MarkdownFormatter.codeBlock(text[, syntax=''])

Formats a string as multi-line code.

Parameters

Name Type Description
text string
  • The text to format.
syntax='' string
  • The syntax highlighting to use.
Optional

Returns

string

The formatted Markdown string.

new StringMap()

Returns

Void

StringMap.constructor(iterable)

Constructor.

Parameters

Name Type Description
iterable
  • The iterable to use to construct the map.

Returns

Void

StringMap.delete(key)

Removes a key from the map.

Parameters

Name Type Description
key string
  • The key to remove.

Returns

boolean

True if an element is removed, else false.

StringMap.get(key)

Returns a value from the map.

Parameters

Name Type Description
key string
  • The key of the value to get.

Returns

The returned value if found, else undefined.

StringMap.has(key)

Checks if a key exists in the map.

Parameters

Name Type Description
key string
  • The key to check.

Returns

boolean

True if the key is found, else false.

StringMap.set(key, value)

Sets a value in the map.

Parameters

Name Type Description
key string
  • The key of the value to add.
value
  • The value to add.

Returns

StringMap

The instance this method was called on.