WIP: Move Config.all_channels/0 and Config.channel_modules/1 logic into State; add State.add_loaded_module/2

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>
This commit is contained in:
2020-06-13 17:13:05 -04:00
parent 590b3ddbec
commit 304a8d4164
12 changed files with 172 additions and 106 deletions

View File

@@ -3,6 +3,7 @@ defmodule Omnibot.ModuleSupervisor do
use Supervisor
require Logger
alias Omnibot.State
def start_link(opts \\ []) do
Supervisor.start_link(__MODULE__, opts[:cfg], opts)
@@ -12,6 +13,10 @@ defmodule Omnibot.ModuleSupervisor do
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
@@ -21,6 +26,9 @@ defmodule Omnibot.ModuleSupervisor do
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