gateway

package
v0.0.0-...-fa60fbe Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2021 License: ISC Imports: 12 Imported by: 0

Documentation

Overview

Package gateway contains a gateway client and event handlers. Sadly, thanks to how Revolt sends events, gateway clients also need a cache, so it doesn't work completely standalone.

Index

Constants

View Source
const (
	ErrUnlabeled             = errors.Sentinel("unlabeled error")
	ErrInternalError         = errors.Sentinel("internal error")
	ErrInvalidSession        = errors.Sentinel("invalid session")
	ErrOnboardingNotFinished = errors.Sentinel("onboarding not finished")
	ErrAlreadyAuthenticated  = errors.Sentinel("already authenticated")
)

Authentication errors

View Source
const ErrNotAuthenticatedEvent = errors.Sentinel("first event wasn't Authenticated")

ErrNotAuthenticatedEvent is returned when the first event from the websocket isn't the Authenticated or Error event.

View Source
const ErrWSAlreadyOpen = errors.Sentinel("websocket already opened")

ErrWSAlreadyOpen is returned when you attempt to open a websocket that already is open.

Variables

View Source
var StoreDebug = func(tmpl string, args ...interface{}) {
	return
}

StoreDebug is called for store debug logging.

View Source
var WSDebug = func(tmpl string, args ...interface{}) {
	return
}

WSDebug is called for websocket debug logging.

Functions

This section is empty.

Types

type ChannelCreateEvent

type ChannelCreateEvent struct {
	revolt.Channel
}

ChannelCreateEvent ...

type ChannelStartTypingEvent

type ChannelStartTypingEvent struct {
	ChannelID string `json:"channel_id"`
	UserID    string `json:"user"`
}

ChannelStartTypingEvent ...

type ChannelStopTypingEvent

type ChannelStopTypingEvent struct {
	ChannelID string `json:"channel_id"`
	UserID    string `json:"user"`
}

ChannelStopTypingEvent ...

type ChannelUpdateEvent

type ChannelUpdateEvent struct {
	ID    string                 `json:"id"`
	Data  ChannelUpdateEventData `json:"data"`
	Clear string                 `json:"clear,omitempty"`
}

ChannelUpdateEvent ...

func (ChannelUpdateEvent) Update

func (ev ChannelUpdateEvent) Update(ch *revolt.Channel)

Update updates the given channel with data from the event.

type ChannelUpdateEventData

type ChannelUpdateEventData struct {
	Type *string            `json:"channel_type,omitempty"`
	Name *string            `json:"name,omitempty"`
	Icon *revolt.Attachment `json:"icon,omitempty"`
	NSFW *bool              `json:"nsfw,omitempty"`

	Description *string `json:"description,omitempty,omitempty"`

	LastMessageID *string `json:"last_message_id,omitempty"`

	// Only in groups
	Permissions *uint64 `json:"permissions,omitempty"`
	// Only in servers
	DefaultPermissions *uint64           `json:"default_permissions,omitempty"`
	RolePermissions    map[string]uint64 `json:"role_permissions,omitempty"`

	// If a DM, whether it's active
	DMActive *bool `json:"active,omitempty"`
	// If a group, the owner's ID
	GroupOwnerID *string   `json:"owner,omitempty"`
	Recipients   *[]string `json:"recipients,omitempty"`
}

ChannelUpdateEventData ...

type DisconnectEvent

type DisconnectEvent struct{}

DisconnectEvent is emitted when the gateway connection is closed.

type Gateway

type Gateway struct {
	Handler *Handler
	Store   store.Store

	LastPing, LastPong time.Time

	// if the time since the last pong is larger than this, trigger a gateway reconnect
	// default: 3 minutes
	MaximumPong time.Duration

	sync.RWMutex
	// contains filtered or unexported fields
}

Gateway is a gateway client.

func New

func New(url, token string) *Gateway

New creates a new Gateway. Note: only bot tokens are supported.

func (*Gateway) Close

func (g *Gateway) Close() (err error)

Close closes the gateway connection.

func (*Gateway) Open

func (g *Gateway) Open() (err error)

Open opens the gateway connection.

func (*Gateway) Ping

func (g *Gateway) Ping() time.Duration

Ping returns the latency between the last ping and pong.

type Handler

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

Handler is a container for command handlers. A zero-value instance is a valid instance.

func NewHandler

func NewHandler() *Handler

NewHandler returns a new Handler.

func (*Handler) AddHandler

func (h *Handler) AddHandler(handler interface{}) (rm func())

AddHandler adds the handler, returning a function that would remove this handler when called. A handler type is either a single-argument no-return function or a channel.

Function

A handler can be a function with a single argument that is the expected event type. It must not have any returns or any other number of arguments.

// An example of a valid function handler.
h.AddHandler(func(*gateway.MessageCreateEvent) {})

Channel

A handler can also be a channel. The underlying type that the channel wraps around will be the event type. As such, the type rules are the same as function handlers.

Keep in mind that the user must NOT close the channel. In fact, the channel should not be closed at all. The caller function WILL PANIC if the channel is closed!

When the rm callback that is returned is called, it will also guarantee that all blocking sends will be cancelled. This helps prevent dangling goroutines.

// An example of a valid channel handler.
ch := make(chan *gateway.MessageCreateEvent)
h.AddHandler(ch)

func (*Handler) AddHandlerCheck

func (h *Handler) AddHandlerCheck(handler interface{}) (rm func(), err error)

AddHandlerCheck adds the handler, but safe-guards reflect panics with a recoverer, returning the error. Refer to AddHandler for more information.

func (*Handler) AddSyncHandler

func (h *Handler) AddSyncHandler(handler interface{}) (rm func())

AddSyncHandler is a synchronous variant of AddHandler. Handlers added using this method will block the Call method, which is helpful if the user needs to rely on the order of events arriving. Handlers added using this method should not block for very long, as it may clog up other handlers.

func (*Handler) AddSyncHandlerCheck

func (h *Handler) AddSyncHandlerCheck(handler interface{}) (rm func(), err error)

AddSyncHandlerCheck is the safe-guarded version of AddSyncHandler. It is similar to AddHandlerCheck.

func (*Handler) Call

func (h *Handler) Call(ev interface{})

Call calls all handlers with the given event. This is an internal method; use with care.

func (*Handler) ChanFor

func (h *Handler) ChanFor(fn func(interface{}) bool) (out <-chan interface{}, cancel func())

ChanFor returns a channel that would receive all incoming events that match the callback given. The cancel() function removes the handler and drops all hanging goroutines.

This method is more intended to be used as a filter. For a persistent event channel, consider adding it directly as a handler with AddHandler.

func (*Handler) WaitFor

func (h *Handler) WaitFor(ctx context.Context, fn func(interface{}) bool) interface{}

WaitFor blocks until there's an event. It's advised to use ChanFor instead, as WaitFor may skip some events if it's not ran fast enough after the event arrived.

type MessageDeleteEvent

type MessageDeleteEvent struct {
	ID        string `json:"id"`
	ChannelID string `json:"channel_id"`
}

MessageDeleteEvent ...

type MessageEvent

type MessageEvent struct {
	revolt.Message
}

MessageEvent ...

type MessageUpdateEvent

type MessageUpdateEvent struct {
	ID   string         `json:"id"`
	Data revolt.Message `json:"data"`
}

MessageUpdateEvent ...

type ReadyEvent

type ReadyEvent struct {
	Users    []revolt.User    `json:"users"`
	Servers  []revolt.Server  `json:"servers"`
	Channels []revolt.Channel `json:"channels"`
}

ReadyEvent ...

type UnknownEvent

type UnknownEvent struct {
	Type    string
	RawData []byte
}

UnknownEvent is emitted when an unknown event is received.

Jump to

Keyboard shortcuts

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