aboutsummaryrefslogtreecommitdiff
path: root/lib/consumer.ex
blob: 614c2615a630973cab019231ceacd99142f7ca24 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
require Logger

alias Nostrum.Struct.Message
alias Phoenix.PubSub

defmodule Thulani.DiscordConsumer do
  use Nostrum.Consumer

  def start_link do
    Nostrum.Consumer.start_link(__MODULE__)
  end

  @spec handle_event(Nostrum.Consumer.message_create()) :: nil
  def handle_event({
        :MESSAGE_CREATE,
        message = %Message{},
        _ws_state
      }) do
    case Thulani.Message.validate(message) do
      {:ok, command} ->
        Logger.debug(%{command: command})
        PubSub.broadcast!(:thulani_pubsub, "cmd", {:cmd, command, message})

      :ignore ->
        nil
    end
  end

  def handle_event(_event), do: :noop
end