feat(commands): add command definitions and channel-aware registry

This commit is contained in:
mingmxren 2026-03-01 02:25:29 +08:00
parent f94d6ae4d3
commit 885dc310e3
3 changed files with 56 additions and 0 deletions

View file

@ -0,0 +1,9 @@
package commands
type Definition struct {
Name string
Description string
Usage string
Aliases []string
Channels []string
}

26
pkg/commands/registry.go Normal file
View file

@ -0,0 +1,26 @@
package commands
type Registry struct {
defs []Definition
}
func NewRegistry(defs []Definition) *Registry {
return &Registry{defs: defs}
}
func (r *Registry) ForChannel(channel string) []Definition {
out := make([]Definition, 0, len(r.defs))
for _, d := range r.defs {
if len(d.Channels) == 0 {
out = append(out, d)
continue
}
for _, ch := range d.Channels {
if ch == channel {
out = append(out, d)
break
}
}
}
return out
}

View file

@ -0,0 +1,21 @@
package commands
import "testing"
func TestRegistry_FilterByChannel(t *testing.T) {
defs := []Definition{
{Name: "help", Description: "Show help"},
{Name: "admin", Description: "Admin only", Channels: []string{"telegram"}},
}
r := NewRegistry(defs)
gotTG := r.ForChannel("telegram")
if len(gotTG) != 2 {
t.Fatalf("telegram defs = %d, want 2", len(gotTG))
}
gotWA := r.ForChannel("whatsapp")
if len(gotWA) != 1 || gotWA[0].Name != "help" {
t.Fatalf("whatsapp defs = %+v, want only help", gotWA)
}
}