Create stop (disconnenct) command

This commit is contained in:
Roman 2023-06-06 17:01:17 -04:00
parent c2240be553
commit 6a5498410c
2 changed files with 31 additions and 2 deletions

View file

@ -16,9 +16,10 @@ defmodule Sergei.Consumer do
@slash_commands [
{"ping", "Pong", []},
{"play", "Play some tunes", @play_opts},
{"stop", "Stop media playback and leave the voice channel", []},
{"pause", "Pause media playback", []},
{"resume", "Resume media playback", []},
{"play", "Play some tunes", @play_opts}
{"resume", "Resume media playback", []}
]
def start_link do
@ -109,6 +110,21 @@ defmodule Sergei.Consumer do
end
end
# /stop
def do_command(%{guild_id: guild_id, data: %{name: "stop"}}) do
case Sergei.Player.stop(guild_id) do
:ok ->
{:ok, "Bye!"}
:not_playing ->
{:ok, "I'm not playing anything right now."}
{:error, err} ->
Logger.error("Failed to stop media: #{err}")
{:error, "This is embarrasing..."}
end
end
# /pause
def do_command(%{guild_id: guild_id, data: %{name: "pause"}}) do
case Sergei.Player.pause(guild_id) do

View file

@ -20,6 +20,11 @@ defmodule Sergei.Player do
GenServer.call(__MODULE__, {:play, guild_id, channel_id, url})
end
@spec stop(integer()) :: :ok | :not_playing | {:error, String.t()}
def stop(guild_id) do
GenServer.call(__MODULE__, {:stop, guild_id})
end
@spec pause(integer()) :: :ok | :not_playing | {:error, String.t()}
def pause(guild_id) do
GenServer.call(__MODULE__, {:pause, guild_id})
@ -62,6 +67,14 @@ defmodule Sergei.Player do
{:reply, :not_playing, state}
end
@impl true
def handle_call({:stop, guild_id}, _from, state) do
Voice.stop(guild_id)
Voice.leave_channel(guild_id)
{:reply, :ok, Map.delete(state, guild_id)}
end
@impl true
def handle_call({:pause, guild_id}, _from, state) do
%{url: url} = Map.fetch!(state, guild_id)