Help us shape the future of Go! Take the 2026 Go Developer Survey

discord

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Sep 20, 2026 License: MIT Imports: 21 Imported by: 0

Documentation

Overview

Package discord implements the Discord adapter for the orb chat gateway: Gateway (WebSocket) ingress over the internal RFC 6455 client — hello/heartbeat/identify, READY capture, resume-first reconnects — plus REST delivery with a refreshed typing indicator, streamed preview edits, chunked finalization with mass-mention suppression, and pre-signed attachment download. It plugs into the chat processor via chat.Adapter; ingress runs through Adapter.Run.

Index

Constants

View Source
const DefaultBaseURL = "https://discord.com/api/v10"

DefaultBaseURL is the production REST API endpoint.

Variables

This section is empty.

Functions

This section is empty.

Types

type APIError

type APIError struct {
	// Method and Path identify the failed call.
	Method, Path string
	// Status is the HTTP status code.
	Status int
	// Code is the Discord JSON error code (e.g. 10008 Unknown Message), 0
	// when the body carried none.
	Code int
	// Message is the error text from the response body.
	Message string
	// RetryAfter is the server-requested pause from a 429 response.
	RetryAfter time.Duration
}

APIError is a decoded Discord REST failure.

func (*APIError) Error

func (e *APIError) Error() string

Error implements the error interface.

type Adapter

type Adapter struct {
	// contains filtered or unexported fields
}

Adapter is the Discord implementation of chat.Adapter.

func New

func New(opts Options) (*Adapter, error)

New creates a Discord adapter. It performs no network calls; the gateway session starts with Adapter.Run.

func (*Adapter) Account

func (a *Adapter) Account() string

Account returns the bot user id this adapter serves, matching the Account set on every normalized message. It is fixed at construction — from Options.BotUserID or the bot id embedded in the token — because the processor registers it before the gateway connects; the READY event confirms it and logs a mismatch.

func (*Adapter) Download

func (a *Adapter) Download(ctx context.Context, ref chat.AttachmentRef) (io.ReadCloser, string, error)

Download implements chat.Adapter. ref.ID is the pre-signed CDN URL of the inbound attachment, fetched with a plain GET — signed links reject Authorization headers.

ponytail: signed URLs expire (~24h); turns run promptly after ingress so the link is fresh — a much later replay 404s instead of re-resolving a fresh signed URL through the message endpoint.

func (*Adapter) NewDelivery

func (a *Adapter) NewDelivery(key chat.ConversationKey, replyTo string, resumePreviewID string) chat.Delivery

NewDelivery implements chat.Adapter. replyTo is the inbound event id ("dc:<channel_id>:<message_id>"); a non-empty resumePreviewID makes Finalize edit that message instead of sending a new one (crash recovery).

func (*Adapter) Platform

func (a *Adapter) Platform() string

Platform implements chat.Adapter.

func (*Adapter) Run

func (a *Adapter) Run(ctx context.Context, publish func(chat.Message) error) error

Run connects to the Discord gateway and pumps normalized MESSAGE_CREATE dispatches into publish until ctx ends or a fatal configuration error (bad token, disallowed intents) occurs. Connection losses reconnect with capped exponential backoff, resuming the previous session whenever the close code allows it — a fresh IDENTIFY is budgeted (1000/day), so resume is always tried first. Run one Run per bot account.

publish must enqueue durably and return fast: it is the ingress publish-then-ack edge and must never wait on turn processing.

type Options

type Options struct {
	// Token is the bot token from the Developer Portal. Required.
	Token string
	// BaseURL overrides the REST endpoint (tests). Default [DefaultBaseURL].
	BaseURL string
	// BotUserID pre-seeds the bot identity used for [Adapter.Account], echo
	// filtering, and mention gating. When empty it is derived from the bot
	// id embedded in the token and confirmed by the gateway READY event.
	BotUserID string
	// HTTPClient overrides the REST client (tests). Default: 30s timeout.
	HTTPClient *http.Client
	// TypingInterval is the typing-indicator refresh cadence (the indicator
	// expires after ~10s). Default 4s.
	TypingInterval time.Duration
	// PreviewMinInterval rate-limits preview edits per delivery. Default
	// 1.5s (safely under the ~5/5s per-channel message bucket).
	PreviewMinInterval time.Duration
	// Logger receives non-fatal diagnostics. Optional.
	Logger *slog.Logger
}

Options configures New.

Jump to

Keyboard shortcuts

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