Nosedrum.TextCommand.Invoker.Split (nosedrum v0.6.0) View Source

An OptionParser.split/1-based command processor.

This parser supports command prefixes configured via the nosedrum.prefix configuration variable. You can specify a single prefix (as a string):

config :nosedrum,
  prefix: "!"

Or a multiple prefixes (as a list of strings):

config :nosedrum,
  prefix: ["!", "?"]

If multiple prefixes are specified, the first match in the list will be used. For example, if the prefixes are ["a", "ab"], then the message "ab foo" will invoke the command b, with the argument foo. However, if the prefixes were ordered ["ab", "a"], the message "abfoo" would invoke the command foo with no arguments.

The default prefix is ., and the prefix are looked up at compilation time due to the nature of Elixir's binary matching. This means that if you change your prefix, you need to recompile this module, usually using mix deps.compile --force nosedrum.

This invoker checks predicates and returns predicate failures to the caller.

Link to this section Summary

Link to this section Functions

Link to this function

handle_message(message, storage \\ Nosedrum.TextCommand.Storage.ETS, storage_process \\ :nosedrum_commands)

View Source

Specs

handle_message(Nostrum.Struct.Message.t(), module(), atom() | pid()) ::
  :ignored
  | {:error, {:unknown_subcommand, String.t(), :known, [String.t() | :default]}}
  | {:error, :predicate, {:error | :noperm, any()}}
  | any()

Handle the given message.

This involves checking whether the message starts with the given prefix, splitting the message into command and arguments, looking up a candidate command, and finally resolving it and invoking the module.

Arguments

  • message: The message to handle.
  • storage: The storage implementation the command invoker should use.
  • storage_process: The storage process, ETS table, or similar that is used by the storage process. For instance, this allows you to use different ETS tables for the Nosedrum.TextCommand.Storage.ETS module if you wish.

Return value

Returns :ignored if one of the following applies:

  • The message does not start with the configured prefix.
  • The message only contains the configured prefix.
  • No command could be looked up that matches the command the message invokes.

Examples

iex> Nosedrum.TextCommand.Invoker.Split.handle_message(%{content: "foo"})
:ignored
iex> Nosedrum.TextCommand.Invoker.Split.handle_message(%{content: "."})
:ignored