mirror of
https://codeberg.org/godmaire/sergei.git
synced 2024-09-20 00:08:25 +00:00
Use bare /play to resume playback
This replaces the /resume command since most people don't think "pause and resume" as much as "play and pause".
This commit is contained in:
parent
6525659522
commit
13614e0b7b
2 changed files with 31 additions and 36 deletions
|
@ -11,15 +11,14 @@ defmodule Sergei.Consumer do
|
||||||
end
|
end
|
||||||
|
|
||||||
@play_opts [
|
@play_opts [
|
||||||
opt.(3, "url", "URL of the audio to play", required: true)
|
opt.(3, "url", "URL of the audio to play", [])
|
||||||
]
|
]
|
||||||
|
|
||||||
@slash_commands [
|
@slash_commands [
|
||||||
{"ping", "Pong", []},
|
{"ping", "Pong", []},
|
||||||
{"play", "Play some tunes", @play_opts},
|
{"play", "Play a song or resume playback", @play_opts},
|
||||||
{"stop", "Stop media playback and leave the voice channel", []},
|
|
||||||
{"pause", "Pause media playback", []},
|
{"pause", "Pause media playback", []},
|
||||||
{"resume", "Resume media playback", []}
|
{"stop", "Stop media playback and leave the voice channel", []}
|
||||||
]
|
]
|
||||||
|
|
||||||
def start_link do
|
def start_link do
|
||||||
|
@ -89,15 +88,11 @@ defmodule Sergei.Consumer do
|
||||||
end
|
end
|
||||||
|
|
||||||
# /play <url>
|
# /play <url>
|
||||||
def do_command(
|
def do_command(%{
|
||||||
%{
|
guild_id: guild_id,
|
||||||
guild_id: guild_id,
|
member: %{user: %{id: invoker_id}},
|
||||||
member: %{user: %{id: invoker_id}},
|
data: %{name: "play", options: [%{name: "url", value: url}]}
|
||||||
data: %{name: "play", options: opts}
|
}) do
|
||||||
} = _interaction
|
|
||||||
) do
|
|
||||||
[%{name: "url", value: url}] = opts
|
|
||||||
|
|
||||||
case Sergei.VoiceStateCache.get_state(invoker_id) do
|
case Sergei.VoiceStateCache.get_state(invoker_id) do
|
||||||
%{guild_id: id} = _res when guild_id != id ->
|
%{guild_id: id} = _res when guild_id != id ->
|
||||||
{:error, "You're not connected to a voice channel in this server."}
|
{:error, "You're not connected to a voice channel in this server."}
|
||||||
|
@ -110,17 +105,17 @@ defmodule Sergei.Consumer do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# /stop
|
# /play
|
||||||
def do_command(%{guild_id: guild_id, data: %{name: "stop"}}) do
|
def do_command(%{guild_id: guild_id, data: %{name: "play"}}) do
|
||||||
case Sergei.Player.stop(guild_id) do
|
case Sergei.Player.resume(guild_id) do
|
||||||
:ok ->
|
:ok ->
|
||||||
{:ok, "Bye!"}
|
{:ok, "Resuming playback..."}
|
||||||
|
|
||||||
:not_playing ->
|
:not_playing ->
|
||||||
{:ok, "I'm not playing anything right now."}
|
{:ok, "I'm not playing anything right now."}
|
||||||
|
|
||||||
{:error, err} ->
|
{:error, err} ->
|
||||||
Logger.error("Failed to stop media: #{err}")
|
Logger.error("Failed to resume media: #{err}")
|
||||||
{:error, "This is embarrasing..."}
|
{:error, "This is embarrasing..."}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -140,17 +135,17 @@ defmodule Sergei.Consumer do
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
# /resume
|
# /stop
|
||||||
def do_command(%{guild_id: guild_id, data: %{name: "resume"}}) do
|
def do_command(%{guild_id: guild_id, data: %{name: "stop"}}) do
|
||||||
case Sergei.Player.resume(guild_id) do
|
case Sergei.Player.stop(guild_id) do
|
||||||
:ok ->
|
:ok ->
|
||||||
{:ok, "Resuming..."}
|
{:ok, "Bye!"}
|
||||||
|
|
||||||
:not_playing ->
|
:not_playing ->
|
||||||
{:ok, "I'm not playing anything right now."}
|
{:ok, "I'm not playing anything right now."}
|
||||||
|
|
||||||
{:error, err} ->
|
{:error, err} ->
|
||||||
Logger.error("Failed to resume media: #{err}")
|
Logger.error("Failed to stop media: #{err}")
|
||||||
{:error, "This is embarrasing..."}
|
{:error, "This is embarrasing..."}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -20,11 +20,6 @@ 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 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()}
|
@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})
|
||||||
|
@ -35,6 +30,11 @@ defmodule Sergei.Player do
|
||||||
GenServer.call(__MODULE__, {:resume, guild_id})
|
GenServer.call(__MODULE__, {:resume, guild_id})
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@spec stop(integer()) :: :ok | :not_playing | {:error, String.t()}
|
||||||
|
def stop(guild_id) do
|
||||||
|
GenServer.call(__MODULE__, {:stop, guild_id})
|
||||||
|
end
|
||||||
|
|
||||||
# Server
|
# Server
|
||||||
@impl true
|
@impl true
|
||||||
def handle_info(:tick, state) do
|
def handle_info(:tick, state) do
|
||||||
|
@ -67,14 +67,6 @@ defmodule Sergei.Player do
|
||||||
{:reply, :not_playing, state}
|
{:reply, :not_playing, state}
|
||||||
end
|
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
|
@impl true
|
||||||
def handle_call({:pause, guild_id}, _from, state) do
|
def handle_call({:pause, guild_id}, _from, state) do
|
||||||
%{url: url} = Map.fetch!(state, guild_id)
|
%{url: url} = Map.fetch!(state, guild_id)
|
||||||
|
@ -103,6 +95,14 @@ defmodule Sergei.Player do
|
||||||
{:reply, :ok, state}
|
{:reply, :ok, state}
|
||||||
end
|
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
|
||||||
|
|
||||||
def play_music(guild_id, channel_id, url) do
|
def play_music(guild_id, channel_id, url) do
|
||||||
cond do
|
cond do
|
||||||
Voice.get_channel_id(guild_id) != channel_id ->
|
Voice.get_channel_id(guild_id) != channel_id ->
|
||||||
|
|
Loading…
Reference in a new issue