59 lines
1.6 KiB
Elixir
59 lines
1.6 KiB
Elixir
defmodule Mithridate.Consumer do
|
|
@behaviour Nostrum.Consumer
|
|
|
|
require Logger
|
|
alias Nostrum.Struct.Interaction
|
|
alias Nostrum.Constants.InteractionType
|
|
alias Mithridate.CommandStore
|
|
|
|
@application_command InteractionType.application_command()
|
|
@message_component InteractionType.message_component()
|
|
@autocomplete InteractionType.application_command_autocomplete()
|
|
|
|
def handle_event({:READY, _ev, _ws}) do
|
|
CommandStore.ChatInput.serialize()
|
|
|> Kernel.++(CommandStore.User.serialize())
|
|
|> register_commands()
|
|
end
|
|
|
|
def handle_event({:INTERACTION_CREATE, interaction, _ws}), do: handle_interaction(interaction)
|
|
|
|
def handle_event(_), do: :ok
|
|
|
|
def handle_interaction(%Interaction{type: type} = interaction),
|
|
do: handle_interaction(type, interaction)
|
|
|
|
defp handle_interaction(@application_command, interaction) do
|
|
dbg(interaction)
|
|
:ok
|
|
end
|
|
|
|
defp handle_interaction(@message_component, interaction) do
|
|
dbg(interaction)
|
|
:ok
|
|
end
|
|
|
|
defp handle_interaction(@autocomplete, interaction) do
|
|
dbg(interaction)
|
|
:ok
|
|
end
|
|
|
|
defp handle_interaction(_type, _interaction), do: :ok
|
|
|
|
defp register_commands(commands) do
|
|
env = Mix.env()
|
|
{:ok, _commands} = register_commands(commands, env)
|
|
|
|
Logger.info("registered commands", env: env)
|
|
end
|
|
|
|
defp register_commands(commands, :dev) do
|
|
Application.fetch_env!(:mithridate, :discord)
|
|
|> Keyword.fetch!(:test_guild)
|
|
|> Nostrum.Api.ApplicationCommand.bulk_overwrite_guild_commands(commands)
|
|
end
|
|
|
|
defp register_commands(commands, :prod) do
|
|
Nostrum.Api.ApplicationCommand.bulk_overwrite_global_commands(commands)
|
|
end
|
|
end
|