Move more stuff from other places into Omnibot.Core

* Present rooms are tracked by Omnibot.Core now, instead of
  Omnibot.State
* Channel synchronization is done through Omnibot.Core instead of
  Omnibot.Irc
* Add Omnibot.Module.on_init/1 callback, which returns a "state" value
  for the module to keep track of. By default, the state is nil.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-06-13 18:45:02 -04:00
parent 91cdd9cae8
commit 4000528d81
4 changed files with 59 additions and 70 deletions

View File

@@ -15,7 +15,10 @@ defmodule Omnibot.Module do
def on_part(_irc, _channel, _nick), do: nil
@impl true
def on_kick(_irc, _channel, _nick), do: nil
def on_kick(_irc, _channel, _nick, _target), do: nil
@impl true
def on_init(_cfg), do: nil
end
end
end
@@ -30,11 +33,24 @@ defmodule Omnibot.Module do
@behaviour Module
def start_link(opts) do
Agent.start_link(fn -> opts[:cfg] end, opts ++ [name: __MODULE__])
cfg = opts[:cfg]
Agent.start_link(fn -> {cfg, on_init(cfg)} end, opts ++ [name: __MODULE__])
end
def cfg do
Agent.get(__MODULE__, & &1)
Agent.get(__MODULE__, fn {cfg, _} -> cfg end)
end
def state do
Agent.get(__MODULE__, fn {_, state} -> state end)
end
def update_state(update, timeout \\ 5000) do
Agent.update(
__MODULE__,
fn {cfg, state} -> {cfg, apply(update, [state])} end,
timeout
)
end
@impl Module
@@ -66,8 +82,8 @@ defmodule Omnibot.Module do
on_part(irc, channel, nick)
"KICK" ->
[channel | _] = msg.params
on_kick(irc, channel, nick)
[channel, target | _] = msg.params
on_kick(irc, channel, nick, target)
_ ->
nil
@@ -91,7 +107,8 @@ defmodule Omnibot.Module do
) :: any
@callback on_join(irc :: pid(), channel :: String.t(), nick :: String.t()) :: any
@callback on_part(irc :: pid(), channel :: String.t(), nick :: String.t()) :: any
@callback on_kick(irc :: pid(), channel :: String.t(), nick :: String.t()) :: any
@callback on_kick(irc :: pid(), channel :: String.t(), nick :: String.t(), target :: String.t()) :: any
@callback on_init(cfg :: any) :: any
defmacro command(cmd, opts) do
quote generated: true do