state

package
v3.2.4 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2021 License: MIT Imports: 14 Imported by: 4

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidHandler gets returned if a handler given to
	// EventManager.AddHandler or EventManager.MustAddHandler is not a valid
	// handler func, i.e. not following the form of func(*State, e) where e is
	// either a pointer to an event, *Base or interface{}.
	ErrInvalidHandler = errors.New("state: the passed interface{} does not resemble a valid handler")
	// ErrInvalidMiddleware gets returned if a middleware given to
	// EventManger.AddHandler or EventManager.MustAddHandler has not the same
	// type as its handler.
	//
	// Additionally, it is returned by AddMiddleware and
	// MustAddMiddleware if the middleware func is invalid.
	ErrInvalidMiddleware = errors.New("state: the passed middleware does not match the type of the handler")

	// Filtered should be returned if a filter blocks an event.
	Filtered = errors.New("filtered") //nolint:golint,stylecheck
)

Functions

This section is empty.

Types

type Base

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

Base is the base of all events.

func NewBase

func NewBase() *Base

NewBase creates a new Base.

func (*Base) Get

func (b *Base) Get(key interface{}) (val interface{})

Get gets the element with the passed key.

func (*Base) Lookup

func (b *Base) Lookup(key interface{}) (val interface{}, ok bool)

Lookup returns the element with the passed key. Additionally, it specifies with the second return parameter, if the element exists, acting similar to a two parameter map lookup.

func (*Base) Set

func (b *Base) Set(key, val interface{})

Set stores the passed element under the given key.

type ChannelUnreadUpdateEvent

type ChannelUnreadUpdateEvent struct {
	*gateway.ChannelUnreadUpdateEvent
	*Base
}

type CloseEvent

type CloseEvent struct {
	*Base
}

CloseEvent gets dispatched when the gateway closes.

type EventHandler

type EventHandler struct {
	ErrorHandler func(err error)
	PanicHandler func(err interface{})
	// contains filtered or unexported fields
}

func NewEventHandler

func NewEventHandler(s *State) *EventHandler

NewEventHandler creates a new EventHandler.

func (*EventHandler) AddHandler

func (h *EventHandler) AddHandler(handler interface{}, middlewares ...interface{}) (rm func(), err error)

AddHandler adds a handler with the passed middlewares to the event handlers. A handler can either be a function, or a channel of type chan *eventType. Note, however, that channel sends are non-blocking, and you must either buffer your channel sufficiently, or ensure you are listening.

The signature of a handler func is func(*State, e) where e is either a pointer to an event, *Base or interface{}. Optionally, a handler may return an error.

Middlewares must be of the same type as the handlers or must be an interface{} or Base handlers.

func (*EventHandler) AddHandlerOnce added in v3.1.0

func (h *EventHandler) AddHandlerOnce(handler interface{}, middlewares ...interface{}) error

AddHandlerOnce adds a handler that is only executed once. If middlewares prevent execution, the handler will be executed on the next event.

func (*EventHandler) AddMiddleware

func (h *EventHandler) AddMiddleware(f interface{}) error

AddMiddleware adds the passed middleware as a global middleware.

The signature of a middleware func is func(*State, e) where e is either a pointer to an event, *Base or interface{}. Optionally, a middleware may return an error.

func (*EventHandler) AutoAddHandlers

func (h *EventHandler) AutoAddHandlers(scan interface{}, middlewares ...interface{})

AutoAddHandlers adds all handlers methods of the passed struct to the EventHandler. scan must be a pointer to a struct.

func (*EventHandler) Call

func (h *EventHandler) Call(e interface{})

Call can be used to manually dispatch an event. For this to succeed, e must be a pointer to an event, and it's Base field must be set.

func (*EventHandler) Close

func (h *EventHandler) Close()

Close stops the event listener and blocks until all handlers have finished executing.

func (*EventHandler) DeriveIntents

func (h *EventHandler) DeriveIntents() (i gateway.Intents)

DeriveIntents derives the intents based on the event handlers and global middlewares that were added. Interface and Base handlers will not be taken into account.

Note that this does not reflect the intents needed to enable caching for API calls made anywhere in code.

func (*EventHandler) MustAddHandler

func (h *EventHandler) MustAddHandler(handler interface{}, middlewares ...interface{}) func()

MustAddHandler is the same as AddHandler, but panics if AddHandler returns an error.

func (*EventHandler) MustAddHandlerOnce added in v3.1.0

func (h *EventHandler) MustAddHandlerOnce(handler interface{}, middlewares ...interface{})

MustAddHandlerOnce is the same as AddHandlerOnce, but panics if AddHandlerOnce returns an error.

func (*EventHandler) MustAddMiddleware

func (h *EventHandler) MustAddMiddleware(f interface{})

MustAddMiddleware is the same as AddMiddleware but panics if AddMiddleware returns an error.

func (*EventHandler) Open

func (h *EventHandler) Open(events <-chan interface{})

Open starts listening for events until the returned closer function is called.

type GuildAvailableEvent

type GuildAvailableEvent struct {
	*GuildCreateEvent
}

GuildAvailableEvent is a situation-specific GuildCreate event. It gets fired when a guild becomes available, after getting marked unavailable during a GuildUnavailableEvent event. This event will not be fired for guilds that were already unavailable when initially connecting.

type GuildCreateEvent

type GuildCreateEvent struct {
	*gateway.GuildCreateEvent
	*Base
}

https://discord.com/developers/docs/topics/gateway#guild-create

Note that this event will not be sent in Base and All handlers. Instead, the situation-specific sub-events will be sent.

type GuildDeleteEvent

type GuildDeleteEvent struct {
	*gateway.GuildDeleteEvent
	*Base

	Old *discord.Guild
}

https://discord.com/developers/docs/topics/gateway#guild-delete

Note that this event will not be sent in Base and All handlers. Instead, the situation-specific sub-events will be sent.

type GuildJoinEvent

type GuildJoinEvent struct {
	*GuildCreateEvent
}

GuildJoinEvent is a situation-specific GuildCreate event. It gets fired when the user/bot joins a guild.

type GuildLeaveEvent

type GuildLeaveEvent struct {
	*GuildDeleteEvent
}

GuildLeaveEvent is a situation-specific GuildDeleteEvent event. It gets fired when the user/bot leaves guild, gets kicked/banned from it, or the owner deletes it.

type GuildReadyEvent

type GuildReadyEvent struct {
	*GuildCreateEvent
}

GuildReadyEvent is a situation-specific GuildCreate event. It gets fired during Ready for all available guilds. Additionally, it gets fired for all those guilds that become available after initially connecting, but were not during Ready.

type GuildUnavailableEvent

type GuildUnavailableEvent struct {
	*GuildDeleteEvent
}

GuildUnavailableEvent is a situation-specific GuildDeleteEvent event. It gets fired if the guild becomes unavailable, e.g. through a discord outage.

type GuildUpdateEvent

type GuildUpdateEvent struct {
	*gateway.GuildUpdateEvent
	*Base

	Old *discord.Guild
}

https://discord.com/developers/docs/topics/gateway#guild-update

Note that this event will not be sent in Base and All handlers. Instead, the situation-specific sub-events will be sent.

type PresencesReplaceEvent

type PresencesReplaceEvent struct {
	*gateway.PresencesReplaceEvent
	*Base
}

undocumented

type RelationshipAddEvent

type RelationshipAddEvent struct {
	*gateway.RelationshipAddEvent
	*Base
}

undocumented

type RelationshipRemoveEvent

type RelationshipRemoveEvent struct {
	*gateway.RelationshipRemoveEvent
	*Base
}

undocumented

type SessionsReplaceEvent

type SessionsReplaceEvent struct {
	*gateway.SessionsReplaceEvent
	*Base
}

SessionsReplaceEvent is an undocumented user event. It's likely used for current user's presence updates.

type State

type State struct {
	*state.State
	*EventHandler
	// contains filtered or unexported fields
}

func CloneMocker

func CloneMocker(m *dismock.Mocker, t *testing.T) (*dismock.Mocker, *State)

CloneMocker clones the passed dismock.Mocker and returns a new mocker and mocked State.

func New

func New(token string) (*State, error)

New creates a new State using the passed token. If creating a bot session, the token must start with 'Bot '.

func NewFromSession

func NewFromSession(s *session.Session, cabinet store.Cabinet) (st *State)

NewFromSession creates a new *State from the passed Session. The Session may not be opened.

func NewFromState

func NewFromState(s *state.State) (st *State)

NewFromState creates a new State based on a arikawa State. Event handlers from the old state won't be copied.

func NewMocker

func NewMocker(t *testing.T) (*dismock.Mocker, *State)

NewMocker returns a dismock.Mocker and mocked version of the State.

func NewWithCabinet

func NewWithCabinet(token string, cabinet store.Cabinet) (*State, error)

NewWithCabinet creates a new State with a custom state.Store.

func NewWithIntents

func NewWithIntents(token string, intents ...gateway.Intents) (*State, error)

NewWithIntents creates a new State with the given gateway intents using the passed token. If creating a bot session, the token must start with 'Bot '. For more information, refer to gateway.Intents.

func (*State) AddIntents added in v3.1.0

func (s *State) AddIntents(i gateway.Intents)

AddIntents adds the passed intents to the state.

func (*State) Close

func (s *State) Close() (err error)

Close closes the connection to the gateway and stops listening for events.

func (*State) Open

func (s *State) Open() error

Open opens a connection to the gateway.

func (*State) WithContext

func (s *State) WithContext(ctx context.Context) *State

WithContext returns a shallow copy of State with the context replaced in the API client. All methods called on the State will use this given context. This method is thread-safe.

type UserGuildSettingsUpdateEvent

type UserGuildSettingsUpdateEvent struct {
	*gateway.UserGuildSettingsUpdateEvent
	*Base
}

undocumented

type UserNoteUpdateEvent

type UserNoteUpdateEvent struct {
	*gateway.UserNoteUpdateEvent
	*Base
}

undocumented

type UserSettingsUpdateEvent

type UserSettingsUpdateEvent struct {
	*gateway.UserSettingsUpdateEvent
	*Base
}

undocumented

Jump to

Keyboard shortcuts

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