A flexible elixir gateway for the Discord API
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
config
deploy
lib
.formatter.exs
.gitignore chore: inital commit Apr 25, 2018
.travis.yml
LICENSE
README.md
mix.exs
mix.lock

README.md

Crux.Gateway

Package providing a flexible gateway connection to the Discord API.

Useful links

Installation

The package can be installed by adding crux_gateway to your list of dependencies in mix.exs:

def deps do
  [
    {:crux_gateway, "~> 0.1.4"}
  ]
end

Usage

For example:

defmodule MyApp.Application do
  use Application

  def start(_, _) do
    children = [
      {Crux.Gateway,
       {%{
          token: "your token",
          url: "current gateway url",
          shard_count: 1
        }, name: MyApp.Gateway}},
      MyApp.Consumer
    ]

    Supervisor.start_link(children, strategy: :one_for_one)
  end
end
defmodule MyApp.Consumer do
  use GenStage

  def start_link(args) do
    GenStage.start_link(__MODULE__, args)
  end

  def init(args) do
    pids = Crux.Gateway.Connection.Producer.producers(MyApp.Gateway) |> Map.values()
    {:consumer, args, subscribe_to: pids}
  end

  def handle_events(events, _from, state) do
    for {type, data, shard_id} <- events do
      handle_event(type, data, shard_id)
    end

    {:noreply, [], state}
  end

  def handle_event(
        :MESSAGE_CREATE,
        %{content: "ping", author: %{username: username, discriminator: discriminator}},
        _shard_id
      ) do
    IO.puts("Received a ping from #{username}##{discriminator}!")
  end

  def handle_event(_, _, _), do: nil
end