The problem: When we're going through the list of modules to send messages to based on the channels they're a part of, it was being done so through the config. Since the config doesn't (and shouldn't) list all of the core modules that get included, any core modules that were loaded and running under the ModuleSupervisor would not get included in the router's attempt to send messages to a module. Now, the Config.all_channels and Config.channel_modules functions live in State, and State has a new "add_loaded_module" function where loaded modules are registered. The aforementioned moved functions will use this as the "source of truth" when deciding where to send messages for modules to handle. Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
61 lines
1.7 KiB
Elixir
61 lines
1.7 KiB
Elixir
defmodule Omnibot.ModuleSupervisor do
|
|
@moduledoc false
|
|
|
|
use Supervisor
|
|
require Logger
|
|
alias Omnibot.State
|
|
|
|
def start_link(opts \\ []) do
|
|
Supervisor.start_link(__MODULE__, opts[:cfg], opts)
|
|
end
|
|
|
|
@impl true
|
|
def init(cfg) do
|
|
compile_files(cfg.module_paths || [])
|
|
|
|
# These are modules that need to be loaded for core functionality of the bot
|
|
#{Omnibot.Core.Welcome, cfg: [channels: :all]},
|
|
#{Omnibot.Core.Join, cfg: [channels: :all]},
|
|
|
|
# Map the modules in the configuration to the children
|
|
children =
|
|
for mod <- cfg.modules do
|
|
case mod do
|
|
{name, cfg} -> {name, cfg: cfg, name: name}
|
|
name -> {name, cfg: [], name: name}
|
|
end
|
|
end
|
|
|
|
# Add each child to the "loaded modules" list in the State
|
|
Enum.each(children, fn module -> State.add_loaded_module(module) end)
|
|
|
|
Supervisor.init(children, strategy: :one_for_one)
|
|
end
|
|
|
|
defp compile_files([]), do: nil
|
|
|
|
defp compile_files([{path, opts} | module_paths]) do
|
|
case {File.exists?(path), File.dir?(path)} do
|
|
{_, true} -> compile_dir(path, opts[:recurse] || false)
|
|
{true, false} -> Code.require_file(path)
|
|
{_, _} -> Logger.error("module path '#{path}' does not exist, it will not be loaded")
|
|
end
|
|
|
|
compile_files(module_paths)
|
|
end
|
|
|
|
defp compile_files([path | module_paths]) do
|
|
compile_files([{path, []} | module_paths])
|
|
end
|
|
|
|
defp compile_dir(path, recurse) do
|
|
files =
|
|
File.ls!(path)
|
|
|> Enum.map(fn file -> {Path.join(path, file), [recurse: recurse]} end)
|
|
|> Enum.filter(fn {file, [recurse: recurse]} ->
|
|
(!File.dir?(file) || recurse) && File.exists?(file)
|
|
end)
|
|
compile_files(files)
|
|
end
|
|
end
|