gda

package module
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 31, 2026 License: MIT Imports: 9 Imported by: 0

README

Go Discord API

Overview

gda is a low-level Go driver for the Discord API v10 and Gateway WebSocket protocol, implemented using only the Go standard library. It provides direct access to event streams, REST endpoints, and interaction webhooks without forcing an opinionated application architecture.

Ecosystem

gda is designed to be the protocol foundation:

  • gda: Direct access to Gateway events, REST routes, and interaction handlers. You control socket lifecycles, HTTP servers, and execution flows directly.
  • gdk: A higher-level interaction framework built on top of gda. Choose gdk if you want automated slash command registration, cooldowns, role permission guards, and auto-deferrals.

[!NOTE] gdk is currently under development and will be available soon.

Install

go get github.com/sfossl/gda

Example

package main

import (
	"context"
	"fmt"
	"log"
	"os"

	"github.com/sfossl/gda"
	"github.com/sfossl/gda/rest"
	"github.com/sfossl/gda/types"
)

func main() {
	client, err := gda.New(
		os.Getenv("TOKEN"),
		gda.WithIntents(types.IntentGuildMessages|types.IntentMessageContent),
	)
	if err != nil {
		log.Fatal(err)
	}

	client.OnReady(func(ctx context.Context, r *types.ReadyEvent) {
		fmt.Printf("Logged in as %s\n", r.User.Username)
	})

	client.OnMessageCreate(func(ctx context.Context, msg *types.Message) {
		if msg.Author.Bot {
			return
		}
		if msg.Content == "hi" {
			_, _ = client.REST.SendMessage(ctx, msg.ChannelID, rest.MessagePayload{
				Content: "hello from gda!",
			})
		}
	})

	if err := client.Start(context.Background()); err != nil {
		log.Fatal(err)
	}
}

Design

  • State Storage: gda does not maintain global caches for guilds, channels, or members. State requirements vary drastically between a single-process bot and a multi-shard cluster, so caching strategy is left entirely to the application layer.
  • Audio: Discord voice streaming requires Opus encoding and media transcoding. Rather than introducing CGO or external FFmpeg dependencies, gda provides voice protocol types and leaves media processing to dedicated audio tooling.
  • Dependencies: The entire driver relies solely on standard library packages (net/http, crypto/ed25519, compress/zlib), ensuring fast builds, zero dependency drift, and simple cross-compilation.

Docs

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrMissingToken         = errors.New("bot token is required")
	ErrInvalidShard         = errors.New("shard id must be non-negative and less than shard count")
	ErrMissingMessageIntent = errors.New("missing IntentGuildMessages or IntentDirectMessages for message events")
)
View Source
var (
	ErrMissingPublicKey = errors.New("public key is required for webhook interactions")
)

Functions

This section is empty.

Types

type Client

type Client struct {
	Config     Config
	REST       *rest.Client
	Gateway    *gateway.Client
	Router     *interaction.Router
	Dispatcher *gateway.EventDispatcher
}

func New

func New(token string, opts ...Option) (*Client, error)

func NewWithConfig

func NewWithConfig(cfg Config) (*Client, error)

func (*Client) Close

func (c *Client) Close() error

func (*Client) Latency

func (c *Client) Latency() time.Duration

func (*Client) On

func (c *Client) On(event types.EventName, h gateway.RawHandler)

func (*Client) OnCommand

func (c *Client) OnCommand(name string, h interaction.Handler)

func (*Client) OnComponent

func (c *Client) OnComponent(customID string, h interaction.Handler)

func (*Client) OnMessageCreate

func (c *Client) OnMessageCreate(h func(context.Context, *types.Message))

func (*Client) OnModal

func (c *Client) OnModal(customID string, h interaction.Handler)

func (*Client) OnReady

func (c *Client) OnReady(h func(context.Context, *types.ReadyEvent))

func (*Client) OnSubcommand

func (c *Client) OnSubcommand(command, subcommand string, h interaction.Handler)

func (*Client) OnSubcommandGroup

func (c *Client) OnSubcommandGroup(command, group, subcommand string, h interaction.Handler)

func (*Client) Start

func (c *Client) Start(ctx context.Context) error

func (*Client) String

func (c *Client) String() string

func (*Client) TriggerTyping

func (c *Client) TriggerTyping(ctx context.Context, channelID types.Snowflake) error

func (*Client) UpdatePresence

func (c *Client) UpdatePresence(ctx context.Context, presence types.PresenceUpdate) error

func (*Client) ValidateEventIntents

func (c *Client) ValidateEventIntents(event types.EventName) error

func (*Client) WebhookHandler

func (c *Client) WebhookHandler() (http.Handler, error)

type Config

type Config struct {
	Token          string
	AppID          string
	PublicKey      string
	Intents        types.Intent
	ShardID        int
	ShardCount     int
	HTTPTimeout    time.Duration
	MaxRetries     int
	LargeThreshold int
	AutoDefer      bool
	Compress       bool
}

func DefaultConfig

func DefaultConfig(token string) Config

func (Config) String

func (c Config) String() string

func (*Config) Validate

func (c *Config) Validate() error

func (Config) ValidateEventIntents

func (c Config) ValidateEventIntents(event types.EventName) error

type Option

type Option func(*Config)

func WithAppID

func WithAppID(appID string) Option

func WithAutoDefer

func WithAutoDefer(autoDefer bool) Option

func WithCompression

func WithCompression(compress bool) Option

func WithHTTPTimeout

func WithHTTPTimeout(d time.Duration) Option

func WithIntents

func WithIntents(intents types.Intent) Option

func WithLargeThreshold

func WithLargeThreshold(threshold int) Option

func WithMaxRetries

func WithMaxRetries(retries int) Option

func WithPublicKey

func WithPublicKey(publicKey string) Option

func WithShard

func WithShard(shardID, shardCount int) Option

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL