agat

Package Version Hex Docs

🌟 A Gleamy Discord Gateway API Client

Agat is a Discord Gateway API Client for the Erlang target, being built upon the grom library.

It should be very ergonomic to use to long-time Gleam users, while also handling various internal mechanisms of the Gateway API.

Agat is considered experimental; there are many breaking changes to come, and bugs may be uncovered. Please report any bugs in issues.

Features

Installation

gleam add agat

Example

import gleam/httpc
import gleam/otp/static_supervisor
import gleam/result
import grom
import agat

type GatewayState {
  GatewayState(
    // hold your usual state stuff here,
    // database process names, agat process names,
    // etc.
  )
}

pub fn main() -> Nil {
  let names = agat.create_names()

  let assert Ok(connection_info) =
    grom.get_gateway_connection_info(todo as "token")
    |> httpc.send
    |> result.replace_error(Nil)
    |> result.try(grom.gateway_connection_info_response)
    |> result.replace_error(Nil)
    |> result.try(agat.create_connection_info)

  let identification = agat.create_identification(
    token: todo as "token",
    intents: grom.all_unprivileged_gateway_intents
  )

  let state = GatewayState(todo as "your state")

  // By default, this will create the amount of shards Discord recommends
  // in the connection info you requested earlier.
  let gateway =
    agat.new(
      names:,
      connection_info:,
      identification:,
      state:,
    )
    |> agat.on_event(on_event)
    |> agat.supervised

  let assert Ok(_supervisor) =
    static_supervisor.new(static_supervisor.OneForOne)
    |> static_supervisor.add(gateway)
    |> static_supervisor.start
}

fn on_event(state: GatewayState, event: agat.Event) {
  case event {
    // Careful! This fires once per shard.
    agat.Ready(ready) -> echo ready.application.id
    // Mischevious!
    agat.MessageCreate(message) -> todo as "Delete message"
  }
}

Further documentation can be found at https://agat.hexdocs.pm/.

✨ Search Document