Documentation
¶
Overview ¶
Package discord implements the comms contract on Discord, using disgoorg/disgo.
Register it with a blank import:
import _ "gitlab.com/phpboyscout/go/comms-discord"
c, err := comms.NewClient(ctx, "discord", comms.ClientConfig{
Token: tok,
Needs: []comms.Need{comms.NeedMessages},
})
p, err := c.Provider(ctx, guild, comms.WithAllowedChannels(chans...))
Why disgo ¶
It is the only candidate Go Discord library that implements resume_gateway_url, the endpoint Discord supplies for resuming a session. Reconnecting elsewhere risks the resume being rejected, which degrades to a fresh identify and drops every event buffered during the outage. For a bot that answers questions, those are questions nobody will answer — a failure with no error and no trace.
Index ¶
Constants ¶
const Name = "discord"
Name is the key this provider registers under.
Variables ¶
var ErrFormWindowClosed = errors.NewSentinel("comms-discord.form_window_closed", "discord: form window closed; the interaction was already acknowledged")
ErrFormWindowClosed reports that a form can no longer be opened for an interaction, because the acknowledgement has already been sent.
Distinct from ErrUnsupported: the capability exists, the moment passed. A caller that cannot tell those apart will retry something that can never succeed.
var ErrTooManyChoices = errors.NewSentinel("comms-discord.too_many_choices", "discord: too many choices for one message")
ErrTooManyChoices reports that a prompt offers more choices than Discord can render on one message.
Functions ¶
func ConnectControl ¶
ConnectControl opens the gateway of a client built for a control hook.
It exists because a broker holds no space. NewClient returns a comms.Client, whose surface is Provider and Close; Connect belongs to a Reader, and a Reader is reached only through a scope. A broker wants no scope and no message feed -- minting one to open a transport would allocate a feed nobody reads and say the wrong thing about what a broker is.
It refuses a client built without WithControl: on that client a scope is the way in, and connecting behind the scopes' backs would leave them thinking they were disconnected.
func NewClient ¶
NewClient builds a Discord client from a consumer's declared configuration.
It opens NOTHING. disgo derives the application id from the token, so constructing eagerly would make this fail without live credentials — and a client that cannot be built without secrets cannot be inspected for its capabilities or exercised by the conformance harness, which runs without a network by design.
The connection is dialled by the first comms.Reader.Connect across the scopes this mints. A Client owns the transport's LIFETIME, not its construction.
A missing token is therefore NOT an error here. Construction validates configuration; Connect reaches out, and that is where a credential is needed and where its absence is reported.
Types ¶
type ControlHook ¶
type ControlHook interface {
// Attach is called with every client this provider builds, BEFORE that
// client's gateway is opened, so the hook's listeners are installed before
// a resume replays anything.
//
// An error here fails the construction or the connection that caused it: a
// broker that cannot attach cannot serve, and a provider that carried on
// would hold a session nothing was listening to.
Attach(client *bot.Client) error
}
ControlHook is what WithControl attaches. The broker implements it.
type Option ¶
type Option func(*options)
Option configures a provider.
func WithBotMessages ¶
func WithBotMessages() Option
WithBotMessages stops the provider filtering bot-authored messages.
Test affordance only. Testing a chat bot needs either a second identity or a deliberate bypass, since a bot ignores its own messages by contract. It is an Option rather than a Config field so no production configuration file can reach it.
func WithClient ¶
WithClient supplies a pre-built disgo client. Intended for tests, which drive the provider through a fake gateway rather than a live connection.
func WithControl ¶
func WithControl(h ControlHook) Option
WithControl gives the gateway to a control hook and takes voice away from this provider.
The two halves are one option because they are one decision. A process serving voice workers must not also join channels itself: it would be competing with them for the one voice connection per guild that Discord allows, and the failure would appear in whichever of them lost.
func WithResumeState ¶
WithResumeState gives the gateway a session to resume rather than a fresh identify.
A zero state sets nothing, so a first start identifies. An empty ResumeURL selects no URL rather than an empty one, because disgo dials whatever the pointer holds and an empty string would dial nothing.
Source Files
¶
- actor.go
- authoring.go
- channel.go
- client.go
- closecode.go
- commands.go
- control.go
- convert.go
- directory.go
- formkey.go
- indicator.go
- intents.go
- interactive.go
- manage.go
- mentions.go
- participants.go
- post.go
- provider.go
- reactions.go
- reader_events.go
- scopegate.go
- space.go
- stage.go
- thread_history.go
- voice.go
- voice_wiring.go
- voice_wiring_cgo.go
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
voiceengine
Package voiceengine is the join and session engine shared by the Discord provider and the Discord voice node.
|
Package voiceengine is the join and session engine shared by the Discord provider and the Discord voice node. |
|
Package voicenode is a comms.Client that holds a Discord voice channel and nothing else: no gateway, no REST, no bot token.
|
Package voicenode is a comms.Client that holds a Discord voice channel and nothing else: no gateway, no REST, no bot token. |
|
broker
Package broker is the gateway holder's side of the control link: it owns every guild the bot is in, one worker at a time, and turns requests from a bus into Opcode 4 on a Discord gateway it already holds.
|
Package broker is the gateway holder's side of the control link: it owns every guild the bot is in, one worker at a time, and turns requests from a bus into Opcode 4 on a Discord gateway it already holds. |
|
broker/storetest
Package storetest is the suite any broker.Store must pass.
|
Package storetest is the suite any broker.Store must pass. |
|
internal/lossy
Package lossy is a messaging.Backend decorator that drops the sends a test names.
|
Package lossy is a messaging.Backend decorator that drops the sends a test names. |
|
internal/wire
Package wire is the control link's payloads and subjects, shared by the link and the broker so the two cannot drift.
|
Package wire is the control link's payloads and subjects, shared by the link and the broker so the two cannot drift. |
|
link
Package link is the worker's side of the control link: a voicenode.Control that proxies Opcode 4 over a messaging bus and hands back the two gateway events a voice join needs.
|
Package link is the worker's side of the control link: a voicenode.Control that proxies Opcode 4 over a messaging bus and hands back the two gateway events a voice join needs. |