api

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: May 18, 2023 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	CategoryInternal       = "Internal"
	CategoryAdministration = "Administration"
	CategoryFun            = "Fun"
	CategoryUtilities      = "Utilities"
)
View Source
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

View Source
const DefaultEmbedColor = 0x5D397C

DefaultEmbedColor is the default color that should be used for embeds send by the bot

Variables

View Source
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 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

func InitCommandHandling(session *discordgo.Session) error

InitCommandHandling initializes the command handling by registering the necessary event Handler.

func IsComponentEnabled added in v0.0.3

func IsComponentEnabled(comp *Component, guildId string) bool

IsComponentEnabled checks if a specific component is currently enabled for a specific guild. If the guild id is empty, the function will return the global status of the component.

func IsCoreComponent added in v0.0.3

func IsCoreComponent(c *Component) bool

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

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. The originalHandler should only be set during creation of the handler. The handler can be swapped with some new function that wraps the original function. This allows to add custom logic like the component status checks.

Important: The handler can only being wrapped before it is registered in DiscordGo. Wrappers are meant for system features of the bot like component status checks or permissions. Those features are injected during the call of the ComponentHandlerContainer.Register and ComponentHandlerContainer.RegisterOnce methods.

func GetHandler

func GetHandler(name HandlerName) (*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() HandlerName

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 AuditLogConfigEntityManager added in v0.2.0

type AuditLogConfigEntityManager interface {
	// GetByGuildId tries to get a AuditLogConfig by its guild ID.
	// The function uses a cache and first tries to resolve a value from it.
	// If no cache entry is present, a request to the entities will be made.
	// If no AuditLogConfig can be found, the function returns a new empty
	// AuditLogConfig.
	GetByGuildId(guildId uint) (*entities.AuditLogConfig, error)

	// Create saves the passed entities.AuditLogConfig in the db.
	// Use Update or Save to update an already existing Guild.
	Create(auditLogConfig *entities.AuditLogConfig) error
	// Save updates the passed entities.AuditLogConfig 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(auditLogConfig *entities.AuditLogConfig) error
	// Update updates the defined field on the entity and saves it in the db.
	Update(auditLogConfig *entities.AuditLogConfig, column string, value interface{}) error
}

AuditLogConfigEntityManager is an entity manager that provides functionality for entities.AuditLogConfig CRUD operations.

type AuditLogEntityManager added in v0.2.0

type AuditLogEntityManager interface {

	// Create saves the passed entities.AuditLog entry in the db.
	// Use Update or Save to update an already existing Guild.
	Create(auditLog *entities.AuditLog) error
	// Save updates the passed entities.AuditLog 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(auditLog *entities.AuditLog) error
	// Update updates the defined field on the entity and saves it in the db.
	Update(auditLog *entities.AuditLog, column string, value interface{}) error
}

AuditLogEntityManager is an entity manager that provides functionality for entities.AuditLog CRUD operations.

type BotAuditLogger added in v0.2.0

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

BotAuditLogger represents the service type to do bot audit logging.

func (*BotAuditLogger) Log added in v0.2.0

func (bal *BotAuditLogger) Log(guild *discordgo.Guild, user *discordgo.User, msg string, announce bool)

Log creates an audit log entry. The function always creates a database entry using the passed parameters. When announce is true and a channel has been configured for bot audit logs, the audit log entry will also be announced.

type Categories added in v0.3.0

type Categories []Category

Categories holds a list of Categories

type Category added in v0.3.0

type Category string

Category is used to categorize commands and components into logical groups.

type Command added in v0.0.3

type Command struct {
	Cmd      *discordgo.ApplicationCommand
	Global   bool
	Category Category
	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

func (Command) GetComponentCode added in v0.4.0

func (c Command) GetComponentCode() entities.ComponentCode

GetComponentCode returns the code of the component that owns the 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)
	// GetCommandsForComponent returns all commands for the
	// specified component. The component needs to be specified by its unique code.
	GetCommandsForComponent(code entities.ComponentCode) []*Command
	// GetCommands returns all currently registered commands.
	GetCommands() []*Command
	// 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 Component

type Component struct {
	// Metadata
	Code entities.ComponentCode
	Name string
	// Categories are filled with the categories of the commands
	// that are owned by the component. Manually added categories will
	// be still merged for components that do not have any command.
	Categories   Categories
	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) BotAuditLogger added in v0.2.0

func (c *Component) BotAuditLogger() *BotAuditLogger

BotAuditLogger returns the bot audit logger for the current component, which allows to create audit log entries.

func (*Component) BotStatusManager added in v0.2.0

func (c *Component) BotStatusManager() StatusManager

BotStatusManager returns the current StatusManager which allows to add additional status to the bot.

func (*Component) DiscordApi added in v0.2.0

func (c *Component) DiscordApi() DiscordApiWrapper

DiscordApi is used to obtain the components slash DiscordApiWrapper management

On first call, this function initializes the private Component.discordAPi field. On consecutive calls, the already present DiscordGoApiWrapper will be used.

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 general management of event handlers.

It should be always used when event handlers to listen to Discord events are necessary. It natively handles stuff like logging, component status and permissions.

func (*Component) LoadComponent added in v0.0.3

func (c *Component) LoadComponent(discord *discordgo.Session) error

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

func (c *Component) Logger() services.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

func (c *Component) SetLogger(l services.Logger)

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

func (c *Component) UnloadComponent(*discordgo.Session) error

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. The methods provided by ComponentHandlerContainer include automated logging and wrappers to apply some standards to all handlers.

func (*ComponentHandlerContainer) Register

func (c *ComponentHandlerContainer) Register(name string, handler interface{}) (HandlerName, bool)

Register can be used to register a new AssignedEventHandler.

The passed handler function will be:

  1. registered in DiscordGo as a Handler
  2. wrapped with some internal logic of the bot
  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)

func (*ComponentHandlerContainer) RegisterOnce

func (c *ComponentHandlerContainer) RegisterOnce(
	name string,
	handler interface{},
) (HandlerName, 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. wrapped with some internal logic of the bot
  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)

func (*ComponentHandlerContainer) Unregister

func (c *ComponentHandlerContainer) Unregister(handlerName HandlerName) 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 {
	// Register can be used to register a new AssignedEventHandler.
	//
	// The passed handler function will be:
	//   1. registered in DiscordGo as a Handler
	//   2. wrapped with some internal logic of the bot
	//   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{}) (HandlerName, 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. wrapped with some internal logic of the bot
	//   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{}) (HandlerName, 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 HandlerName) error

	// UnregisterAll unregisters all event handlers.
	// Internally, it does the same as calling Unregister for all handlers.
	UnregisterAll()
}

ComponentHandlerManager is the interface that defines all methods for event handler management.

The functions of the interface allow registration and removal of Discord event handlers.

Under the hood, DiscordGo event handlers are used.

type DiscordApiWrapper added in v0.2.0

type DiscordApiWrapper interface {
	// GuildCount returns the number of guilds the bot is currently on.
	GuildCount() int
	// SetBotStatus updates the status of the bot according to the passed
	// SimpleBotStatus data.
	SetBotStatus(status SimpleBotStatus) error
}

DiscordApiWrapper provides some useful functions of the discord API, that might be needed without an ongoing event. An example would be the WebAPI.

Also, the functions of this interface are sharding compatible. As soon as sharding is enabled, functions like GuildCount will still output the proper value.

type DiscordGoApiWrapper added in v0.2.0

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

DiscordGoApiWrapper is a wrapper around some crucial discordgo functions. It provides functions that might be frequently used without an ongoing event.

func (*DiscordGoApiWrapper) GuildCount added in v0.2.0

func (dgw *DiscordGoApiWrapper) GuildCount() int

GuildCount returns the number of guilds the bot is currently on.

TODO: As soon as sharding support is implemented, the guild count needs to be computed from data collected across all shards

func (*DiscordGoApiWrapper) SetBotStatus added in v0.2.0

func (dgw *DiscordGoApiWrapper) SetBotStatus(status SimpleBotStatus) error

SetBotStatus updates the status of the bot according to the passed SimpleBotStatus data.

type DiscordGoStatusManager added in v0.2.0

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

DiscordGoStatusManager holds the available status of the bot and manages the cycling of these.

func (*DiscordGoStatusManager) AddStatusToRotation added in v0.2.0

func (dgsm *DiscordGoStatusManager) AddStatusToRotation(status SimpleBotStatus)

AddStatusToRotation adds the given status to the list of rotated status.

func (*DiscordGoStatusManager) Next added in v0.2.0

Next works like next on an iterator which self resets automatically.

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) AuditLog added in v0.2.0

func (em *EntityManager) AuditLog() AuditLogEntityManager

AuditLog returns the AuditLogEntityManager that is currently active, which can be used to do entities.AuditLog specific entities actions.

func (*EntityManager) AuditLogConfig added in v0.2.0

func (em *EntityManager) AuditLogConfig() AuditLogConfigEntityManager

AuditLogConfig returns the AuditLogConfigEntityManager that is currently active, which can be used to do entities.AuditLogConfig specific entities actions.

func (*EntityManager) DB added in v0.0.3

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(registeredComponentStatusId 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 HandlerName added in v0.2.0

type HandlerName string

HandlerName is the name of a specific handler that belongs to a specific component. Its format should be always `component_name_handler_name`.

func GetHandlerName

func GetHandlerName(c entities.ComponentCode, name string) HandlerName

GetHandlerName returns the name of a Handler for a component.

It acts as the auto-formatter that should be used to retrieve Handler names.

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 entities.ComponentCode) (*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 entities.ComponentCode)
}

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
	// BotAuditLogger returns the bot audit logger for the current component,
	// which allows to create audit log entries.
	BotAuditLogger() *BotAuditLogger
	// DiscordApi is used to obtain the components slash DiscordApiWrapper management
	//
	// On first call, this function initializes the private Component.discordAPi
	// field. On consecutive calls, the already present DiscordGoApiWrapper will be used.
	DiscordApi() DiscordApiWrapper
	// BotStatusManager returns the current StatusManager which
	// allows to add additional status to the bot.
	BotStatusManager() StatusManager
}

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 SimpleBotStatus added in v0.2.0

type SimpleBotStatus struct {
	ActivityType discordgo.ActivityType
	Content      string
	Url          string
}

SimpleBotStatus is a simplified version of discordgo.UpdateStatusData that can be used to simply change the status of the bot to something else. Note that the URL should be only set for discordgo.ActivityTypeStreaming.

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) GetCommands added in v0.4.0

func (c *SlashCommandManager) GetCommands() []*Command

GetCommands returns all currently registered commands.

func (*SlashCommandManager) GetCommandsForComponent added in v0.3.0

func (c *SlashCommandManager) GetCommandsForComponent(code entities.ComponentCode) []*Command

GetCommandsForComponent returns all commands for the specified component. The component needs to be specified by its unique code.

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

func (*SlashCommandManager) SyncApplicationComponentGlobalCommands added in v0.3.0

func (c *SlashCommandManager) SyncApplicationComponentGlobalCommands(
	session *discordgo.Session,
)

SyncApplicationComponentGlobalCommands ensures that the available discordgo.ApplicationCommand are synced for the given component globally.

This means that disabled commands are enabled and enabled commands are disabled depending on their global 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?

type StatusManager added in v0.2.0

type StatusManager interface {
	// AddStatusToRotation adds the given status to the list of
	// rotated status.
	AddStatusToRotation(status SimpleBotStatus)
	// Next works like next on an iterator which self resets automatically.
	Next() *SimpleBotStatus
}

StatusManager manages the available status which are set fopr the bot.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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