holder/lib/mithridate/command_store/chat_input.ex

37 lines
994 B
Elixir

defmodule Mithridate.CommandStore.ChatInput do
use Agent
alias Mithridate.Option
alias Mithridate.Command
alias Mithridate.CommandStore
@command_type Nostrum.Constants.ApplicationCommandType.chat_input()
def start_link(prefix) do
commands =
CommandStore.fetch_commands(prefix)
|> Enum.map(fn %Command{} = command -> %Command{command | type: @command_type} end)
|> Map.new(&{&1.name, &1})
Agent.start_link(fn -> commands end, name: __MODULE__)
end
def serialize() do
Agent.get(__MODULE__, & &1)
|> Map.to_list()
|> Enum.map(fn {_name, cmd} -> serialize(cmd) end)
end
defp serialize(%Command{} = cmd) do
cmd
|> Map.take([:name, :description, :options, :type, :nsfw])
|> Map.replace_lazy(:options, fn opts -> Enum.map(opts, &Option.to_discord/1) end)
end
def get(name) do
Agent.get(__MODULE__, &Map.get(&1, name))
end
def set(name, value) do
Agent.update(__MODULE__, &Map.put(&1, name, value))
end
end