feat: add /song command

/song command will show the song currently playing.
This commit is contained in:
Roman 2023-06-06 17:01:17 -04:00
parent c4834abfe3
commit ffcc3f6b66
2 changed files with 30 additions and 1 deletions

View file

@ -18,7 +18,8 @@ defmodule Sergei.Consumer do
{"ping", "Pong", []},
{"play", "Play a song or resume playback", @play_opts},
{"pause", "Pause media playback", []},
{"stop", "Stop media playback and leave the voice channel", []}
{"stop", "Stop media playback and leave the voice channel", []},
{"song", "What song is currently playing?", []}
]
def start_link do
@ -159,4 +160,15 @@ defmodule Sergei.Consumer do
{:error, "This is embarrasing..."}
end
end
# /song
def do_command(%{guild_id: guild_id, data: %{name: "song"}}) do
case Sergei.Player.get_current_song(guild_id) do
:not_playing ->
{:ok, "I'm not playing anything right now."}
url ->
{:ok, url}
end
end
end

View file

@ -35,6 +35,11 @@ defmodule Sergei.Player do
GenServer.call(__MODULE__, {:stop, guild_id})
end
@spec get_current_song(integer()) :: String.t() | :not_playing
def get_current_song(guild_id) do
GenServer.call(__MODULE__, {:get_current_song, guild_id})
end
# Server
@impl true
def handle_info(:tick, state) do
@ -103,6 +108,18 @@ defmodule Sergei.Player do
{:reply, :ok, Map.delete(state, guild_id)}
end
@impl true
def handle_call({:get_current_song, guild_id}, _from, state) do
res =
case Map.get(state, guild_id) do
%{url: url} -> url
nil -> :not_playing
_ -> Logger.error("error: Guild found, but no URL is given")
end
{:reply, res, state}
end
def play_music(guild_id, channel_id, url) do
cond do
Voice.get_channel_id(guild_id) != channel_id ->