Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore former add_command functionality #1

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/nosedrum/storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,27 @@ defmodule Nosedrum.Storage do
## Return value
Returns `:ok` if successful, and `{:error, reason}` otherwise.
"""
@callback add_command(
@callback queue_command(
name_or_path :: String.t() | application_command_path,
command_module :: module,
name_or_pid
) :: :ok | {:error, Nostrum.Error.ApiError.t()}

@doc """
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.
"""
@callback 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()}

@doc """
Remove the command under the given name or application command path.

Expand All @@ -124,7 +139,7 @@ defmodule Nosedrum.Storage do
## Return value
Returns `:ok` if successful, and `{:error, reason}` otherwise.
"""
@callback update_discord(
@callback process_queued_commands(
scope :: command_scope,
name_or_pid
) :: :ok | {:error, Nostrum.Error.ApiError.t()}
Expand Down
61 changes: 59 additions & 2 deletions lib/nosedrum/storage/dispatcher.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ defmodule Nosedrum.Storage.Dispatcher do
end

@impl true
def update_discord(scope, id \\ __MODULE__) do
def process_queued_commands(scope, id \\ __MODULE__) do
GenServer.call(id, {:submit, scope})
end

@impl true
def add_command(path, command, id \\ __MODULE__) do
def queue_command(path, command, id \\ __MODULE__) do
command_name =
if is_binary(path) do
path
Expand All @@ -72,6 +72,23 @@ defmodule Nosedrum.Storage.Dispatcher do
GenServer.call(id, {:add, command_name, command})
end

@impl true
def add_command(path, command, scope, id \\ __MODULE__) do
payload = build_payload(path, command)

command_name =
if is_binary(path) do
path
else
path
|> Enum.take(1)
|> List.first()
|> unwrap_key()
end

GenServer.call(id, {:add, payload, command_name, command, scope})
end

@impl true
def remove_command(name, command_id, scope, id \\ __MODULE__) do
GenServer.call(id, {:remove, name, command_id, scope})
Expand Down Expand Up @@ -116,6 +133,46 @@ defmodule Nosedrum.Storage.Dispatcher do
{:reply, :ok, Map.put(commands, name, command)}
end


@impl true
def handle_call({:add, payload, name, command, :global}, _from, commands) do
case Nostrum.Api.create_global_application_command(payload) do
{:ok, _} = response ->
{:reply, response, Map.put(commands, name, command)}

error ->
{:reply, {:error, error}, commands}
end
end

@impl true
def handle_call({:add, payload, name, command, guild_id_list}, _from, commands)
when is_list(guild_id_list) do
res =
Enum.reduce(guild_id_list, {[], []}, fn guild_id, {errors, responses} ->
case Nostrum.Api.create_guild_application_command(guild_id, payload) do
{:ok, _} = response ->
{errors, [response | responses]}

error ->
{[error | errors], responses}
end
end)

{:reply, res, Map.put(commands, name, command)}
end

@impl true
def handle_call({:add, payload, name, command, guild_id}, _from, commands) do
case Nostrum.Api.create_guild_application_command(guild_id, payload) do
{:ok, _} = response ->
{:reply, response, Map.put(commands, name, command)}

error ->
{:reply, {:error, error}, commands}
end
end

@impl true
def handle_call({:remove, name, command_id, :global}, _from, commands) do
case Nostrum.Api.delete_global_application_command(command_id) do
Expand Down