Nosedrum.Storage behaviour (nosedrum v0.6.0) View Source
Storage
s keep track of your Application Command names and their associated modules.
A Storage
handles incoming Nostrum.Struct.Interaction.t/0
s, 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.
Responds to an Interaction with the given Nosedrum.ApplicationCommand.response/0
.
Link to this section Types
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
The name or pid of the Storage process.
Link to this section Callbacks
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.
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.
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
Specs
followup(Nostrum.Struct.Interaction.t(), Nosedrum.ApplicationCommand.callback()) :: {:ok, Nostrum.Struct.Message.t()} | Nostrum.Api.error()
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.
Specs
respond(Nostrum.Struct.Interaction.t(), Nosedrum.ApplicationCommand.response()) :: {:ok} | Nostrum.Api.error()
Responds to an Interaction with the given Nosedrum.ApplicationCommand.response/0
.
Return value
Returns {:ok}
if successful, and a Nostrum.Api.error/0
otherwise.