Documentation
¶
Index ¶
- Constants
- Variables
- func DeinitCommandHandling()
- func GetHandlerName(c *Component, name string) string
- func Init(em EntityManager) error
- func InitCommandHandling(session *discordgo.Session) error
- func IsComponentEnabled(comp *Component, guildId string) bool
- func IsCoreComponent(c *Component) bool
- func ProcessSubCommands(s *discordgo.Session, i *discordgo.InteractionCreate, ...) bool
- func RegisterComponent(component *Component, ...)
- type AssignedEventHandler
- type AssignedEventHandlerAccess
- type Command
- type CommonSlashCommandManager
- type CompoMessageHandlerManager
- type Component
- func (c *Component) EntityManager() *EntityManager
- func (c *Component) HandlerManager() ComponentHandlerManager
- func (c *Component) LoadComponent(discord *discordgo.Session) error
- func (c *Component) Logger() services.Logger
- func (c *Component) SetLogger(l services.Logger)
- func (c *Component) SlashCommandManager() *SlashCommandManager
- func (c *Component) UnloadComponent(*discordgo.Session) error
- type ComponentHandlerContainer
- func (c *ComponentHandlerContainer) AddDecorator(name string, decorator interface{}) bool
- func (c *ComponentHandlerContainer) Register(name string, handler interface{}) (string, bool)
- func (c *ComponentHandlerContainer) RegisterComplexMessageHandler(name string, ...) (string, bool)
- func (c *ComponentHandlerContainer) RegisterOnce(name string, handler interface{}) (string, bool)
- func (c *ComponentHandlerContainer) RegisterSimpleMessageHandler(name string, ...) (string, bool)
- func (c *ComponentHandlerContainer) Unregister(name string) error
- func (c *ComponentHandlerContainer) UnregisterAll()
- type ComponentHandlerManager
- type DiscordSession
- type EntityManager
- func (em *EntityManager) DB() services.DatabaseAccess
- func (em *EntityManager) GlobalComponentStatus() GlobalComponentStatusEntityManager
- func (em *EntityManager) GuildComponentStatus() GuildComponentStatusEntityManager
- func (em *EntityManager) Guilds() GuildEntityManager
- func (em *EntityManager) Logger() services.Logger
- func (em *EntityManager) RegisterDefaultEntities() error
- func (em *EntityManager) RegisterEntity(entityType interface{}) error
- func (em *EntityManager) RegisteredComponent() RegisteredComponentEntityManager
- type GlobalComponentStatusEntityManager
- type GuildComponentStatusEntityManager
- type GuildEntityManager
- type RegisteredComponentEntityManager
- type RegistrableComponent
- type ServiceManager
- type SlashCommandManager
- type State
Constants ¶
const CoreComponentPrefix = "bot_"
CoreComponentPrefix is the prefix put in front of components that cannot be managed by server owners, as they are important core components
const DefaultEmbedColor = 0x5D397C
DefaultEmbedColor is the default color that should be used for embeds send by the bot
Variables ¶
var Components = make([]*Component, 0)
Components holds all available components that have been registered.
The component code is used as key. The code of a component is unique, registering multiple components with the same code results in overriding a previously registered one.
Functions ¶
func DeinitCommandHandling ¶ added in v0.0.3
func DeinitCommandHandling()
DeinitCommandHandling unregisters the event Handler that is registered by InitCommandHandling.
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.
func Init ¶ added in v0.0.3
func Init(em EntityManager) error
Init initializes the api.
This covers initializing the following parts:
- database api
- entity management
func InitCommandHandling ¶ added in v0.0.3
InitCommandHandling initializes the command handling by registering the necessary event Handler.
func IsComponentEnabled ¶ added in v0.0.3
IsComponentEnabled checks if a specific component is currently enabled for a specific guild.
func IsCoreComponent ¶ added in v0.0.3
IsCoreComponent checks whether the passed component is a core component or not.
Core components are components which are prefixed with the CoreComponentPrefix.
func ProcessSubCommands ¶ added in v0.0.3
func ProcessSubCommands( s *discordgo.Session, i *discordgo.InteractionCreate, option *discordgo.ApplicationCommandInteractionDataOption, handlers map[string]func( s *discordgo.Session, i *discordgo.InteractionCreate, option *discordgo.ApplicationCommandInteractionDataOption, ), ) bool
ProcessSubCommands is an easy way to handle sub-commands and sub-command-groups. The function will return true if there was a valid sub-command to handle
func RegisterComponent ¶ added in v0.0.3
func RegisterComponent(component *Component, loadComponentFunction func(session *discordgo.Session) error)
RegisterComponent adds the given Component to the list of registered components.
This function should only being called in the init functions of components. To get the application to automatically call this function, add an import to the <repo-root>/components/component_registry.go.
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 Command ¶ added in v0.0.3
type Command struct { Cmd *discordgo.ApplicationCommand Handler func(s *discordgo.Session, i *discordgo.InteractionCreate) // contains filtered or unexported fields }
Command is a struct that acts as a container for discordgo.ApplicationCommand and the assigned command Handler.
Create an instance of the struct and pass to Register a command
type CommonSlashCommandManager ¶ added in v0.0.3
type CommonSlashCommandManager interface { // Register allows to register a command // // It requires a Command to be passed. // The Command holds the common discordgo.ApplicationCommand // and the function that should handle the command. Register(cmd *Command) error // SyncApplicationComponentCommands ensures that the available discordgo.ApplicationCommand // are synced for the given component with the given guild. // // This means that disabled commands are enabled and enabled commands are disabled // depending on the component enable state. // // Also orphaned commands are cleaned up. // This is executed whenever a guild is joined or a component is toggled. SyncApplicationComponentCommands(session *discordgo.Session, guildId string) // GetCommandCount returns the number of registered slash commands GetCommandCount() int }
CommonSlashCommandManager provides a standardized interface how slash commands should be created and registered in the application
type CompoMessageHandlerManager ¶ added in v0.0.3
type CompoMessageHandlerManager interface { // RegisterSimpleMessageHandler can be used to register a new AssignedEventHandler to handle messages. // // In general, the function just works like the common Register method of the API, // except that strings can be passed as third argument. // The created event handler will be decorated and only trigger if the received message // matches one of the specified strings. // // Note that handlers registered using this function ignore messages from bots // and messages that the bot itself send. // // 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). RegisterSimpleMessageHandler( name string, handler func(session *discordgo.Session, create *discordgo.MessageCreate), messages ...string, ) (string, bool) // RegisterComplexMessageHandler can be used to register a new AssignedEventHandler to handle messages. // // In general, the function just works like the common Register method of the API, // except that strings can be passed as third argument. // The created event handler will be decorated and only trigger if the received message // matches one of the specified strings. // // The passed messages should be regex patterns. What makes RegisterComplexMessageHandler special // is the fact that it allows to match messages with Regex. // // Note that handlers registered using this function ignore messages from bots // and messages that the bot itself send. // // 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 returned values contain the name of the (potentially registered) handler // and a bool indicating whether the registration was successful or not. RegisterComplexMessageHandler( name string, handler func(session *discordgo.Session, create *discordgo.MessageCreate), messages ...string, ) (string, bool) }
CompoMessageHandlerManager is an interface that provides functions to register specialized event handlers for message handling.
type Component ¶
type Component struct { // Metadata Code string Name string Description string LoadPriority int // State State *State // 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) EntityManager ¶ added in v0.0.3
func (c *Component) EntityManager() *EntityManager
EntityManager returns the currently active EntityManager. The currently active EntityManager is shared across all components.
The entities.DatabaseAccess allows to interact with the entities of the application or access the raw gorm.DB instance, which is used for db access.
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) LoadComponent ¶ added in v0.0.3
LoadComponent is used by the component registration system that automatically calls the LoadComponent method for all Component instances in the components.Components array.
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) SetLogger ¶ added in v0.0.3
SetLogger allows to set a custom logger for the component
func (*Component) SlashCommandManager ¶ added in v0.0.3
func (c *Component) SlashCommandManager() *SlashCommandManager
SlashCommandManager is used to obtain the components slash Command management
On first call, this function initializes the private Component.slashCommandManager field. On consecutive calls, the already present CommonSlashCommandManager will be used.
func (*Component) UnloadComponent ¶ added in v0.0.3
UnloadComponent 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{}) bool
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, bool)
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) RegisterComplexMessageHandler ¶ added in v0.0.3
func (c *ComponentHandlerContainer) RegisterComplexMessageHandler( name string, handler func(session *discordgo.Session, create *discordgo.MessageCreate), patterns ...string, ) (string, bool)
RegisterComplexMessageHandler can be used to register a new AssignedEventHandler to handle messages.
In general, the function just works like the common Register method of the API, except that strings can be passed as third argument. The created event handler will be decorated and only trigger if the received message matches one of the specified strings.
The passed messages should be regex patterns. What makes RegisterComplexMessageHandler special is the fact that it allows to match messages with Regex.
Note that handlers registered using this function ignore messages from bots and messages that the bot itself send.
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 returned values contain the name of the (potentially registered) handler and a bool indicating whether the registration was successful or not.
func (*ComponentHandlerContainer) RegisterOnce ¶
func (c *ComponentHandlerContainer) RegisterOnce( name string, handler interface{}, ) (string, bool)
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) RegisterSimpleMessageHandler ¶ added in v0.0.3
func (c *ComponentHandlerContainer) RegisterSimpleMessageHandler( name string, handler func(session *discordgo.Session, create *discordgo.MessageCreate), messages ...string, ) (string, bool)
RegisterSimpleMessageHandler can be used to register a new AssignedEventHandler to handle messages.
In general, the function just works like the common Register method of the API, except that strings can be passed as third argument. The created event handler will be decorated and only trigger if the received message matches one of the specified strings.
Note that handlers registered using this function ignore messages from bots and messages that the bot itself send.
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 returned values contain the name of the (potentially registered) handler and a bool indicating whether the registration was successful or not.
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.
func (*ComponentHandlerContainer) UnregisterAll ¶ added in v0.0.3
func (c *ComponentHandlerContainer) UnregisterAll()
UnregisterAll takes care of unregistering all handlers attached to the component that owns the ComponentHandlerContainer
type ComponentHandlerManager ¶
type ComponentHandlerManager interface { CompoMessageHandlerManager // Register can be used to register a new 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, bool) // 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, bool) // 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{}) bool UnregisterAll() }
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 DiscordSession ¶ added in v0.0.3
type DiscordSession interface {
GuildApplicationCommandsPermissions(appID, guildID string) (permissions []*discordgo.GuildApplicationCommandPermissions, err error)
}
type EntityManager ¶ added in v0.0.3
type EntityManager struct {
// contains filtered or unexported fields
}
EntityManager is a struct embedded by GormDatabaseAccessor that holds the instances of the entity specific entity managers
func GetEntityManager ¶ added in v0.0.3
func GetEntityManager() *EntityManager
GetEntityManager returns the currently active EntityManager. The currently active EntityManager is shared across all components.
The entities.DatabaseAccess allows to interact with the entities of the application or access the raw gorm.DB instance, which is used for db access.
func NewEntityManager ¶ added in v0.0.3
func NewEntityManager(database services.DatabaseAccess, logger services.Logger) EntityManager
NewEntityManager creates a new EntityManager using the given services.Logger and services.DatabaseAccess instances.
func (*EntityManager) DB ¶ added in v0.0.3
func (em *EntityManager) DB() services.DatabaseAccess
DB returns the gorm.DB instance used internally by the EntityManager and services.DatabaseAccess.
func (*EntityManager) GlobalComponentStatus ¶ added in v0.0.3
func (em *EntityManager) GlobalComponentStatus() GlobalComponentStatusEntityManager
GlobalComponentStatus returns the GlobalComponentStatusEntityManager that is currently active, which can be used to do GlobalComponentStatus specific entities actions.
func (*EntityManager) GuildComponentStatus ¶ added in v0.0.3
func (em *EntityManager) GuildComponentStatus() GuildComponentStatusEntityManager
GuildComponentStatus returns the GuildComponentStatusEntityManager that is currently active, which can be used to do GuildComponentStatus specific entities actions.
func (*EntityManager) Guilds ¶ added in v0.0.3
func (em *EntityManager) Guilds() GuildEntityManager
Guilds returns the GuildEntityManager that is currently active, which can be used to do Guild specific entities actions.
func (*EntityManager) Logger ¶ added in v0.0.3
func (em *EntityManager) Logger() services.Logger
Logger returns the services.Logger of the EntityManager.
func (*EntityManager) RegisterDefaultEntities ¶ added in v0.0.3
func (em *EntityManager) RegisterDefaultEntities() error
RegisterDefaultEntities takes care of letting gorm know about all entities in this file.
func (*EntityManager) RegisterEntity ¶ added in v0.0.3
func (em *EntityManager) RegisterEntity(entityType interface{}) error
RegisterEntity registers a new entity (struct) and runs its automated migration to ensure the entities schema is up-to-date.
func (*EntityManager) RegisteredComponent ¶ added in v0.0.3
func (em *EntityManager) RegisteredComponent() RegisteredComponentEntityManager
RegisteredComponent returns the RegisteredComponentEntityManager that is currently active, which can be used to do RegisteredComponent specific entities actions.
type GlobalComponentStatusEntityManager ¶ added in v0.0.3
type GlobalComponentStatusEntityManager interface { // Get tries to get a GlobalComponentStatus from the // cache. If no cache entry is present, a request to the db will be made. // If no GlobalComponentStatus can be found, the function returns a new empty // GlobalComponentStatus. Get(globalComponentStatusId uint) (*entities.GlobalComponentStatus, error) // GetDisplayString returns the string that indicates whether a component is // enabled or disabled globally. The string can directly being used to print // out messages in Discord. GetDisplayString(globalComponentStatusId uint) (string, error) // Create saves the passed GlobalComponentStatus in the db. // Use Update or Save to update an already existing GlobalComponentStatus. Create(globalComponentStatus *entities.GlobalComponentStatus) error // Save updates the passed GlobalComponentStatus in the db. // This does a generic update, use Update to do a precise and more performant update // of the entity when only updating a single field! Save(globalComponentStatus *entities.GlobalComponentStatus) error // Update updates the defined field on the entity and saves it in the db. Update(globalComponentStatus *entities.GlobalComponentStatus, column string, value interface{}) error }
GlobalComponentStatusEntityManager is an entity manager that provides functionality for entities.GlobalComponentStatus CRUD operations.
type GuildComponentStatusEntityManager ¶ added in v0.0.3
type GuildComponentStatusEntityManager interface { // Get tries to get a GuildComponentStatus from the // cache. If no cache entry is present, a request to the entities will be made. // If no GuildComponentStatus can be found, the function returns a new empty // GuildComponentStatus. Get(guildId uint, componentId uint) (*entities.GuildComponentStatus, error) // GetDisplay returns the status of a component in a form // that can be directly displayed in Discord. GetDisplay(guildId uint, componentId uint) (string, error) // Create saves the passed Guild in the db. // Use Update or Save to update an already existing Guild. Create(guildComponentStatus *entities.GuildComponentStatus) error // Save updates the passed Guild in the db. // This does a generic update, use Update to do a precise and more performant update // of the entity when only updating a single field! Save(guildComponentStatus *entities.GuildComponentStatus) error // Update updates the defined field on the entity and saves it in the db. Update(component *entities.GuildComponentStatus, column string, value interface{}) error }
GuildComponentStatusEntityManager is an entity manager that provides functionality for entities.GuildComponentStatus CRUD operations.
type GuildEntityManager ¶ added in v0.0.3
type GuildEntityManager interface { // Get tries to get a Guild from the // cache. If no cache entry is present, a request to the db will be made. // If no Guild can be found, the function returns a new empty // Guild. Get(guildId string) (*entities.Guild, error) // Count returns the number of all guilds stored in the db Count() (int64, error) // Create saves the passed Guild in the db. // Use Update or Save to update an already existing Guild. Create(guild *entities.Guild) error // Save updates the passed Guild in the db. // This does a generic update, use Update to do a precise and more performant update // of the entity when only updating a single field! Save(guild *entities.Guild) error // Update updates the defined field on the entity and saves it in the db. Update(guild *entities.Guild, column string, value interface{}) error }
GuildEntityManager is an entity manager that provides functionality for entities.Guild CRUD operations.
type RegisteredComponentEntityManager ¶ added in v0.0.3
type RegisteredComponentEntityManager interface { // Get tries to get a RegisteredComponent from the // cache. If no cache entry is present, a request to the entities will be made. // If no RegisteredComponent can be found, the function returns a new empty // RegisteredComponent. Get(registeredComponentCode string) (*entities.RegisteredComponent, error) // GetAvailable returns all components that have been registered // during application bootstrap. GetAvailable() []*entities.RegisteredComponent // Create saves the passed RegisteredComponent in the db. // Use Update or Save to update an already existing RegisteredComponent. Create(regComp *entities.RegisteredComponent) error // Save updates the passed RegisteredComponent in the db. // This does a generic update, use Update to do a precise and more performant update // of the entity when only updating a single field! Save(regComp *entities.RegisteredComponent) error // Update updates the defined field on the entity and saves it in the db. Update(regComp *entities.RegisteredComponent, column string, value interface{}) error // MarkAsAvailable marks the passed component as available, by putting // the codes into an array. // Note that duplicates will be filtered. MarkAsAvailable(code string) }
RegisteredComponentEntityManager is an entity manager that provides functionality for entities.RegisteredComponent CRUD operations.
type RegistrableComponent ¶
type RegistrableComponent interface { LoadComponent(discord *discordgo.Session) error UnloadComponent(discord *discordgo.Session) error }
RegistrableComponent is the interface that allows a component to be initialized and registered.
type ServiceManager ¶ added in v0.0.3
type ServiceManager interface { // 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. Logger() services.Logger // 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. HandlerManager() ComponentHandlerManager // SlashCommandManager is used to obtain the components slash Command management // // On first call, this function initializes the private Component.slashCommandManager // field. On consecutive calls, the already present CommonSlashCommandManager will be used. SlashCommandManager() CommonSlashCommandManager // DatabaseAccess returns the currently active DatabaseAccess. // The currently active DatabaseAccess is shared across all components. // // The DatabaseAccess allows to interact with the entities of the application. // Prefer using the EntityManager instead, as DatabaseAccess is considered // a low-level api. DatabaseAccess() *EntityManager }
ServiceManager is a simple interface that defines the methods that provide the APIs features, like Logger.
Although it is most of the time not best practice, the API returns interfaces. We decided to use this design as interfaces are great to offer an API where internal things or complete subsystems can be swapped without breaking components. They act as contracts in this application. This allows us to maintain a separate logger implementation in the `services/logger` that is compatible with this API. So, when using the API package, think of using contracts.
tl;dr: everything from the services package is low level and should not be used directly when possible (although it is not prohibited). The api package is the consumer of the services package and implements interfaces. These interfaces are provided to components. Because of that, we decided to return interfaces instead of structs in the API.
type SlashCommandManager ¶ added in v0.0.3
type SlashCommandManager struct {
// contains filtered or unexported fields
}
SlashCommandManager is a type that is used to hold the owner that keeps the information about the component used by the slash Command manager methods.
func (*SlashCommandManager) GetCommandCount ¶ added in v0.0.3
func (c *SlashCommandManager) GetCommandCount() int
GetCommandCount returns the number of registered slash commands
func (*SlashCommandManager) Register ¶ added in v0.0.3
func (c *SlashCommandManager) Register(cmd *Command) error
Register allows to register a command
It requires a Command to be passed. The Command holds the common discordgo.ApplicationCommand and the function that should handle the command.
func (*SlashCommandManager) SyncApplicationComponentCommands ¶ added in v0.0.3
func (c *SlashCommandManager) SyncApplicationComponentCommands( session *discordgo.Session, guildId string, )
SyncApplicationComponentCommands ensures that the available discordgo.ApplicationCommand are synced for the given component with the given guild.
This means that disabled commands are enabled and enabled commands are disabled depending on the component enable state.
Also orphaned commands are cleaned up. This is executed whenever a guild is joined or a component is toggled.
Sync is a four-step process:
- remove orphaned commands
- remove disabled commands
- add new commands
- update existing commands
type State ¶
type State struct { // Loaded is used to determine if the component // has been loaded properly or not. Loaded bool // DefaultEnabled is the default status to set for the component // in the database when the bot joins a new guild. DefaultEnabled bool }
State holds the state of a component. This includes states like:
- is the component enabled?
- is the component currently loaded?