Add Omnibot.Module

Modules can easily be defined with `use Omnibot.Module`.
Omnibot.Contrib.Fortune has been updated to use this.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-06-12 18:24:11 -04:00
parent 6340936895
commit e2e7ae22b8
4 changed files with 95 additions and 30 deletions

View File

@@ -3,6 +3,19 @@ defmodule Omnibot.Router do
alias Omnibot.{Config, Irc, Irc.Msg, State}
def route(irc, msg) do
# TODO - consider removing this check and specific handling into the `use Omnibot.Module` block
# PROS:
# - Don't have to determine message command twice (first here, second in Omnibot.Module)
# - Allows for much more powerful modules. JOIN, PART, KICK, etc handlers would be modules themselves
# - This is an extremely big win IMO
#
# CONS:
# - All routed functionality needs to be in a module
# - This may get a little old
# - A failed command could cause important messages to be missed (?)
# - Do messages in a named PID's mailbox persist after that PID goes away? My guess is "no"
# - To get around this, there could be a mailbox for "special" commands, probably using ETS
#
case String.upcase(msg.command) do
"PRIVMSG" -> handle(irc, :privmsg, msg)
"JOIN" -> handle(irc, :join, msg)
@@ -15,15 +28,12 @@ defmodule Omnibot.Router do
end
def handle(_irc, :privmsg, msg) do
# TODO : get channel, pass along to modules
[channel | params] = msg.params
line = Enum.join(params, " ")
nick = msg.prefix.nick
[channel | _params] = msg.params
# Find modules that want this message
State.cfg()
|> Config.channel_modules(channel)
|> Enum.each(fn {module, _} -> module.privmsg(module, channel, nick, line) end)
|> Enum.each(fn {module, _} -> module.msg(module, msg) end)
end
def handle(_irc, :join, %Msg {prefix: %Msg.Prefix{nick: nick}, params: [channel | _]}) do