Skip to content

Commit

Permalink
Plugin.ls() lists actual plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
ProducerMatt committed Mar 12, 2024
1 parent 1b54ae0 commit ca557fa
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ erl_crash.dump

### Elixir Patch ###
/doc
/.elixir-tools

# Nix resources
/.nix-mix
Expand Down
21 changes: 19 additions & 2 deletions lib/plugin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,26 @@ defmodule Plugin do
end
end

@doc "returns loaded modules using the Plugin behavior."
@spec! ls() :: MapSet.t(module())
def ls() do
S.find_submodules(__MODULE__)
S.find_submodules(Elixir)
|> Enum.reduce(MapSet.new(), fn
mod, acc ->
if not Kernel.function_exported?(mod, :__info__, 1) do
acc
else
b =
apply(mod, :__info__, [:attributes])
|> Keyword.get(:behaviour, [])

if Plugin in b do
MapSet.put(acc, mod)
else
acc
end
end
end)
end

def default_plugin_mfa(plug, [cfg, msg]) do
Expand Down Expand Up @@ -218,7 +236,6 @@ defmodule Plugin do
)
|> S.Interact.record_interaction!()

# TODO: logging interactions
chosen_response

%Response{callback: {mod, fun, args}} ->
Expand Down
6 changes: 4 additions & 2 deletions lib/service/discord.ex
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ defmodule Service.Discord do

[
"Message from ",
msg.author_id |> Nostrum.Struct.User.full_name() |> inspect(),
msg.author_id |> Nostrum.Api.get_user!() |> Nostrum.Struct.User.full_name() |> inspect(),
" lead to ",
error_type,
" in plugin ",
Expand Down Expand Up @@ -145,7 +145,9 @@ defmodule Service.Discord do
"Erlang-level error ",
inspect(level),
"\n",
message |> S.pp() |> Service.Discord.txt_format(:source_block)
message
|> TxtBlock.to_str_list(Service.Discord)
|> Service.Discord.txt_format(:source_block)
]

_ = send_msg(channel_id, log)
Expand Down
16 changes: 11 additions & 5 deletions lib/stampede.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,17 @@ defmodule Stampede do
@spec! find_submodules(module()) :: MapSet.t(module())
def find_submodules(module_name) do
:code.all_available()
|> Enum.map(&(elem(&1, 0) |> to_string))
|> Enum.filter(&String.starts_with?(&1, to_string(module_name) <> "."))
|> Enum.sort()
|> Enum.map(&String.to_atom/1)
|> MapSet.new()
|> Enum.reduce(MapSet.new(), fn
{name, _location, _loaded}, acc ->
name
|> List.to_string()
|> String.starts_with?(to_string(module_name) <> ".")
|> if do
MapSet.put(acc, List.to_atom(name))
else
acc
end
end)
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion lib/stampede/interaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ defmodule Stampede.Interaction do
plugin: _ :: any(),
msg: _ :: Msg,
response: _ :: Response,
traceback: [] :: iodata() | String.t(),
traceback: [] :: TxtBlock.t(),
channel_lock: false :: S.channel_lock_action()
)

Expand Down

0 comments on commit ca557fa

Please sign in to comment.