diff --git a/lib/contrib/wordbot/wordbot.ex b/lib/contrib/wordbot/wordbot.ex index de88229..48f547f 100644 --- a/lib/contrib/wordbot/wordbot.ex +++ b/lib/contrib/wordbot/wordbot.ex @@ -127,7 +127,6 @@ defmodule Omnibot.Contrib.Wordbot do |> Enum.reverse() |> Enum.with_index() |> Map.new() - |> IO.inspect() Enum.each(scores, &Irc.send_to(irc, channel, "#{rankings[&1.score] + 1}. #{Util.denotify_nick(&1.user)}. #{&1.score}")) diff --git a/lib/plugin/plugin.ex b/lib/plugin/plugin.ex index e9b3d42..11492d6 100644 --- a/lib/plugin/plugin.ex +++ b/lib/plugin/plugin.ex @@ -34,15 +34,15 @@ defmodule Omnibot.Plugin do GenServer.start_link(__MODULE__, cfg, opts) end - def cfg() do + def cfg() when unquote(opts[:include_base]) do Omnibot.Plugin.CfgState.cfg(__MODULE__.CfgState) end - def state() do + def state() when unquote(opts[:include_base]) do Omnibot.Plugin.CfgState.state(__MODULE__.CfgState) end - def update_state(fun) do + def update_state(fun) when unquote(opts[:include_base]) do Omnibot.Plugin.CfgState.update_state(__MODULE__.CfgState, fun) end @@ -54,7 +54,13 @@ defmodule Omnibot.Plugin do ## Server callbacks @impl GenServer - def init(_cfg) do + def init(cfg) do + # call on_init(cfg) for the plugin + state = on_init(cfg) + # If we know this plugin uses CfgState, then use that + if unquote(opts[:include_base]) do + Omnibot.Plugin.CfgState.update_state(__MODULE__.CfgState, fn _ -> state end) + end {:ok, nil} end @@ -64,19 +70,22 @@ defmodule Omnibot.Plugin do {:noreply, state} end - defp base_children(cfg, state) when unquote(opts[:include_base]) do - [ - {Omnibot.Plugin.CfgState, cfg: cfg, state: state, name: __MODULE__.CfgState}, - {__MODULE__, name: __MODULE__}, - ] + defp base_children_before(cfg) when unquote(opts[:include_base]) do + [{Omnibot.Plugin.CfgState, cfg: cfg, name: __MODULE__.CfgState}] end - defp base_children(_cfg, _state), do: [] + defp base_children_after(cfg) when unquote(opts[:include_base]) do + [{__MODULE__, cfg: cfg, name: __MODULE__}] + end + + defp base_children_before(_cfg), do: [] + + defp base_children_after(_cfg), do: [] @impl Omnibot.Plugin def children(cfg), do: [] - def plugin_children(cfg, state), do: base_children(cfg, state) ++ children(cfg) + def plugin_children(cfg), do: base_children_before(cfg) ++ children(cfg) ++ base_children_after(cfg) @behaviour Omnibot.Plugin defoverridable Omnibot.Plugin diff --git a/lib/plugin/supervisor.ex b/lib/plugin/supervisor.ex index 6fd3ef1..bc4c954 100644 --- a/lib/plugin/supervisor.ex +++ b/lib/plugin/supervisor.ex @@ -21,8 +21,7 @@ defmodule Omnibot.Plugin.Supervisor do @impl true def init({plugin, cfg}) when is_atom(plugin) do - state = plugin.on_init(cfg) - children = IO.inspect(plugin.plugin_children(cfg, state)) + children = plugin.plugin_children(cfg) Supervisor.init(children, strategy: :one_for_one) end end