Add commands() for modules, which scoops up all commands defined.

Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
2020-06-13 20:59:09 -04:00
parent a627842717
commit 8c8f3c928d

View File

@@ -19,6 +19,8 @@ defmodule Omnibot.Module do
@impl true @impl true
def on_init(_cfg), do: nil def on_init(_cfg), do: nil
def commands(), do: MapSet.to_list(@commands)
end end
end end
end end
@@ -69,7 +71,9 @@ defmodule Omnibot.Module do
line = Enum.join(params, " ") line = Enum.join(params, " ")
case String.split(line, " ") do case String.split(line, " ") do
[cmd | params] -> on_channel_msg(irc, channel, nick, cmd, params) [cmd | params] -> if Enum.member?(commands(), cmd),
do: on_channel_msg(irc, channel, nick, cmd, params),
else: on_channel_msg(irc, channel, nick, line)
_ -> on_channel_msg(irc, channel, nick, line) _ -> on_channel_msg(irc, channel, nick, line)
end end
@@ -92,6 +96,7 @@ defmodule Omnibot.Module do
defoverridable Module defoverridable Module
@commands MapSet.new()
@before_compile Omnibot.Module.Hooks @before_compile Omnibot.Module.Hooks
end end
end end
@@ -112,6 +117,7 @@ defmodule Omnibot.Module do
defmacro command(cmd, opts) do defmacro command(cmd, opts) do
quote generated: true do quote generated: true do
@commands MapSet.put(@commands, unquote(cmd))
@impl Omnibot.Module @impl Omnibot.Module
def on_channel_msg(var!(irc), var!(channel), var!(nick), unquote(cmd), var!(params)) do def on_channel_msg(var!(irc), var!(channel), var!(nick), unquote(cmd), var!(params)) do
unquote(opts[:do]) unquote(opts[:do])
@@ -132,6 +138,7 @@ defmodule Omnibot.Module do
) )
quote generated: true do quote generated: true do
@commands MapSet.put(@commands, unquote(cmd))
@impl Omnibot.Module @impl Omnibot.Module
def on_channel_msg(var!(irc), var!(channel), var!(nick), unquote(cmd), unquote(params)) do def on_channel_msg(var!(irc), var!(channel), var!(nick), unquote(cmd), unquote(params)) do
unquote(opts[:do]) unquote(opts[:do])