|
| 1 | +defmodule Crux.Gateway do |
| 2 | + @moduledoc """ |
| 3 | + Main entry point to start the Gateway connection. |
| 4 | +
|
| 5 | + Required for this to run are: |
| 6 | + - `:token` to identify with, you can get your bot's from [here](https://discordapp.com/developers/applications/me). |
| 7 | + > You want to keep that token secret at all times. |
| 8 | +
|
| 9 | + - `:url` to connect to. Probably something like `wss://gateway.discord.gg`. |
| 10 | + (Do not append query strings) |
| 11 | + > You usually want to GET the url via `/gateway/bot` along the recommended shard count. |
| 12 | +
|
| 13 | + - `:shard_count` you plan to run altogether. |
| 14 | + > Can and probably should be retrieved via `/gateway/bot`. |
| 15 | +
|
| 16 | + - Optionally `:shards`, which has to be a list of numbers and ranges. |
| 17 | +
|
| 18 | + Examples: `[1..3]` `[1, 2, 3]` `[1..3, 8, 9]` |
| 19 | + > If omitted all shards will be run. |
| 20 | +
|
| 21 | + - Optionally `:dispatcher`, which has to be a valid `GenStage.Dispatcher` or a tuple of one and initial state. |
| 22 | + > See `Crux.Gateway.Connection.Producer` for more info. |
| 23 | + """ |
| 24 | + |
| 25 | + @typedoc """ |
| 26 | + Used to specify or override gateway options when initially starting the connection. |
| 27 | +
|
| 28 | + See `start/1` |
| 29 | + """ |
| 30 | + @type gateway_options :: %{ |
| 31 | + optional(:token) => String.t(), |
| 32 | + optional(:url) => String.t(), |
| 33 | + optional(:shard_count) => pos_integer(), |
| 34 | + optional(:shards) => [non_neg_integer | Range.t()], |
| 35 | + optional(:dispatcher) => GenStage.Dispatcher.t() | {GenStage.Dispatcher.t(), term} |
| 36 | + } |
| 37 | + |
| 38 | + @doc """ |
| 39 | + Initialises the connection(s) and actually starts the gateway. |
| 40 | +
|
| 41 | + You can specify or override `:token`, `:url`, `:shard_count` and `:shards` here via `t:gateway_options`. |
| 42 | + """ |
| 43 | + @spec start(args :: gateway_options) :: [Supervisor.on_start_child()] |
| 44 | + def start(args \\ %{}) do |
| 45 | + producer = Map.get(args, :dispatcher, GenStage.BroadcastDispatcher) |
| 46 | + Application.put_env(:crux_gateway, :dispatcher, producer) |
| 47 | + |
| 48 | + shard_count = fetch_or_put_env(args, :shard_count, &is_number/1) |
| 49 | + |
| 50 | + shards = |
| 51 | + case Application.fetch_env(:crux_gateway, :shards) do |
| 52 | + :error -> |
| 53 | + shards = Enum.to_list(0..(shard_count - 1)) |
| 54 | + Application.put_env(:crux_gateway, :shards, shards) |
| 55 | + |
| 56 | + shards |
| 57 | + |
| 58 | + {:ok, shards} when is_list(shards) -> |
| 59 | + shards = |
| 60 | + shards |
| 61 | + |> Enum.flat_map(&map_shard/1) |
| 62 | + |> Enum.uniq() |
| 63 | + |> Enum.sort() |
| 64 | + |
| 65 | + if Enum.min(shards) < 0 do |
| 66 | + raise """ |
| 67 | + Specified shards are out of range. |
| 68 | + A negative shard id is not valid |
| 69 | +
|
| 70 | + :shards resolved to: |
| 71 | + #{inspect(shards)} |
| 72 | + """ |
| 73 | + end |
| 74 | + |
| 75 | + if Enum.max(shards) >= shard_count do |
| 76 | + raise """ |
| 77 | + Specified shards are out of range. |
| 78 | + Shard ids must be lower than shard_count |
| 79 | +
|
| 80 | + :shards resolved to: |
| 81 | + #{inspect(shards)} |
| 82 | + """ |
| 83 | + end |
| 84 | + |
| 85 | + Application.put_env(:crux_gateway, :shards, shards) |
| 86 | + |
| 87 | + shards |
| 88 | + |
| 89 | + _ -> |
| 90 | + raise_shards() |
| 91 | + end |
| 92 | + |
| 93 | + %{ |
| 94 | + url: fetch_or_put_env(args, :url, &is_bitstring/1), |
| 95 | + token: fetch_or_put_env(args, :token, &is_bitstring/1), |
| 96 | + shard_count: shard_count |
| 97 | + } |
| 98 | + |> Crux.Gateway.Supervisor.start_gateway(shards) |
| 99 | + end |
| 100 | + |
| 101 | + defp fetch_or_put_env(args, atom, validator) do |
| 102 | + value = |
| 103 | + case args do |
| 104 | + %{^atom => value} -> |
| 105 | + Application.put_env(:crux_gateway, atom, value) |
| 106 | + |
| 107 | + value |
| 108 | + |
| 109 | + _ -> |
| 110 | + Application.fetch_env!(:crux_gateway, atom) |
| 111 | + end |
| 112 | + |
| 113 | + if validator.(value) do |
| 114 | + value |
| 115 | + else |
| 116 | + raise """ |
| 117 | + :#{inspect(atom)} is not of the correct type. |
| 118 | +
|
| 119 | + Received: |
| 120 | + #{inspect(value)} |
| 121 | + """ |
| 122 | + end |
| 123 | + end |
| 124 | + |
| 125 | + defp map_shard(num) when is_number(num), do: [num] |
| 126 | + defp map_shard(%Range{} = range), do: range |
| 127 | + |
| 128 | + defp map_shard(other) do |
| 129 | + """ |
| 130 | +
|
| 131 | + Faulty element: |
| 132 | + #{inspect(other)} |
| 133 | + """ |
| 134 | + |> raise_shards() |
| 135 | + end |
| 136 | + |
| 137 | + defp raise_shards(suffix \\ "") do |
| 138 | + raise """ |
| 139 | + :shards must be a list of numbers and/or ranges |
| 140 | +
|
| 141 | + Received :shards value: |
| 142 | + #{inspect(Application.fetch_env!(:crux_gateway, :shards))} |
| 143 | + """ <> suffix |
| 144 | + end |
| 145 | +end |
0 commit comments