holder/lib/mithridate/command_store.ex

45 lines
1.1 KiB
Elixir

defmodule Mithridate.CommandStore do
alias Mithridate.Command
@spec fetch_commands(module() | nil) :: [Command.t()]
def fetch_commands(nil), do: []
def fetch_commands(prefix) do
prefix = to_string(prefix)
:code.all_available()
|> Enum.map(fn x -> elem(x, 0) |> to_string end)
|> Enum.filter(&String.starts_with?(&1, prefix))
|> Enum.map(fn mod_name ->
mod = String.to_atom(mod_name)
Code.ensure_loaded!(mod)
name =
if function_exported?(mod, :name, 0) do
mod.name()
else
mod_name
|> String.replace_prefix("#{prefix}.", "")
|> String.downcase()
end
description =
if function_exported?(mod, :description, 0),
do: mod.description(),
else: ""
nsfw =
if function_exported?(mod, :nsfw, 0),
do: mod.nsfw(),
else: false
options =
if function_exported?(mod, :options, 0),
do: mod.options(),
else: []
%Command{name: name, description: description, nsfw: nsfw, options: options, module: mod}
end)
end
end