holder/lib/mithridate/option.ex

38 lines
1.2 KiB
Elixir

defmodule Mithridate.Option do
@enforce_keys [:name, :description, :type, :kind]
defstruct name: nil,
description: nil,
type: nil,
required: false,
choices: [],
options: [],
autocomplete: false,
kind: nil
@type t() :: %__MODULE__{}
@type choice() :: {atom() | String.t() | integer(), String.t()}
@type options() :: [
required: boolean(),
choices: [choice()],
options: [t()],
autocomplete: boolean()
]
@callback parse(String.t(), Nostrum.Struct.Interaction.t()) :: any()
@callback new(atom(), String.t(), options()) :: t()
@callback to_discord(t()) :: Nostrum.Struct.ApplicationCommand.command_option()
@spec serialize_choices([choice()]) :: [Nostrum.Struct.ApplicationCommand.command_choice()]
def serialize_choices(choices) do
choices
|> Enum.map(fn
{value, name} when is_atom(value) -> %{name: name, value: Atom.to_string(value)}
{value, name} -> %{name: name, value: value}
end)
end
@spec to_discord(t()) :: Nostrum.Struct.ApplicationCommand.command_option()
def to_discord(%__MODULE__{} = option) do
option.kind.to_discord(option)
end
end