holder/lib/mithridate/command_store/user.ex

32 lines
800 B
Elixir

defmodule Mithridate.CommandStore.User do
use Agent
alias Mithridate.Command
alias Mithridate.CommandStore
@command_type Nostrum.Constants.ApplicationCommandType.user()
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} ->
Map.take(cmd, [:name, :description, :type, :nsfw])
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