From d24e6530b7bc6698fd5be5f4b0cfa762970b02c0 Mon Sep 17 00:00:00 2001 From: Roman Date: Tue, 6 Jun 2023 17:01:17 -0400 Subject: [PATCH] feat: Add 'queue clear' command --- lib/sergei/commands.ex | 3 ++- lib/sergei/commands/queue.ex | 14 +++++++++----- lib/sergei/player.ex | 27 ++++++++++++++++++++++++++- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/lib/sergei/commands.ex b/lib/sergei/commands.ex index c1ce294..e0da753 100644 --- a/lib/sergei/commands.ex +++ b/lib/sergei/commands.ex @@ -16,7 +16,8 @@ defmodule Sergei.Commands do ] @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 [ diff --git a/lib/sergei/commands/queue.ex b/lib/sergei/commands/queue.ex index 774ffd5..3188ece 100644 --- a/lib/sergei/commands/queue.ex +++ b/lib/sergei/commands/queue.ex @@ -1,6 +1,4 @@ defmodule Sergei.Commands.Queue do - require Logger - @spec handle(integer(), String.t(), [%{name: String.t(), value: String.t()}]) :: {:ok, String.t()} | {:err, String.t()} def handle(guild_id, "add", opts) do @@ -14,10 +12,16 @@ defmodule Sergei.Commands.Queue do :not_playing -> {:ok, "I'm not playing anything right now."} + end + end - {:error, err} -> - Logger.error("Failed to queue media: #{err}") - {:error, "This is embarassing..."} + def handle(guild_id, "clear", _opts) do + case Sergei.Player.queue_clear(guild_id) do + :ok -> + {:ok, "Queue cleared."} + + :not_playing -> + {:ok, "I'm not playing anything right now."} end end end diff --git a/lib/sergei/player.ex b/lib/sergei/player.ex index f2cf471..85cbab2 100644 --- a/lib/sergei/player.ex +++ b/lib/sergei/player.ex @@ -24,11 +24,16 @@ defmodule Sergei.Player do GenServer.call(__MODULE__, {:play, guild_id, channel_id, url}) 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 GenServer.call(__MODULE__, {:queue_add, guild_id, url}) 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()} def pause(guild_id) do GenServer.call(__MODULE__, {:pause, guild_id}) @@ -127,6 +132,11 @@ defmodule Sergei.Player do end # 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 def handle_call({:queue_add, guild_id, url}, _from, state) do %{queue: queue} = Map.fetch!(state, guild_id) @@ -143,11 +153,26 @@ defmodule Sergei.Player do end # Guard: Ensure Sergei is playing something in the guild + # All commands below this point assume that this is true @impl true def handle_call({_, guild_id}, _from, state) when not is_map_key(state, guild_id) do {:reply, :not_playing, state} 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 @impl true def handle_call({:pause, guild_id}, _from, state) do