Nosedrum.Storage behaviour (nosedrum v0.6.0) View Source

Storages keep track of your Application Command names and their associated modules.

A Storage handles incoming Nostrum.Struct.Interaction.t/0s, invoking Nosedrum.ApplicationCommand.command/1 callbacks and responding to the Interaction.

In addition to tracking commands locally for the bot, a Storage is responsible for registering an Application Command with Discord when add_command/4 or remove_command/4 is called.

Link to this section Summary

Types

Defines a structure of commands, subcommands, subcommand groups.

The name or pid of the Storage process.

Callbacks

Add a new command under the given name or application command path.

Handle an Application Command invocation.

Remove the command under the given name or application command path.

Functions

Edits an interaction with a follow up response.

Link to this section Types

Link to this type

application_command_path()

View Source

Specs

application_command_path() :: %{
  required({group_name :: String.t(), group_desc :: String.t()}) => [
    application_command_path() | [Nosedrum.ApplicationCommand.option()]
  ]
}

Defines a structure of commands, subcommands, subcommand groups.

Note that Discord only supports nesting 3 levels deep, like command -> subcommand group -> subcommand.

Example path:

%{
  {"castle", MyApp.CastleCommand.description()} =>
    %{
      {"prepare", "Prepare the castle for an attack."} => [],
      {"open", "Open up the castle for traders and visitors."} => [],
      # ...
    }
}

References

Specs

command_scope() ::
  :global | Nostrum.Struct.Guild.id() | [Nostrum.Struct.Guild.id()]

Specs

name_or_pid() :: atom() | pid()

The name or pid of the Storage process.

Link to this section Callbacks

Link to this callback

add_command( name_or_path, command_module, scope, name_or_pid )

View Source

Specs

add_command(
  name_or_path :: String.t() | application_command_path(),
  command_module :: module(),
  scope :: command_scope(),
  name_or_pid()
) :: :ok | {:error, Nostrum.Error.ApiError.t()}

Add a new command under the given name or application command path.

If the command already exists, it will be overwritten.

Return value

Returns :ok if successful, and {:error, reason} otherwise.

Link to this callback

handle_interaction(interaction, name_or_pid)

View Source

Specs

handle_interaction(interaction :: Nostrum.Struct.Interaction.t(), name_or_pid()) ::
  {:ok}
  | {:ok, Nostrum.Struct.Message.t()}
  | {:error, :unknown_command}
  | Nostrum.Api.error()

Handle an Application Command invocation.

This callback should be invoked upon receiving an interaction via the :INTERACTION_CREATE event.

Example using Nosedrum.Storage.Dispatcher:

# In your `Nostrum.Consumer` file:
def handle_event({:INTERACTION_CREATE, interaction, _ws_state}) do
  IO.puts "Got interaction"
  Nosedrum.Storage.Dispatcher.handle_interaction(interaction)
end

Return value

Returns {:ok} on success, or {:ok, t:Nostrum.Struct.Message.t()} when deferring and the supplied callback completes with a successful edit of the original response. {:error, reason} is returned otherwise.

Link to this callback

remove_command( name_or_path, command_id, scope, name_or_pid )

View Source

Specs

remove_command(
  name_or_path :: String.t() | application_command_path(),
  command_id :: Nostrum.Snowflake.t(),
  scope :: command_scope(),
  name_or_pid()
) :: :ok | {:error, Nostrum.Error.ApiError.t()}

Remove the command under the given name or application command path.

Return value

Returns :ok if successful, and {:error, reason} otherwise.

If the command does not exist, no error should be returned.

Link to this section Functions

Link to this function

followup(interaction, callback_tuple)

View Source

Specs

Edits an interaction with a follow up response.

The response is obtained by running the given function/MFA tuple, see Nosedrum.ApplicationCommand.callback/0.

Return value

Returns {:ok,t:Nostrum.Struct.Message.t()} if successful, and a Nostrum.Api.error/0 otherwise.

Link to this function

respond(interaction, command_response)

View Source

Specs

Responds to an Interaction with the given Nosedrum.ApplicationCommand.response/0.

Return value

Returns {:ok} if successful, and a Nostrum.Api.error/0 otherwise.