feat: switch to using global commands in prod

This commit is contained in:
Roman 2023-06-06 17:01:17 -04:00
parent bad3ea3679
commit c4834abfe3
2 changed files with 29 additions and 16 deletions

View file

@ -3,6 +3,9 @@ import Config
config :nostrum, config :nostrum,
youtubedl: "/usr/bin/yt-dlp" youtubedl: "/usr/bin/yt-dlp"
config :sergei,
env: config_env()
# Import environment specific config. This must remain at the bottom # Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above. # of this file so it overrides the configuration defined above.
import_config "#{config_env()}.exs" import_config "#{config_env()}.exs"

View file

@ -25,10 +25,11 @@ defmodule Sergei.Consumer do
Consumer.start_link(__MODULE__) Consumer.start_link(__MODULE__)
end end
# Bulk overwrite commands per guild. While this is efficient enough for now, moving # Initialization of the Discord Client
# to global commands in the future is probably a good idea. def handle_event({:READY, %{guilds: guilds} = _event, _ws_state}) do
@spec register_commands!(integer) :: :ok # Playing some tunes
def register_commands!(guild_id) do Api.update_status(:online, "some tunes", 0)
commands = commands =
Enum.map(@slash_commands, fn {name, description, options} -> Enum.map(@slash_commands, fn {name, description, options} ->
%{ %{
@ -38,21 +39,30 @@ defmodule Sergei.Consumer do
} }
end) end)
case Api.bulk_overwrite_guild_application_commands(guild_id, commands) do case Application.get_env(:sergei, :env) do
{:ok, _res} -> :ok :prod ->
{:error, err} -> raise err 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
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(&register_commands!/1)
end
def handle_event({:VOICE_STATE_UPDATE, state, _ws_state}) do def handle_event({:VOICE_STATE_UPDATE, state, _ws_state}) do
Sergei.VoiceStateCache.update_state(state) Sergei.VoiceStateCache.update_state(state)
end end