diff options
Diffstat (limited to 'lib/consumer.ex')
| -rw-r--r-- | lib/consumer.ex | 50 |
1 files changed, 40 insertions, 10 deletions
diff --git a/lib/consumer.ex b/lib/consumer.ex index a594846..406ddeb 100644 --- a/lib/consumer.ex +++ b/lib/consumer.ex @@ -1,26 +1,56 @@ require Logger -alias Nostrum.Struct.Message -alias Phoenix.PubSub - defmodule Thulani.DiscordConsumer do use Nostrum.Consumer @spec handle_event(Nostrum.Consumer.message_create()) :: nil + def handle_event({ :MESSAGE_CREATE, - message = %Message{}, + msg, _ws_state }) do - case Thulani.Message.validate(message) do - {:ok, command} -> - Logger.debug(%{command: command}) - PubSub.broadcast!(:thulani_pubsub, "cmd", {:cmd, command, message}) + unless msg.author.bot do + case Nosedrum.TextCommand.Invoker.Split.handle_message(msg) do + {:error, {:unknown_subcommand, name, :known, known}} -> + Logger.error("unknown subcommand", name: name, known: known) + + {:error, other} -> + Logger.error("unknown error", data: other) - :ignore -> - nil + {:ok, data} -> + Logger.info("handled command ok", data: data) + + other -> + Logger.warning("unhandled textcommand case", result: other) + end end end + def handle_event({ + :READY, + data, + _ws_state + }) do + Logger.info("bot ready", data: data) + + commands() + |> Stream.flat_map(fn command_mod -> + apply(command_mod, :aliases, []) + |> Stream.zip(Stream.cycle([command_mod])) + end) + |> Stream.each(fn {name, mod} -> + :ok = Nosedrum.TextCommand.Storage.ETS.add_command([name], mod) + Logger.info("registered command", command_name: name, mod: mod) + end) + |> Stream.run() + end + def handle_event(_event), do: :noop + + defp commands, + do: [ + Thulani.Command.Meme, + Thulani.Command.AddMeme + ] end |
