feat: Add 'queue clear' command

This commit is contained in:
Roman 2023-06-06 17:01:17 -04:00
parent 7d57478dda
commit d24e6530b7
3 changed files with 37 additions and 7 deletions

View file

@ -16,7 +16,8 @@ defmodule Sergei.Commands do
] ]
@queue_opts [ @queue_opts [
opt.(1, "add", "Add a song to the queue", options: @queue_add_opts) opt.(1, "add", "Add a song to the queue", options: @queue_add_opts),
opt.(1, "clear", "Clear the queue", [])
] ]
@slash_commands [ @slash_commands [

View file

@ -1,6 +1,4 @@
defmodule Sergei.Commands.Queue do defmodule Sergei.Commands.Queue do
require Logger
@spec handle(integer(), String.t(), [%{name: String.t(), value: String.t()}]) :: @spec handle(integer(), String.t(), [%{name: String.t(), value: String.t()}]) ::
{:ok, String.t()} | {:err, String.t()} {:ok, String.t()} | {:err, String.t()}
def handle(guild_id, "add", opts) do def handle(guild_id, "add", opts) do
@ -14,10 +12,16 @@ defmodule Sergei.Commands.Queue do
:not_playing -> :not_playing ->
{:ok, "I'm not playing anything right now."} {:ok, "I'm not playing anything right now."}
end
end
{:error, err} -> def handle(guild_id, "clear", _opts) do
Logger.error("Failed to queue media: #{err}") case Sergei.Player.queue_clear(guild_id) do
{:error, "This is embarassing..."} :ok ->
{:ok, "Queue cleared."}
:not_playing ->
{:ok, "I'm not playing anything right now."}
end end
end end
end end

View file

@ -24,11 +24,16 @@ defmodule Sergei.Player do
GenServer.call(__MODULE__, {:play, guild_id, channel_id, url}) GenServer.call(__MODULE__, {:play, guild_id, channel_id, url})
end end
@spec queue_add(integer(), String.t()) :: :ok | :not_playing | {:error, String.t()} @spec queue_add(integer(), String.t()) :: :ok | :not_playing
def queue_add(guild_id, url) do def queue_add(guild_id, url) do
GenServer.call(__MODULE__, {:queue_add, guild_id, url}) GenServer.call(__MODULE__, {:queue_add, guild_id, url})
end end
@spec queue_clear(integer()) :: :ok | :not_playing
def queue_clear(guild_id) do
GenServer.call(__MODULE__, {:queue_clear, guild_id})
end
@spec pause(integer()) :: :ok | :not_playing | {:error, String.t()} @spec pause(integer()) :: :ok | :not_playing | {:error, String.t()}
def pause(guild_id) do def pause(guild_id) do
GenServer.call(__MODULE__, {:pause, guild_id}) GenServer.call(__MODULE__, {:pause, guild_id})
@ -127,6 +132,11 @@ defmodule Sergei.Player do
end end
# Queue # Queue
@impl true
def handle_call({:queue_add, guild_id, _}, _from, state) when not is_map_key(state, guild_id) do
{:reply, :not_playing, state}
end
@impl true @impl true
def handle_call({:queue_add, guild_id, url}, _from, state) do def handle_call({:queue_add, guild_id, url}, _from, state) do
%{queue: queue} = Map.fetch!(state, guild_id) %{queue: queue} = Map.fetch!(state, guild_id)
@ -143,11 +153,26 @@ defmodule Sergei.Player do
end end
# Guard: Ensure Sergei is playing something in the guild # Guard: Ensure Sergei is playing something in the guild
# All commands below this point assume that this is true
@impl true @impl true
def handle_call({_, guild_id}, _from, state) when not is_map_key(state, guild_id) do def handle_call({_, guild_id}, _from, state) when not is_map_key(state, guild_id) do
{:reply, :not_playing, state} {:reply, :not_playing, state}
end end
# Queue Clear
@impl true
def handle_call({:queue_clear, guild_id}, _from, state) do
{
:reply,
:ok,
Map.update!(
state,
guild_id,
&%{&1 | queue: :queue.new()}
)
}
end
# Pause # Pause
@impl true @impl true
def handle_call({:pause, guild_id}, _from, state) do def handle_call({:pause, guild_id}, _from, state) do