Skip to main content

lib/gelotv_bot/command.ex

defmodule GelotvBot.Command do
  @moduledoc """
  Reusable command payload that can be sent to one or many live chats.

  Commands are intentionally simple: a name for routing/observability, a body
  for the platform chat message, and metadata for internal systems.
  """

  alias GelotvBot.Message

  @enforce_keys [:name, :body]
  defstruct name: nil, body: nil, metadata: %{}

  @type t :: %__MODULE__{name: atom() | String.t(), body: String.t(), metadata: map()}

  @spec new(atom() | String.t(), String.t(), map()) :: t()
  def new(name, body, metadata \\ %{}) when is_binary(body) and is_map(metadata) do
    %__MODULE__{name: name, body: body, metadata: Map.put_new(metadata, :command, name)}
  end

  @spec to_message(t()) :: Message.t()
  def to_message(%__MODULE__{body: body, metadata: metadata}), do: Message.new(body, metadata)
end