Nosedrum.Interactor behaviour (nosedrum v0.5.0) View Source

Interactors take the role of both Nosedrum.Invoker and Nosedrum.Storage when it comes to Discord's Application Commands. An Interactor 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, an Interactor 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, as outlined in the official documentation.

The name or pid of the Interactor process.

Callbacks

Add a new command under the given name or application command path. Returns :ok if successful, and {:error, reason} otherwise.

Handle an Application Command invocation.

Remove the command under the given name or application command path. Returns :ok if successful, and {:error, reason} otherwise.

Functions

Responds to an Interaction with the values in the given Nosedrum.ApplicationCommand.response/0. Returns {:ok} if successful, and a Nostrum.Api.error/0 otherwise.

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, as outlined in the official documentation.

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."} => [],
      # ...
    }
}

Specs

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

Specs

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

The name or pid of the Interactor 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, reason :: String.t()}

Add a new command under the given name or application command path. Returns :ok if successful, and {:error, reason} otherwise.

If the command already exists, it will be overwritten.

Link to this callback

handle_interaction(interaction, name_or_pid)

View Source

Specs

handle_interaction(interaction :: Nostrum.Struct.Interaction.t(), name_or_pid()) ::
  :ok

Handle an Application Command invocation.

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

Example using Nosedrum.Interactor.Dispatcher:

# In your `Nostrum.Consumer` file:
def handle_event({:INTERACTION_CREATE, interaction, _ws_state}) do
  IO.puts "Got interaction"
  Nosedrum.Interactor.Dispatcher.handle_interaction(interaction)
end
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, reason :: String.t()}

Remove the command under the given name or application command path. 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

respond(interaction, command_response)

View Source

Specs

Responds to an Interaction with the values in the given Nosedrum.ApplicationCommand.response/0. Returns {:ok} if successful, and a Nostrum.Api.error/0 otherwise.