state

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2020 License: MIT Imports: 12 Imported by: 0

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("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 AddGlobalMiddleware and
	// MustAddGlobalMiddleware if the middleware func is invalid.
	ErrInvalidMiddleware = errors.New("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")
)

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 string) (val interface{})

Get gets the element with the passed key.

func (*Base) Lookup

func (b *Base) Lookup(key string) (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 string, 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) AddGlobalMiddleware

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

AddGlobalMiddleware adds the passed middleware as a global middleware.

The scheme 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) AddHandler

func (h *EventHandler) AddHandler(f interface{}, middlewares ...interface{}) (func(), error)

AddHandler adds a handlers with the passed globalMiddlewares to the event handlers.

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

The scheme 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.

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()

func (*EventHandler) MustAddGlobalMiddleware

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

MustAddGlobalMiddleware is the same as AddGlobalMiddleware but panics if AddGlobalMiddleware returns an error.

func (*EventHandler) MustAddHandler

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

MustAddHandler is the same as AddHandler, but panics if AddHandler 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

	Ready *gateway.ReadyEvent

	StateLog func(error)
	// contains filtered or unexported fields
}

func CloneMocker

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

CloneMocker clones the passed mocker and returns a new dismock.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, store state.Store) (st *State)

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

func NewFromState added in v2.1.0

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 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 NewWithStore

func NewWithStore(token string, store state.Store) (*State, error)

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

func (*State) AuthorDisplayName

func (s *State) AuthorDisplayName(message *gateway.MessageCreateEvent) string

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