Files
Discord-Bot-Go/internal/commands/registry.go
T

39 lines
919 B
Go

package commands
import (
"context"
"github.com/disgoorg/disgo/discord"
"github.com/disgoorg/disgo/events"
)
type Command interface {
Definition() discord.ApplicationCommandCreate
Execute(ctx context.Context, event *events.ApplicationCommandInteractionCreate) error
}
type Registry struct {
commands map[string]Command
}
func NewRegistry(cmds ...Command) *Registry {
registry := &Registry{commands: make(map[string]Command, len(cmds))}
for _, cmd := range cmds {
registry.commands[cmd.Definition().CommandName()] = cmd
}
return registry
}
func (r *Registry) Get(name string) (Command, bool) {
cmd, ok := r.commands[name]
return cmd, ok
}
func (r *Registry) Definitions() []discord.ApplicationCommandCreate {
definitions := make([]discord.ApplicationCommandCreate, 0, len(r.commands))
for _, cmd := range r.commands {
definitions = append(definitions, cmd.Definition())
}
return definitions
}