diff --git a/config/config.exs b/config/config.exs index 191e405..8c61a26 100644 --- a/config/config.exs +++ b/config/config.exs @@ -3,6 +3,9 @@ import Config config :nostrum, youtubedl: "/usr/bin/yt-dlp" +config :sergei, + env: config_env() + # Import environment specific config. This must remain at the bottom # of this file so it overrides the configuration defined above. import_config "#{config_env()}.exs" diff --git a/lib/sergei/consumer.ex b/lib/sergei/consumer.ex index 9d39ea3..3546ca0 100644 --- a/lib/sergei/consumer.ex +++ b/lib/sergei/consumer.ex @@ -25,10 +25,11 @@ defmodule Sergei.Consumer do Consumer.start_link(__MODULE__) end - # Bulk overwrite commands per guild. While this is efficient enough for now, moving - # to global commands in the future is probably a good idea. - @spec register_commands!(integer) :: :ok - def register_commands!(guild_id) do + # Initialization of the Discord Client + def handle_event({:READY, %{guilds: guilds} = _event, _ws_state}) do + # Playing some tunes + Api.update_status(:online, "some tunes", 0) + commands = Enum.map(@slash_commands, fn {name, description, options} -> %{ @@ -38,21 +39,30 @@ defmodule Sergei.Consumer do } end) - case Api.bulk_overwrite_guild_application_commands(guild_id, commands) do - {:ok, _res} -> :ok - {:error, err} -> raise err + case Application.get_env(:sergei, :env) do + :prod -> + case Api.bulk_overwrite_global_application_commands(commands) do + {:ok, _res} -> :ok + {:error, err} -> raise err + end + + # Overwrite commands by guild in dev for a faster dev cycle + :dev -> + guilds + |> Enum.map(fn guild -> guild.id end) + |> Enum.each(fn guild_id -> + case Api.bulk_overwrite_guild_application_commands(guild_id, commands) do + {:ok, _res} -> :ok + {:error, err} -> raise err + end + end) + + _ -> + Logger.error("invalid environment: expected dev or prod") + System.stop(1) end end - # Initialization of the Discord Client - def handle_event({:READY, %{guilds: guilds} = _event, _ws_state}) do - Api.update_status(:online, "some tunes", 0) - - guilds - |> Enum.map(fn guild -> guild.id end) - |> Enum.each(®ister_commands!/1) - end - def handle_event({:VOICE_STATE_UPDATE, state, _ws_state}) do Sergei.VoiceStateCache.update_state(state) end