Documentation ¶
Index ¶
- func GetHandlerName(c *Component, name string) string
- type AssignedEventHandler
- type AssignedEventHandlerAccess
- type Component
- type ComponentHandlerContainer
- func (c *ComponentHandlerContainer) AddDecorator(name string, decorator interface{}) error
- func (c *ComponentHandlerContainer) Register(name string, handler interface{}) (string, error)
- func (c *ComponentHandlerContainer) RegisterOnce(name string, handler interface{}) (string, error)
- func (c *ComponentHandlerContainer) Unregister(name string) error
- type ComponentHandlerManager
- type ComponentLogger
- type LifecycleHooks
- type Logger
- type RegistrableComponent
- type State
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetHandlerName ¶
GetHandlerName returns the name of a handler for a component.
It acts as the auto-formatter that should be used to retrieve handler names.
Types ¶
type AssignedEventHandler ¶
type AssignedEventHandler struct {
// contains filtered or unexported fields
}
AssignedEventHandler holds the necessary data to handle events in the bot. It is used to allow features like decorating handlers by caching the original handler function. The original handler function is then replaced with a proxy function that allows us to add special features.
func GetHandler ¶
func GetHandler(name string) (*AssignedEventHandler, bool)
GetHandler returns a handler by its fully qualified name (id). The required ID can be obtained using GetHandlerName.
func (*AssignedEventHandler) GetComponent ¶
func (ase *AssignedEventHandler) GetComponent() *Component
GetComponent returns the component that owns the handler assigned to the AssignedEventHandler
func (*AssignedEventHandler) GetHandler ¶
func (ase *AssignedEventHandler) GetHandler() interface{}
GetHandler returns the original handler function assigned to the AssignedEventHandler
func (*AssignedEventHandler) GetName ¶
func (ase *AssignedEventHandler) GetName() string
GetName returns the name of the handler assigned to the AssignedEventHandler
type AssignedEventHandlerAccess ¶
type AssignedEventHandlerAccess interface { GetName() string GetComponent() *Component GetHandler() interface{} }
AssignedEventHandlerAccess is an interface that provides access to some fields of AssignedEventHandler through getters.
This ensures those sensitive values don't get overridden.
type Component ¶
type Component struct { // Metadata Name string Description string DmPermission bool // State State State // Lifecycle hooks Lifecycle LifecycleHooks // contains filtered or unexported fields }
Component is the base structure that must be initialized to create a new component.
It holds basic metadata about the component
func (*Component) HandlerManager ¶
func (c *Component) HandlerManager() ComponentHandlerManager
HandlerManager returns the management interface for event handlers.
It allows the registration, decoration and general management of event handlers.
It should be always used when event handlers to listen for Discord events are necessary. It natively handles stuff like logging event handler status.
func (*Component) Logger ¶
Logger is used to obtain the Logger of a component
On first call, this function initializes the private Component.logger field. On consecutive calls, the already present Logger will be used.
func (*Component) RegisterComponent ¶
RegisterComponent is used by the component registration system that automatically calls the RegisterComponent method for all Component instances in the components.Components array.
func (*Component) UnregisterComponent ¶
UnregisterComponent is used by the component registration system that automatically calls the UnregisterComponent method for all Component instances in the components.Components array.
The function takes care of tasks like unregistering slash-commands and so on.
It is used to give components the ability to gracefully shutdown.
type ComponentHandlerContainer ¶
type ComponentHandlerContainer struct {
// contains filtered or unexported fields
}
ComponentHandlerContainer is a helper that eases registration of event handlers while establishing a standard for that task that includes automated logging and adds the ability to allow some weird stuff in the future.
func (*ComponentHandlerContainer) AddDecorator ¶
func (c *ComponentHandlerContainer) AddDecorator(name string, decorator interface{}) error
AddDecorator allows to register a new decorator for an event handler.
Decorators will be called one after another until a decorator does not call a following one (this causes the event handling to be cancelled).
Decorator functions should have the following format:
func ( assignedEvent AssignedEventHandler, originalHandler interface{}, session *discordgo.Session, event interface{} )
Unclear params:
- event must be the event that is handled by the original handler
- originalHandler must be the type of the original handlers function
Note that the name parameter is not the name for the decorator. It is the name of the handler that should be decorated
func (*ComponentHandlerContainer) Register ¶
func (c *ComponentHandlerContainer) Register(name string, handler interface{}) (string, error)
Register can be used to register a new discordgo.AssignedEventHandler.
The passed handler function will be:
- registered in DiscordGo as a handler
- prepared to allow decorations
- saved with a name that allows to retrieve it later on
The passed name for the handler is concatenated with the name of the component that owns the handler (separated by underscore).
The handler must have the same format as when a handler is registered in plain DiscordGo. See the documentation about discordgo.AddHandler for additional information.
In general, the common format for a handler function is:
func (session *discordgo.Session, event <event to call, e.g. discordgo.MessageCreate)
func (*ComponentHandlerContainer) RegisterOnce ¶
func (c *ComponentHandlerContainer) RegisterOnce( name string, handler interface{}, ) (string, error)
RegisterOnce registers an event handler as a one-time event handler. The registered handler will be removed after being triggered once.
The passed handler function will be:
- registered in DiscordGo as a handler
- prepared to allow decorations
- saved with a name that allows to retrieve it later on
The passed name for the handler is concatenated with the name of the component that owns the handler (separated by underscore).
The handler must have the same format as when a handler is registered in plain DiscordGo. See the documentation about discordgo.AddHandler for additional information.
In general, the common format for a handler function is:
func (session *discordgo.Session, event <event to call, e.g. discordgo.MessageCreate)
func (*ComponentHandlerContainer) Unregister ¶
func (c *ComponentHandlerContainer) Unregister(name string) error
Unregister removes the handler with the given name (if existing) from the registered handlers.
If the specified handler does not exist, an error will be returned.
type ComponentHandlerManager ¶
type ComponentHandlerManager interface { // Register can be used to register a new discordgo.AssignedEventHandler. // // The passed handler function will be: // 1. registered in DiscordGo as a handler // 2. prepared to allow decorations // 3. saved with a name that allows to retrieve it later on // // The passed name for the handler is concatenated with the name of the // component that owns the handler (separated by underscore). // // The handler must have the same format as when a handler is registered in // plain DiscordGo. See the documentation about discordgo.AddHandler // for additional information. // // In general, the common format for a handler function is: // func (session *discordgo.Session, event <event to call, e.g. discordgo.MessageCreate) Register(name string, handler interface{}) (string, error) // RegisterOnce registers an event handler as a one-time event handler. // The registered handler will be removed after being triggered once. // // // The passed handler function will be: // 1. registered in DiscordGo as a handler // 2. prepared to allow decorations // 3. saved with a name that allows to retrieve it later on // // The passed name for the handler is concatenated with the name of the // component that owns the handler (separated by underscore). // // The handler must have the same format as when a handler is registered in // plain DiscordGo. See the documentation about discordgo.AddHandler // for additional information. // // In general, the common format for a handler function is: // func (session *discordgo.Session, event <event to call, e.g. discordgo.MessageCreate) RegisterOnce(name string, handler interface{}) (string, error) // Unregister removes the handler with the given name (if existing) from // the registered handlers. // // If the specified handler does not exist, an error will be returned. Unregister(name string) error // AddDecorator allows to register a new decorator for an event // handler. // // Decorators will be called one after another until a decorator does not call // a following one (this causes the event handling to be cancelled). // // Decorator functions should have the following format: // func ( // assignedEvent AssignedEventHandler, // originalHandler interface{}, // session *discordgo.Session, // event interface{} // ) // // Unclear params: // - event must be the event that is handled by the original handler // - originalHandler must be the type of the original handlers function AddDecorator(name string, decorator interface{}) error // contains filtered or unexported methods }
ComponentHandlerManager is the interface that defines all methods for event management.
The functions of the interface allow registration, decoration and removal of Discord event handlers.
Under the hood, DiscordGo event handlers are used.
type ComponentLogger ¶
type ComponentLogger struct {
// contains filtered or unexported fields
}
ComponentLogger is a type that is used to hold the owner that keeps the information about the component used by the logging methods.
func (*ComponentLogger) Debug ¶
func (c *ComponentLogger) Debug(format string, v ...interface{})
Debug logs a message with level debug. This function appends the name of the Component from the receiver to the log message.
func (*ComponentLogger) Err ¶
func (c *ComponentLogger) Err(err error, format string, v ...interface{})
Err logs a message with level error. This function appends the name of the Component from the receiver to the log message.
The supplied error will be applied to the log message.
func (*ComponentLogger) Info ¶
func (c *ComponentLogger) Info(format string, v ...interface{})
Info logs a message with level info. This function appends the name of the Component from the receiver to the log message.
func (*ComponentLogger) Warn ¶
func (c *ComponentLogger) Warn(format string, v ...interface{})
Warn logs a message with level warnings. This function appends the name of the Component from the receiver to the log message.
type LifecycleHooks ¶
type LifecycleHooks struct { LoadComponent func(discord *discordgo.Session) error UnloadComponent func(discord *discordgo.Session) error }
LifecycleHooks allow to specify functions that should be called when components get loaded and unloaded.
The defined functions allow some way of initializing a component.
type Logger ¶
type Logger interface { Debug(format string, v ...interface{}) Info(format string, v ...interface{}) Warn(format string, v ...interface{}) Err(err error, format string, v ...interface{}) }
Logger provides useful methods that ease logging.