aboutsummaryrefslogtreecommitdiff
path: root/lib/command/command.ex
blob: 07be96c86b2949953efa39cdf4291d05fae4d835 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
alias Phoenix.PubSub
alias Nostrum.Api

defmodule Thulani.Command do
  use GenServer
  use Thulani.Command.Util
  require Logger

  def start_link(arg) do
    GenServer.start_link(__MODULE__, arg)
  end

  @impl true
  def init(_) do
    PubSub.subscribe(:thulani_pubsub, "cmd")
    {:ok, %{}}
  end

  @impl true
  def handle_info(Util.command("meme", "", _msg), state) do
    {:noreply, state}
  end

  @impl true
  def handle_info(Util.command("meme", rest, _msg), state) do
    case rest do
      "" -> Thulani.Meme
    end

    {:noreply, state}
  end

  def handle_info(Util.command("http", _rest, _msg), _state) do
  end

  def handle_info(Util.command("roll", rest, msg), state) do
    Logger.info("hi")

    case rest do
      " " <> expr ->
        case Thulani.Calc.eval(expr) do
          {:ok, val} -> Api.create_message!(msg.channel_id, "#{val}")
          {:error, err} -> Logger.error("BAD: #{err}")
        end

      _ ->
        Logger.error("nothing else in the message")
    end

    {:noreply, state}
  end

  def handle_info(_msg, state) do
    Logger.debug("unhandled message")
    {:noreply, state}
  end
end