event

package
v4.0.0-...-412cf09 Latest Latest
Warning

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

Go to latest
Published: Nov 7, 2021 License: MIT Imports: 7 Imported by: 5

Documentation

Overview

Package event provides wrappers around arikawa's events as well as a custom event handler.

Event Handler

disstate's event handler expands the functionality of arikawa's built-in handler system. It adds support for middlewares both on a global level and a per-handler level.

Furthermore, *state.State is the first parameter of all events, to allow access to the state without wrapping functions. Optionally, handler functions may also return an error that is handled by the event handler's ErrorHandler function. Similarly, the event handler will also recover from panics and handle them using the event handler's PanicHandler function.

Event Types

Wrapped events have two key differences in comparison to their arikawa counterparts.

Firstly, they embed a *Base that contains a key-value-store similar to that of context.Context. This allows to share state between middlewares and handlers.

Secondly, some events have an Old field, containing the state of event's entity prior to when the event was received. The Old field will obviously only be filled, if the enitity was previously cached. This functionality replaces arikawa's PreHandler system and allows handlers to work with both the previous and the current entity, something impossible with arikawa's system.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidHandler gets returned if a handler given to
	// Handler.AddHandler or Handler.MustAddHandler is not a valid handler func.
	ErrInvalidHandler = errors.New("state: the passed interface{} does not resemble a valid handler")
	// ErrInvalidMiddleware gets returned if a middleware given to
	// Handler.AddHandler or Handler.MustAddHandler 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:revive
)

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 ChannelCreate

type ChannelCreate struct {
	*Base
	*gateway.ChannelCreateEvent
}

type ChannelDelete

type ChannelDelete struct {
	*Base
	*gateway.ChannelDeleteEvent

	Old *discord.Channel
}

type ChannelPinsUpdate

type ChannelPinsUpdate struct {
	*Base
	*gateway.ChannelPinsUpdateEvent
}

type ChannelUnreadUpdate

type ChannelUnreadUpdate struct {
	*Base
	*gateway.ChannelUnreadUpdateEvent
}

type ChannelUpdate

type ChannelUpdate struct {
	*Base
	*gateway.ChannelUpdateEvent

	Old *discord.Channel
}

type GuildAvailable

type GuildAvailable struct {
	*GuildCreate
}

GuildAvailable 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 GuildBanAdd

type GuildBanAdd struct {
	*Base
	*gateway.GuildBanAddEvent
}

type GuildBanRemove

type GuildBanRemove struct {
	*Base
	*gateway.GuildBanRemoveEvent
}

type GuildCreate

type GuildCreate struct {
	*Base
	*gateway.GuildCreateEvent
}

type GuildDelete

type GuildDelete struct {
	*Base
	*gateway.GuildDeleteEvent

	Old *discord.Guild
}

type GuildEmojisUpdate

type GuildEmojisUpdate struct {
	*Base
	*gateway.GuildEmojisUpdateEvent
}

type GuildIntegrationsUpdate

type GuildIntegrationsUpdate struct {
	*Base
	*gateway.GuildIntegrationsUpdateEvent
}

type GuildJoin

type GuildJoin struct {
	*GuildCreate
}

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

type GuildLeave

type GuildLeave struct {
	*GuildDelete
}

GuildLeave 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 GuildMemberAdd

type GuildMemberAdd struct {
	*Base
	*gateway.GuildMemberAddEvent
}

type GuildMemberListUpdate

type GuildMemberListUpdate struct {
	*Base
	*gateway.GuildMemberListUpdate
}

type GuildMemberRemove

type GuildMemberRemove struct {
	*Base
	*gateway.GuildMemberRemoveEvent

	Old *discord.Member
}

type GuildMemberUpdate

type GuildMemberUpdate struct {
	*Base
	*gateway.GuildMemberUpdateEvent

	Old *discord.Member
}

type GuildMembersChunk

type GuildMembersChunk struct {
	*Base
	*gateway.GuildMembersChunkEvent
}

type GuildReady

type GuildReady struct {
	*GuildCreate
}

GuildReady 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 GuildRoleCreate

type GuildRoleCreate struct {
	*Base
	*gateway.GuildRoleCreateEvent
}

type GuildRoleDelete

type GuildRoleDelete struct {
	*Base
	*gateway.GuildRoleDeleteEvent

	Old *discord.Role
}

type GuildRoleUpdate

type GuildRoleUpdate struct {
	*Base
	*gateway.GuildRoleUpdateEvent

	Old *discord.Role
}

type GuildUnavailable

type GuildUnavailable struct {
	*GuildDelete
}

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

type GuildUpdate

type GuildUpdate struct {
	*Base
	*gateway.GuildUpdateEvent

	Old *discord.Guild
}

type Handler

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

Handler is the event handler.

For a detailed list of differences of arikawa and disstate's event handling refer to the package doc.

func NewHandler

func NewHandler(stateVal reflect.Value) *Handler

NewHandler creates a new Handler using the passed reflect.Value of the *state.State. It panics if the reflect.Value is not of the correct type.

func (*Handler) AddHandler

func (h *Handler) AddHandler(handler interface{}, middlewares ...interface{}) func()

AddHandler adds a handler with the passed middlewares to the event handler.

The handler can either be a channel of pointer to an event type, or a function. Note that the event handler will not wait until the channel is ready to receive. Instead you must ensure, that your channel is sufficiently buffered or you are ready to receive. If you require otherwise, consider add a handler function that send to your channel blockingly.

If using a function as handler, the function must match func(*State, e), where e is either a pointer to an event, *Base, or interface{}. Optionally, a handler function may return an error that will be handled by Handler.ErrorHandler, if non-nil.

The same requirements as for functions apply to middlewares as well.

func (*Handler) AddHandlerOnce

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

AddHandlerOnce is the same as AddHandler, but only handles a single event before removing itself.

func (*Handler) AddMiddleware

func (h *Handler) AddMiddleware(f interface{})

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 (*Handler) AutoAddHandlers

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

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

func (*Handler) Call

func (h *Handler) 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 (*Handler) Close

func (h *Handler) Close(ctx context.Context) error

Close signals the event listener to stop and blocks until all handlers have finished their execution.

If the context expires before all handlers finished executing, Close will simply return. Running handlers will not be stopped.

func (*Handler) DeriveIntents

func (h *Handler) 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 (*Handler) Open

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

Open listens to events on the passed channel until Close is called.

func (*Handler) TryAddHandler

func (h *Handler) TryAddHandler(handler interface{}, middlewares ...interface{}) (rm func(), err error)

TryAddHandler is the same as AddHandler, but returns an error if the signature of the handler or one of the middlewares is invalid.

func (*Handler) TryAddHandlerOnce

func (h *Handler) TryAddHandlerOnce(handler interface{}, middlewares ...interface{}) error

TryAddHandlerOnce is the same as AddHandlerOnce, but returns an error if the signature of the handler of of one of the middlewares is invalid.

func (*Handler) TryAddMiddleware

func (h *Handler) TryAddMiddleware(f interface{}) error

TryAddMiddleware is the same as AddMiddleware, but returns an error if the signature of the middleware is invalid.

type Hello

type Hello struct {
	*Base
	*gateway.HelloEvent
}

type InteractionCreate

type InteractionCreate struct {
	*Base
	*gateway.InteractionCreateEvent
}

type InvalidSession

type InvalidSession struct {
	*Base
	*gateway.InvalidSessionEvent
}

type InviteCreate

type InviteCreate struct {
	*Base
	*gateway.InviteCreateEvent
}

type InviteDelete

type InviteDelete struct {
	*Base
	*gateway.InviteDeleteEvent
}

type MessageAck

type MessageAck struct {
	*Base
	*gateway.MessageAckEvent
}

type MessageCreate

type MessageCreate struct {
	*Base
	*gateway.MessageCreateEvent
}

type MessageDelete

type MessageDelete struct {
	*Base
	*gateway.MessageDeleteEvent

	Old *discord.Message
}

type MessageDeleteBulk

type MessageDeleteBulk struct {
	*Base
	*gateway.MessageDeleteBulkEvent
}

type MessageReactionAdd

type MessageReactionAdd struct {
	*Base
	*gateway.MessageReactionAddEvent
}

type MessageReactionRemove

type MessageReactionRemove struct {
	*Base
	*gateway.MessageReactionRemoveEvent
}

type MessageReactionRemoveAll

type MessageReactionRemoveAll struct {
	*Base
	*gateway.MessageReactionRemoveAllEvent
}

type MessageReactionRemoveEmoji

type MessageReactionRemoveEmoji struct {
	*Base
	*gateway.MessageReactionRemoveEmojiEvent
}

type MessageUpdate

type MessageUpdate struct {
	*Base
	*gateway.MessageUpdateEvent

	Old *discord.Message
}

type PresenceUpdate

type PresenceUpdate struct {
	*Base
	*gateway.PresenceUpdateEvent

	Old *discord.Presence
}

type PresencesReplace

type PresencesReplace struct {
	*Base
	*gateway.PresencesReplaceEvent
}

type Ready

type Ready struct {
	*Base
	*gateway.ReadyEvent
}

type ReadySupplemental

type ReadySupplemental struct {
	*Base
	*gateway.ReadySupplementalEvent
}

type RelationshipAdd

type RelationshipAdd struct {
	*Base
	*gateway.RelationshipAddEvent
}

type RelationshipRemove

type RelationshipRemove struct {
	*Base
	*gateway.RelationshipRemoveEvent
}

type Resumed

type Resumed struct {
	*Base
	*gateway.ResumedEvent
}

type SessionsReplace

type SessionsReplace struct {
	*Base
	*gateway.SessionsReplaceEvent
}

type TypingStart

type TypingStart struct {
	*Base
	*gateway.TypingStartEvent
}

type UserGuildSettingsUpdate

type UserGuildSettingsUpdate struct {
	*Base
	*gateway.UserGuildSettingsUpdateEvent
}

type UserNoteUpdate

type UserNoteUpdate struct {
	*Base
	*gateway.UserNoteUpdateEvent
}

type UserSettingsUpdate

type UserSettingsUpdate struct {
	*Base
	*gateway.UserSettingsUpdateEvent
}

type UserUpdate

type UserUpdate struct {
	*Base
	*gateway.UserUpdateEvent
}

type VoiceServerUpdate

type VoiceServerUpdate struct {
	*Base
	*gateway.VoiceServerUpdateEvent
}

type VoiceStateUpdate

type VoiceStateUpdate struct {
	*Base
	*gateway.VoiceStateUpdateEvent
}

type WebhooksUpdate

type WebhooksUpdate struct {
	*Base
	*gateway.WebhooksUpdateEvent
}

Jump to

Keyboard shortcuts

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