2020-07-08 11:29:13 -07:00
|
|
|
defmodule Omnibot.Contrib.Markov do
|
2020-07-09 14:51:33 -07:00
|
|
|
use Omnibot.Plugin.Base
|
|
|
|
|
alias Omnibot.Contrib.Markov
|
|
|
|
|
use Supervisor
|
2020-07-08 11:29:13 -07:00
|
|
|
|
2020-07-09 14:51:33 -07:00
|
|
|
@default_config path: "markov", order: 2, save_every: 5 * 60
|
2020-07-08 11:29:13 -07:00
|
|
|
|
2020-07-09 14:51:33 -07:00
|
|
|
def start_link(opts) do
|
|
|
|
|
Supervisor.start_link(__MODULE__, opts[:cfg], opts)
|
2020-07-08 11:29:13 -07:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
@impl true
|
2020-07-09 14:51:33 -07:00
|
|
|
def init(cfg) do
|
|
|
|
|
children = [
|
|
|
|
|
{Markov.Bot, cfg: cfg, name: Omnibot.Contrib.Markov.Bot},
|
|
|
|
|
{Task, fn -> save_loop(cfg) end}
|
|
|
|
|
]
|
|
|
|
|
Supervisor.init(children, strategy: :one_for_all)
|
2020-07-08 17:25:26 -07:00
|
|
|
end
|
|
|
|
|
|
2020-07-09 14:51:33 -07:00
|
|
|
defp save_loop(cfg) do
|
|
|
|
|
save_every = cfg[:save_every]
|
|
|
|
|
Process.sleep(save_every * 1000)
|
|
|
|
|
Markov.Bot.save_chains()
|
2020-07-08 17:25:26 -07:00
|
|
|
end
|
|
|
|
|
|
2020-07-09 14:51:33 -07:00
|
|
|
@impl true
|
|
|
|
|
def on_msg(irc, msg), do: Markov.Bot.on_msg(irc, msg)
|
2020-07-08 17:25:26 -07:00
|
|
|
|
2020-07-09 14:51:33 -07:00
|
|
|
@impl true
|
|
|
|
|
def on_channel_msg(irc, channel, nick, msg), do: Markov.Bot.on_channel_msg(irc, channel, nick, msg)
|
2020-07-08 11:29:13 -07:00
|
|
|
end
|